Ansible - The HowTo's


How to specify target hosts ?

A few words of warning :

Examples below :

Now let's target hosts :

Target Syntax Comment
by name
a list of hosts ansible host1,host2,host3
a list of hosts matching a regular expression ansible '~server0[12]\.acme\.org'
  • matches and
  • make sure you're not limiting the effective target with the ansible-playbook -l flag
by group
all hosts of a single group ansible groupName
all hosts known to Ansible ansible all
hosts of several groups ansible group1:group2:group3 The colon : actually means a logical OR. This command above applies to any host belonging either to group1 or to group2 or to group3
When it comes to complex rules with intersections and exclusions (see examples), it may not be a REAL "logical OR"
hosts belonging to the 2 specified groups ansible 'group1:&group2'
  • aka intersection of groups
by name + group
all hosts except those matching an expression
  • ansible 'all:!expression*'
  • ansible 'groupName:!badHost'
all hosts of a group except several of them ansible 'groupName:!~(host1|host2)'
  • ~ is used to introduce a regular expression
  • you get the idea to exclude as many hosts as necessary

How to loop on a list of items ?

Let's consider this play :
- hosts:
  connection: local
  gather_facts: no

  - set_fact:
      fruits: [

  - debug:
      var: item
      - fruits

  - debug:
      var: item
      - "{{ fruits }}"

  - debug:
      var: item
      "{{ fruits }}"
which outputs :
TASK [debug] ********************************************************************
ok: [] => (item=fruits) => {
    "item": "fruits"

TASK [debug] ********************************************************************
ok: [] => (item=[u'apple', u'orange', u'banana']) => {
    "item": [

TASK [debug] ********************************************************************
ok: [] => (item=apple) => {
    "item": "apple"
ok: [] => (item=orange) => {
    "item": "orange"
ok: [] => (item=banana) => {
    "item": "banana"


- fruits
  • this passes the string fruits, not the fruits variable
  • use "{{ }}" to pass a variable
- "{{ fruits }}"
  • the leading dash - introduces a list item
  • _HERE_ it's a one-item list, and this item happens to be a list
  • Anyway, this is why the loop turns only once and displays all fruits at once
"{{ fruits }}"
this is what we expected

How to interrupt a playbook (i.e. something like --stop-at-task) ?

Situation :

Solution :

  - fail:
      msg: "Playbook stopped on purpose for whatever reason"
This stops the execution abruptly, with a failure status, making this solution mostly suited for debugging. It has the advantage that :

Alternate solution :

This _can_ do the job too :
- meta: end_play
But it's fairly different from the fail method : Check it with metaEndPlay.yml.

How to override /etc/ansible/ansible.cfg settings with personal values ?

Create ~/.ansible.cfg and replicate + override the required section / values :
host_key_checking	= False
inventory		= /home/stuart/ansible/hosts
roles_path		= /home/stuart/ansible/roles
vault_password_file	= /var/lib/ansible/.vault_password

How to use with_items ?

Because I can never remember how to use with_items, here's an example :
- name: unmount volume groups
    src: "{{ item.device }}"
    name: "{{ item.mountPoint }}"		will be path in Ansible 2.3+
    state: unmounted
    - { device: '/dev/mapper/vg_data-data', mountPoint: '/var/lib/docker/devicemapper' }
    - { device: '/dev/mapper/vg_data-data', mountPoint: '/var/lib/docker' }