# Not working together - connection: local and delegate\_to

**URL:** <https://forum.ansible.com/t/not-working-together-connection-local-and-delegate-to/37160>\
**Category:** Ansible Project\
**Created:** [September 30, 2022, 5:40pm UTC](https://forum.ansible.com/t/not-working-together-connection-local-and-delegate-to/37160 "2022-09-30T17:40:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vichu](https://avatars.discourse-cdn.com/v4/letter/v/13edae/32.png) [@Vichu](https://forum.ansible.com/u/Vichu)\
**Post date:** [September 30, 2022, 5:40pm UTC](https://forum.ansible.com/t/not-working-together-connection-local-and-delegate-to/37160/1 "2022-09-30T17:40:11Z")

</div>

Hi Team,

Good Day!! Am not able to use **Connection: local and Delegate\_to** together in my playbooks.

**Actual Result** - Both debug outputs give me the same hostname.  
**Expected Result:** First debug should have given me the localhost hostname and second debug should have given me the “pocansibletest” as per the playbook. But doesnt work as expected.

**Playbook:**

- hosts: pocansibletest  
connection: local  
become: false  
remote\_user: admin  
tasks:

- name: Set VM\_Name as fact  
set\_fact:  
vm\_name: “{{ inventory\_hostname }}”

- name: Test host name without delegation  
shell: hostname  
register: name\_without\_del

- name: Debug host name without delegation  
debug:  
msg: “Debug host name without delegation - {{ name\_without\_del.stdout }}”

- name: Test host name with delegation  
delegate\_to: “{{vm\_name}}”  
shell: hostname  
register: name\_with\_del

- name: Debug host name with delegation  
debug:  
msg: “Debug host name with delegation - {{ name\_with\_del.stdout }}”

Thanks & Regards,  
Vichu

---

<div class="post-metadata">

**Author:** ![Vichu](https://avatars.discourse-cdn.com/v4/letter/v/13edae/32.png) [@Vichu](https://forum.ansible.com/u/Vichu)\
**Post date:** [September 30, 2022, 7:15pm UTC](https://forum.ansible.com/t/not-working-together-connection-local-and-delegate-to/37160/2 "2022-09-30T19:15:37Z")

</div>

Below is the ansible version am using and giving out issue

ansible [core 2.13.4]  
config file = /etc/ansible/ansible.cfg  
configured module search path = [‘/home/rundeck/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]  
ansible python module location = /usr/local/lib/python3.8/dist-packages/ansible  
ansible collection location = /home/rundeck/.ansible/collections:/usr/share/ansible/collections  
executable location = /usr/local/bin/ansible  
python version = 3.8.10 (default, Jun 22 2022, 20:18:18) [GCC 9.4.0]  
jinja version = 3.1.2  
libyaml = True

Also to confirm the playbook gives appropriate hostnames on **ansible v2.7**  
ansible 2.7.18.post0  
config file = /etc/ansible/ansible.cfg  
configured module search path = [u’/root/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]  
ansible python module location = /usr/lib/python2.7/site-packages/ansible  
executable location = /usr/bin/ansible  
python version = 2.7.5 (default, Nov 16 2020, 22:23:17) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]

Thanks & Regards,  
Vichu

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [September 30, 2022, 7:19pm UTC](https://forum.ansible.com/t/not-working-together-connection-local-and-delegate-to/37160/3 "2022-09-30T19:19:46Z")

</div>

> > - hosts: pocansibletest

This absolutely IS your inventory host. delegate\_to and connection: local only tell ansible where to run. They do not affect the “inventory”.

Walter

---

<div class="post-metadata">

**Author:** ![Vichu](https://avatars.discourse-cdn.com/v4/letter/v/13edae/32.png) [@Vichu](https://forum.ansible.com/u/Vichu)\
**Post date:** [September 30, 2022, 7:34pm UTC](https://forum.ansible.com/t/not-working-together-connection-local-and-delegate-to/37160/4 "2022-09-30T19:34:10Z")

</div>

Yes your are right but the below task does not give the hostname of “pocansibletest” and gives the hostname of the localhost itself in ansible v6. But the same works perfectly on ansible v2.7 without any changes to playbook.

- name: Test host name with delegation  
delegate\_to: “{{vm\_name}}”  
shell: hostname  
register: name\_with\_del

The requirement for me is to run many tasks on localhost and delegate some tasks to remote hosts using delegate\_to directive. But with ansible v6 its always executing on localhost and I see inappropriate behaviour and failures.

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [September 30, 2022, 7:42pm UTC](https://forum.ansible.com/t/not-working-together-connection-local-and-delegate-to/37160/5 "2022-09-30T19:42:30Z")

</div>

You set\_fact vm\_name to inventory\_hostname in the first task. This assigns “pocansibletest” to vm\_name. If you then delegate\_to vm\_name you are delegating to “pocansibletest”.

The connection: local doesn’t factor into this. All tasks are still operating on the inventory “pocansibletest”.

Walter

---

<div class="post-metadata">

**Author:** ![Vichu](https://avatars.discourse-cdn.com/v4/letter/v/13edae/32.png) [@Vichu](https://forum.ansible.com/u/Vichu)\
**Post date:** [September 30, 2022, 8:01pm UTC](https://forum.ansible.com/t/not-working-together-connection-local-and-delegate-to/37160/6 "2022-09-30T20:01:20Z")

</div>

Yes you are right, thats my understanding, but what output I get is the hostname where the ansible playbook has triggered. So its not working as expected with Ansible V6

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [October 3, 2022, 11:23am UTC](https://forum.ansible.com/t/not-working-together-connection-local-and-delegate-to/37160/7 "2022-10-03T11:23:03Z")

</div>

Please provide the playbook AND the actual output from running the playbook so we can see which task produces which output.

Walter
