1

I know "all" doesn't match the implicit localhost; the title succinctly expresses my question.

Motivation: I have a playbook that needs to be run on cloud hosts generated dynamically and added to groups with add_host during a playbook, but I also need to run the whole playbook on a vagrant vm, as well as in CI (molecule), so I want to use "all" for the latter two cases, so that when I specify inventory, certain plays will target the inventoried hosts. These plays will also have the names of the add_host groups, so that I don't have to duplicate plays. I'm just wondering if I can write

hosts: all # instead of

hosts: all:group1:group1...

for these plays, and have the add_host hosts included, without specifying their names. I will only specify inventory for the vagrant and molecule scenarios, and not for the live cloud deployment scenario, so that I don't target my entire cloud infrastructure.

2

Q: "Does the hosts: all pattern only match hosts specified in inventory, or does it also match add_host hosts?"

A: Simple test shows hosts: all includes also added hosts by add_host. In the same playbook, of course. The dynamic groups live inside a playbook only.

Given the inventory

shell> cat hosts
host1
host2

The playbook

---
- name: Only 2 hosts in the inventory
  hosts: all
  gather_facts: false
  tasks:
    - debug:
        var: ansible_play_hosts_all
      run_once: true

- name: Add host3 to the group1
  hosts: all
  gather_facts: false
  tasks:
    - add_host:
        name: host3
        groups: group1
      run_once: true

- name: All 3 hosts are available to this play
  hosts: all
  gather_facts: false
  tasks:
    - debug:
        var: ansible_play_hosts_all
      run_once: true

gives

PLAY [Only 2 hosts in the inventory] **************************************

TASK [debug] **************************************************************
ok: [host1] => 
  ansible_play_hosts_all:
  - host1
  - host2

PLAY [Add host3 to the group3] ********************************************

TASK [add_host] ***********************************************************
changed: [host1]

PLAY [All 3 hosts are available to this play] *****************************

TASK [debug] **************************************************************
ok: [host3] => 
  ansible_play_hosts_all:
  - host3
  - host1
  - host2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.