# add\_host scope from inside roles

**URL:** <https://forum.ansible.com/t/add-host-scope-from-inside-roles/32853>\
**Category:** Ansible Project\
**Created:** [May 5, 2020, 4:48pm UTC](https://forum.ansible.com/t/add-host-scope-from-inside-roles/32853 "2020-05-05T16:48:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicola\_L](https://avatars.discourse-cdn.com/v4/letter/n/9dc877/32.png) [@Nicola\_L](https://forum.ansible.com/u/Nicola_L)\
**Post date:** [May 5, 2020, 4:48pm UTC](https://forum.ansible.com/t/add-host-scope-from-inside-roles/32853/1 "2020-05-05T16:48:44Z")

</div>

Hi all,  
I’m a bit confused about the behavior of add\_host when used from inside a role or directly in the playbook.  
Am I doing something wrong or am I just hitting some role’s scope boundary?

THIS WORKS (from the playbook)

---

<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:** [May 5, 2020, 6:28pm UTC](https://forum.ansible.com/t/add-host-scope-from-inside-roles/32853/2 "2020-05-05T18:28:33Z")

</div>

The code is one playbook with three plays. It's not possible to put a  
playbook "inside a role".

In particular, it's not possible to apply "hosts" in a "Role"  
[https://docs.ansible.com/ansible/latest/reference\_appendices/playbooks\_keywords.html#role](https://docs.ansible.com/ansible/latest/reference_appendices/playbooks_keywords.html#role)

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Nicola\_L](https://avatars.discourse-cdn.com/v4/letter/n/9dc877/32.png) [@Nicola\_L](https://forum.ansible.com/u/Nicola_L)\
**Post date:** [May 6, 2020, 7:51am UTC](https://forum.ansible.com/t/add-host-scope-from-inside-roles/32853/3 "2020-05-06T07:51:48Z")

</div>

Hi Vladimir,  
thanks for pointing out that mistake.  
I fixed the role, but I still have the same behaviour.  
You can find below the full example with the output.

PLAYBOOK

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 6, 2020, 2:20pm UTC](https://forum.ansible.com/t/add-host-scope-from-inside-roles/32853/4 "2020-05-06T14:20:20Z")

</div>

this is just a variable named 'groupname'  
&nbsp;&nbsp;groupname: launched

Per the docs [https://docs.ansible.com/ansible/latest/modules/add\_host\_module.html](https://docs.ansible.com/ansible/latest/modules/add_host_module.html),  
if you actually want to add it to the 'launched' group:

groups: launched

---

<div class="post-metadata">

**Author:** ![Nicola\_L](https://avatars.discourse-cdn.com/v4/letter/n/9dc877/32.png) [@Nicola\_L](https://forum.ansible.com/u/Nicola_L)\
**Post date:** [May 6, 2020, 2:38pm UTC](https://forum.ansible.com/t/add-host-scope-from-inside-roles/32853/5 "2020-05-06T14:38:27Z")

</div>

Hi Brian,  
thanks a lot, that was indeed one part of the problem.

I also had to remove the meta: refresh\_inventory at the end of the first play.

WORKING VERSION
