However, the output may be empty. The possible reason is that in the remote host, the bash interpreter is specifically configured. So we need to edit our test.sh to add an interpreter for our script, such as:
#!/bin/bash
echo $1
Use script module to run local ansible on a remote host.
With script module, we can also control a host that is connected through an intermediate host.
The trick is to run a playbook in the intermediate host, but it requires the intermediate host to have the Ansible config to connect to our actual target host. With this method, we can put all our scripts in the localhost insteads of uploading to the imtermediate host.
An example:
# playbook in the localhost
- hosts: intermediate_host
vars_files:
- ./vars/main.yml
tasks:
- script: './files/run-me-in-the-intermediate-host.yml' #the file
register: output
- debug:
var: output
failed_when: '"FAILED! =>" in output.stdout'
tags: [I-am-a-tag]
#! /usr/bin/env ansible-playbook
# run-me-in-the-intermediate-host.yml
# To be executed in the intermediate host
- hosts: actual-target-host
tasks:
- shell: ls
register: output
- debug:
var: output
failed_when: output.stderr != ''
Dynamically add a host (ubuntu) that uses a PEM.
- name: Add a host
add_host:
groups: "{{ GROUP_NAME }}"
name: "{{ IP }}"
ansible_user: ubuntu
ansible_ssh_private_key_file: "{{ PEM_PATH }}"
Add a host that is connected through an intermediate host
Confusion of tags on the role and an imported playbook
There is a usage of tags:
- name: A playbook.
hosts: hostX
roles:
- { role: A, tags: [B] }
- import_playbook: a.yml
tags: [B]
Intuitively, we may think it means run all the tasks tagged with B in the role A and the imported playbooka.yml, but it is not true. It actually means adding a tag B to the role A and the playbook importing action.
Syntax to run a playbook with multiple tags or skipped tags
This note records some common usage of Ansible that may not appear in the Ansible official docs.
Use
script
module to run local scripts on a remote host.We may need to use
script
to run a local script on a remote host, and our bash filetest.sh
may be as simple as:And our playbook is like:
However, the
output
may be empty. The possible reason is that in the remote host, the bash interpreter is specifically configured. So we need to edit ourtest.sh
to add an interpreter for our script, such as:Use
script
module to run local ansible on a remote host.With
script
module, we can also control a host that is connected through an intermediate host.The trick is to run a playbook in the intermediate host, but it requires the intermediate host to have the Ansible config to connect to our actual target host. With this method, we can put all our scripts in the localhost insteads of uploading to the imtermediate host. An example:
Dynamically add a host (ubuntu) that uses a PEM.
Add a host that is connected through an intermediate host
Confusion of tags on the role and an imported playbook
There is a usage of
tags
:Intuitively, we may think it means run all the tasks tagged with
B
in the roleA
and the imported playbooka.yml
, but it is not true. It actually means adding a tagB
to the roleA
and the playbook importing action.Syntax to run a playbook with multiple tags or skipped tags
$ ansible-playbook {my playbook} --tags "{tag1}, {tag2}"
$ ansible-playbook {my playbook} --skip-tags "{tag1}, {tag2}"