Joldnine / joldnine.github.io

My github.io blog repo. https://joldnine.github.io
2 stars 1 forks source link

Ansible: Note #15

Open Joldnine opened 6 years ago

Joldnine commented 6 years ago

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 file test.sh may be as simple as:

echo $1

And our playbook is like:

- hosts: my_remote_host
  tasks:
    - script: ./test.sh first_arg
      register: output
    - debug:
        var: output

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. image

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

- add_host:
    groups: {{ HOST_GROUPS }}
    name: {{ HOST_IP }}
    ansible_user: ubuntu
    ansible_ssh_private_key_file: "{{ PEM_FOR_HOST }}"
    ansible_ssh_common_args: '-o ProxyCommand="ssh -i {{ PEM_PATH_FOR_INTERMEDIATE }} -W %h:%p -q ubuntu@{{ INTERMEDIATE_HOST_IP }}"'

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

$ ansible-playbook {my playbook} --tags "{tag1}, {tag2}" $ ansible-playbook {my playbook} --skip-tags "{tag1}, {tag2}"