# add\_host and --limit

**URL:** <https://forum.ansible.com/t/add-host-and-limit/30486>\
**Category:** Ansible Project\
**Created:** [June 4, 2019, 12:58pm UTC](https://forum.ansible.com/t/add-host-and-limit/30486 "2019-06-04T12:58:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![powcarz](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@powcarz](https://forum.ansible.com/u/powcarz)\
**Post date:** [June 4, 2019, 12:58pm UTC](https://forum.ansible.com/t/add-host-and-limit/30486/1 "2019-06-04T12:58:18Z")

</div>

Hello All

I have the following inventory:  
…  
[group1]  
host1

[group2]  
host2

[all]  
host1  
host2  
…

and a playbook:

- name: play1  
hosts: group1  
tasks:

- add\_host:  
name: “{{groups[group2] | first}}”  
groups: temp\_group

- debug: var=groups.temp\_group

- name: play2  
hosts: temp\_group  
tasks:

- debug: msg=“yay”

My intention is to execute a play on host1, where I read a host on which next play is going to be executed, and execute the second play on that host, but when I execute the playbook with --limit group1 even though the host2 is added to temp\_group, ansible won’t match any hosts to run the second play on. The output (writing from top of my head) :

$ ansible-playbook playbook.yml --limit group1  
PLAY [play1] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
TASK [add\_host] \*\*\*\*\*\*\*\*\*\*\*\*  
ok: [host1]  
TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [host1] =\> {  
“groups.temp\_group” : [  
“host2”  
]  
}  
PLAY [play2] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: no hosts matched

Is there any way to make the add\_host work with --limit?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 5, 2019, 7:12am UTC](https://forum.ansible.com/t/add-host-and-limit/30486/2 "2019-06-05T07:12:31Z")

</div>

The whole point with limit is to say, only run on this/these host(s), so no unless you add that host to the limit statement.

Since you only have one host in group1 limit is not needed.

---

<div class="post-metadata">

**Author:** ![Knut\_Karevoll](https://avatars.discourse-cdn.com/v4/letter/k/5e9695/32.png) [@Knut\_Karevoll](https://forum.ansible.com/u/Knut_Karevoll)\
**Post date:** [June 5, 2019, 7:58am UTC](https://forum.ansible.com/t/add-host-and-limit/30486/3 "2019-06-05T07:58:17Z")

</div>

Firstly you should have a look at the delegate\_to which might be a better way at achieving what you want. But I am assuming there is a good reason for your current approach. The reason it does not work is that the limit is still in place after you have done an add\_host. So when it gets to the second play it see that there are no hosts both in temp\_group and group1 and will therefore not do anything. One solution is to add temp\_group to your limit. So you would run “ansible-playbook playbook.yml --limit group1:temp\_group”.

---

<div class="post-metadata">

**Author:** ![powcarz](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@powcarz](https://forum.ansible.com/u/powcarz)\
**Post date:** [June 5, 2019, 7:58am UTC](https://forum.ansible.com/t/add-host-and-limit/30486/4 "2019-06-05T07:58:46Z")

</div>

Thanks Kai,  
I solved this with delegate\_to, fortunately it was only a few simple tasks to execute on host2.  
Out of curiousity, where does Ansible runtime keep the set of hosts which are the subject of a playbook, after being --limit-ed? Is it accesible from inside a playbook? Would a custom module have the access there?

Piotr

śr., 5.06.2019, 09:12 użytkownik Kai Stian Olstad \<[ansible-project+list@olstad.com](mailto:ansible-project%2Blist@olstad.com)\> napisał:

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 5, 2019, 8:42am UTC](https://forum.ansible.com/t/add-host-and-limit/30486/5 "2019-06-05T08:42:49Z")

</div>

Ansible has a list of special variables and the one that gives you that information is ansible\_play\_hosts\_all.

For all of them check this page  
[https://docs.ansible.com/ansible/latest/reference\_appendices/special\_variables.html](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html)
