Overview

A collection of useful tips and tricks for Ansible which don't really justify a full blog post on their own. I'll keep updating this post when I come across something of value.

Callbacks

Convert the output of a playbook run to json. Either set the stdout_callback = json setting in the ansible.cfg or alternatively set the ANSIBLE_STDOUT_CALLBACK=json variable at the CLI on playbook execution.

cmd
ANSIBLE_STDOUT_CALLBACK=json ansible-playbook some-playbook.yml

Inventory

create an ad-hoc inventory using the -i or --inventory flags. An ad-hoc inventory allows you to run ansible or ansible-playbook against a node (or list of nodes) without creating an inventory file.

cmd
ansible-playbook -i myhost,<another-host> all -a 'uname -a'
Credit

Roles

There is a fair bit of boilerplate involved in creating a role. There is an ansible-galaxy command to aid in the process of building a role. Optionally add the --offline flag if the role is not intended for Ansible galaxy.

cmd
ansible-galaxy init role-name --offline

        # output
        - role-name was created successfully

        # role-name directory contents
        role-name/
        ├── defaults
        │   └── main.yml
        ├── files
        ├── handlers
        │   └── main.yml
        ├── meta
        │   └── main.yml
        ├── README.md
        ├── tasks
        │   └── main.yml
        ├── templates
        ├── tests
        │   ├── inventory
        │   └── test.yml
        └── vars
            └── main.yml

        8 directories, 8 files
Credit

Variables

Get a json dict of all vars that will be applicable to a host at playbook runtime. The following command also shows you which groups a host belongs to and all groups that are available.

cmd
ansible -i inventory a.example.com -m debug -a "var=hostvars[inventory_hostname]"

        # output
        a.example.com | success >> {
            "var": {
                "hostvars": {
                    "ansible_ssh_host": "10.0.0.1",
                    "ansible_ssh_user": "user",
                    "group_names": [
                        "group1",
                        "groups"
                    ],
                    "groups": {
                        "all": [
                            "x.example.com",
                            "y.example.com",
                            "a.example.com",
                            "b.example.com"
                        ],
                        "group1": [
                            "a.example.com"
                        ],
                        "group2": [
                            "b.example.com"
                        ],
                        "groups": [
                            "a.example.com",
                            "b.example.com"
                        ],
                        "ungrouped": [
                            "x.example.com",
                            "y.example.com"
                        ]
                    },
                    "inventory_hostname": "a.example.com",
                    "inventory_hostname_short": "a"
                }
            }
        }
Credit

Find the source file that a variable was derived from. This is quite useful when you are using the Ansible variable precedence system to see which file a variable actually came from.

cmd
ansible -i inventory leaf01 -m debug -a "var=hostvars[inventory_hostname]['bgp']['asn']._data_source"

        # output
        leaf01 | SUCCESS => {
            "hostvars[inventory_hostname]['bgp']['asn']._data_source": "/path/to/host_vars/leaf01.yml"
        }
Credit

Tags