# Why does delegate\_to group with include\_role run command on local machine?

**URL:** <https://forum.ansible.com/t/why-does-delegate-to-group-with-include-role-run-command-on-local-machine/34423>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [January 11, 2021, 11:11pm UTC](https://forum.ansible.com/t/why-does-delegate-to-group-with-include-role-run-command-on-local-machine/34423 "2021-01-11T23:11:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jordan\_C](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Jordan\_C](https://forum.ansible.com/u/Jordan_C)\
**Post date:** [January 11, 2021, 11:11pm UTC](https://forum.ansible.com/t/why-does-delegate-to-group-with-include-role-run-command-on-local-machine/34423/1 "2021-01-11T23:11:43Z")

</div>

I am trying to debug a playbook I’ve written which uses a couple of roles to spin up and then configure an AWS instance.

The basic structure is one playbook (`new-server.yml`) imports two roles – `roles/ec2_instance` and `roles/start_env`. The ec2\_instance role should be ran on localhost with my AWS tokens and then the start\_env role gets ran on servers which are generated by the first role.

My playbook `new-server.yml` starts off like this:

- name: provision new instance  
include\_role:  
name: ec2\_instance  
public: yes  
vars:  
instance\_name: “{{ item.host\_name }}”  
env: “{{ item.git\_branch }}”  
env\_type: “{{ item.env\_type }}”  
loop:
- { host\_name: ‘prod’, git\_branch: ‘master’, env\_type: ‘prod’ }
- { host\_name: ‘test’, git\_branch: ‘test’, env\_type: ‘devel’}

This role builds an ec2 instance, updates route 53, uses add\_host to add the host to the in-memory inventory in the `just_created` group.

Next, I have this in the `new_server.yml` playbook. Both of my IPs show up here just fine. My localhost does not show up here.

- name: debug just\_created group  
debug: msg=“{{ groups[‘just\_created’] }}”

Finally, again in `new_server.yml`, I try to do the last mile configuration and start my application on the new instance:

- name: Configure and start environment on new instance  
include\_role:  
name: start\_env  
apply:  
become: yes  
delegate\_to: “{{ item }}”  
with\_items:
- “{{ groups[‘just\_created’] }}”

However, it doesnt look like the task is delegating properly, because I have this task in `roles/start_env/main.yml`:

- name: debug hostname  
debug: msg=“{{ ansible\_hostname }}”

And what I’m seeing in my output is

TASK [start\_env : debug hostname] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Monday 11 January 2021 12:00:05 -0800 (0:00:00.111) 0:00:37.374 \*\*\*\*\*\*\*\*  
ok: [localhost → 10.20.15.225] =\> {  
“msg”: “My-Local-MBP”  
}

TASK [start\_env : debug hostname] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Monday 11 January 2021 12:00:05 -0800 (0:00:00.043) 0:00:37.417 \*\*\*\*\*\*\*\*  
ok: [localhost → 10.20.31.35] =\> {  
“msg”: “My-Local-MBP”  
}

I’ve read a lot about delegate\_to, include\_role and loops this morning. It sounds like Ansible has made things pretty complicated when you want to combine these things, but it also seems like the way I am trying to invoke these should be right. Any idea what I’m doing wrong (or if there is a smarter way to do this? I found this – [https://medium.com/opsops/ansible-2-5-delegate-to-and-include-role-20cd7e67008e](https://medium.com/opsops/ansible-2-5-delegate-to-and-include-role-20cd7e67008e) – and while its a clever workaround, it doesn’t quite fit what I’m seeing and I’d like to avoid creating another tasks file in my roles. Not exactly how I want to manage something like this. Most of the information I’ve been going off of has been this thread:

[https://github.com/ansible/ansible/issues/35398](https://github.com/ansible/ansible/issues/35398)

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [January 11, 2021, 11:19pm UTC](https://forum.ansible.com/t/why-does-delegate-to-group-with-include-role-run-command-on-local-machine/34423/2 "2021-01-11T23:19:34Z")

</div>

The output shows `[localhost -> 10.20.31.35] ` which indicates it is delegating from `localhost` to `10.20.31.35`, however this is only for the connection. Any templating done in the task definition uses the values of the host in the loop, which is `localhost`.

---

<div class="post-metadata">

**Author:** ![Jordan\_C](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Jordan\_C](https://forum.ansible.com/u/Jordan_C)\
**Post date:** [January 11, 2021, 11:26pm UTC](https://forum.ansible.com/t/why-does-delegate-to-group-with-include-role-run-command-on-local-machine/34423/3 "2021-01-11T23:26:37Z")

</div>

I see. Thanks for your response. Are you aware of a good way to use the vars within the role? It seems like import\_role would do that, but would not permit the loop.

---

<div class="post-metadata">

**Author:** ![Jordan\_C](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Jordan\_C](https://forum.ansible.com/u/Jordan_C)\
**Post date:** [January 12, 2021, 12:27am UTC](https://forum.ansible.com/t/why-does-delegate-to-group-with-include-role-run-command-on-local-machine/34423/4 "2021-01-12T00:27:39Z")

</div>

I figured out something in my own way that allows me to most keep what I’ve already written the same. I modified my add\_host task to use the instance\_name var as the hostname and the ec2 IP as the ansible\_host instance var and then updated my last task to

roles/aws.yml:

- name: Add new instance to inventory  
add\_host:  
hostname: “{{ instance\_name }}”  
ansible\_host: “{{ ec2\_private\_ip }}”  
ansible\_user: centos  
ansible\_ssh\_private\_key\_file: …/keys/my-key.pem  
groups: just\_created

new\_servers.yml:

tasks:

- name: provision new instance  
include\_role:  
name: ec2\_instance  
public: yes  
vars:  
instance\_name: “{{ item.host\_name }}”  
env: “{{ item.git\_branch }}”  
env\_type: “{{ item.env\_type }}”  
loop:

- { host\_name: ‘prod’, git\_branch: ‘master’, env\_type: ‘prod’ }

- { host\_name: ‘test’, git\_branch: ‘test’, env\_type: ‘devel’}

- name: Configure and start environment on new instance  
include\_role:  
name: start\_env  
apply:  
become: yes  
delegate\_to: “{{ item }}”  
vars:  
instance\_name: “{{ item }}”  
with\_items:

- “{{ groups[‘just\_created’] }}”

Works well enough and lets me avoid adding that dict to every subsequent included role.
