<p>Q: <em>&quot;<strong>Does the <code>hosts: all</code> pattern only match hosts specified in inventory, or does it also match <code>add_host</code> hosts?</strong>&quot;</em></p>
<p>A: Simple test shows <code>hosts: all</code> includes also added hosts by <code>add_host</code>. In the same playbook, of course. The dynamic groups live inside a playbook only.</p>
<p>Given the inventory</p>
<pre class="lang-sh prettyprint-override"><code>shell&gt; cat hosts
host1
host2
</code></pre>
<p>The playbook</p>
<pre class="lang-yaml prettyprint-override"><code>---
- 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
</code></pre>
<p>gives</p>
<pre class="lang-yaml prettyprint-override"><code>PLAY [Only 2 hosts in the inventory] **************************************

TASK [debug] **************************************************************
ok: [host1] =&gt; 
  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] =&gt; 
  ansible_play_hosts_all:
  - host3
  - host1
  - host2
</code></pre>

CC BY-SA 4.0
2021-07-24T22:29:38.547Z
2021-07-25T05:21:37.230Z
2021-07-25T05:21:37.230Z
Vladimir Botka
<p>I know &quot;all&quot; doesn't match the implicit localhost; the title succinctly expresses my question.</p>
<p>Motivation: I have a playbook that needs to be run on cloud hosts generated dynamically and added to groups with <code>add_host</code> 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 &quot;all&quot; 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</p>
<p><code>hosts: all</code> # instead of</p>
<p><code>hosts: all:group1:group1...</code></p>
<p>for these plays, and have the <code>add_host</code> 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.</p>

<ansible><ansible-inventory>
Ansible: Does the `hosts: all` pattern in a play only match hosts specified in inventory, or does it also match playbook `add_host` hosts?
Life5ign
# Ansible: Does the `hosts: all` pattern in a play only match hosts specified in inventory, or does it also match playbook `add_host` hosts?

(Asked by: username_0 on 2021-07-24)

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.

**Tags:** ansible, ansible-inventory


---

## 1 answers

### Answer by username_1 on 2021-07-24. Score: 2 | Accepted answer

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


---

