Skip to content

Ansible

Ansible

Usage

Meta

Run handlers (before): - meta: flush_handlers

(http://docs.ansible.com/ansible/playbooks_intro.html#handlers-running-operations-on-change)

Loops

Looping over Subelements

Useful when you have a list of dictionaries and you want to pick the same element from all of them.

with_subelements:
  - "{{ some list }}"
  - interesting_list_item
  - flags:
    skip_missing: yes

(https://docs.ansible.com/ansible/latest/playbooks_loops.html#looping-over-subelements)

Command line

Variables

To provide variables directly from the command line: --extra-vars "version=1.23.45 other_variable=foo".

(https://stackoverflow.com/questions/30662069/how-can-i-pass-variable-to-ansible-playbook-in-the-command-line)

Facts

  • ansible_user_id
Only gather facts when tag matches

To speed up playbooks, disable gather_facts. In the parts where this is requiered, you can use this approach:

- hosts: all
  sudo: yes
  gather_facts: False
  pre_tasks:
   - setup:
       filter: ansible_*
  roles:
   - your_role_here
  tags:
    - tag1

This way ansible will only gather some facts and only if the tag matches (when run with the -t option).

(https://stackoverflow.com/questions/34485286/ansible-gathering-facts-with-filter-inside-a-playbook#34487639)

Debug

Include tags ignored

Example:

- import_tasks: db-setup.yml tags=db-setup

This tags are ignored if you skip them. Bug: (https://github.com/ansible/ansible/issues/9862)

could not find item key in iterated item

Solution: specify a third element in the with_subelements list called flags that is a dict and contains skip_missing: yes.

with_subelements:
  - "{{ some list }}"
  - interesting_list_item
  - flags:
    skip_missing: yes

(https://github.com/ansible/ansible/pull/10995)

Reference

Usage

Modules

Utilities

Vault

Plugins

Timing

Displays tasks timing at the end of the playbook output.

To enable, add callback_whitelist = profile_tasks to ansible.cfg.

(https://github.com/jlafon/ansible-profile)