# Add host using add\_host not taking effect

**URL:** <https://forum.ansible.com/t/add-host-using-add-host-not-taking-effect/32026>\
**Category:** Ansible Project\
**Created:** [January 22, 2020, 4:02pm UTC](https://forum.ansible.com/t/add-host-using-add-host-not-taking-effect/32026 "2020-01-22T16:02:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Punit\_Jain](https://avatars.discourse-cdn.com/v4/letter/p/e56c9b/32.png) [@Punit\_Jain](https://forum.ansible.com/u/Punit_Jain)\
**Post date:** [January 22, 2020, 4:02pm UTC](https://forum.ansible.com/t/add-host-using-add-host-not-taking-effect/32026/1 "2020-01-22T16:02:54Z")

</div>

Hi,

I am doing a very straight forward testing of adding a new host dynamically.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 22, 2020, 4:15pm UTC](https://forum.ansible.com/t/add-host-using-add-host-not-taking-effect/32026/2 "2020-01-22T16:15:50Z")

</div>

Split the playbook to 2 plays. Add the host in the first play, and use it the  
second.

- hosts: all  
&nbsp;&nbsp;&nbsp;connection: local  
&nbsp;&nbsp;&nbsp;gather\_facts: no  
&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: add host  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add\_host:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: 'testserver'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;groups: all  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true

- hosts: all  
&nbsp;&nbsp;&nbsp;connection: local  
&nbsp;&nbsp;&nbsp;gather\_facts: no  
&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: print  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "hello"

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Punit\_Jain](https://avatars.discourse-cdn.com/v4/letter/p/e56c9b/32.png) [@Punit\_Jain](https://forum.ansible.com/u/Punit_Jain)\
**Post date:** [January 22, 2020, 5:46pm UTC](https://forum.ansible.com/t/add-host-using-add-host-not-taking-effect/32026/3 "2020-01-22T17:46:05Z")

</div>

In below usecase it is not working:

run.yml

---

<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:** [January 22, 2020, 6:15pm UTC](https://forum.ansible.com/t/add-host-using-add-host-not-taking-effect/32026/4 "2020-01-22T18:15:27Z")

</div>

> run.yml  
> ----------  
> ---  
> - hosts: all  
> &nbsp;&nbsp;connection: local  
> &nbsp;&nbsp;gather\_facts: no
> 
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: include role task  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include\_role:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: role1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks\_from: hello
> 
> - hosts: all  
> &nbsp;&nbsp;connection: local  
> &nbsp;&nbsp;gather\_facts: no
> 
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: include role task  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include\_role:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: role1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks\_from: bye
> 
> then inside role tasks:  
> ------------------------  
> hello.yml
> 
> - name: add host  
> &nbsp;&nbsp;add\_host:  
> &nbsp;&nbsp;&nbsp;&nbsp;name: 'test'  
> &nbsp;&nbsp;&nbsp;&nbsp;groups: all

There is no point in adding a host to all, the all group are a special group that all hosts is a member of.

> - name: refresh  
> &nbsp;&nbsp;meta: refresh\_inventory

Here you say, purge my inventory and read it in again.  
Then all your dynamic hosts get purged and your inventory is read again which don't have the dynamic ones.  
So just remove this task and it should work.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 22, 2020, 6:55pm UTC](https://forum.ansible.com/t/add-host-using-add-host-not-taking-effect/32026/5 "2020-01-22T18:55:57Z")

</div>

The point is to add a host that hasn't been declared in any inventory yet.

&nbsp;&nbsp;-vlado

---

<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:** [January 22, 2020, 8:09pm UTC](https://forum.ansible.com/t/add-host-using-add-host-not-taking-effect/32026/6 "2020-01-22T20:09:06Z")

</div>

I understand that, it wasn't that I commented on, I can rephrase  
Using "groups: all" in add\_host is pointless since all hosts is automatically added to the group all anyway.
