# Problem with computed ansible\_host and delegate\_to

**URL:** <https://forum.ansible.com/t/problem-with-computed-ansible-host-and-delegate-to/45535>\
**Category:** Get Help\
**Created:** [March 16, 2026, 1:00pm UTC](https://forum.ansible.com/t/problem-with-computed-ansible-host-and-delegate-to/45535 "2026-03-16T13:00:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwilck](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mwilck/32/10470_2.png) [@mwilck](https://forum.ansible.com/u/mwilck)\
**Post date:** [March 16, 2026, 1:00pm UTC](https://forum.ansible.com/t/problem-with-computed-ansible-host-and-delegate-to/45535/1 "2026-03-16T13:00:36Z")

</div>

I recently added a table of host names mapped to IP addresses to my environment, because I needed this anyway to configure my DHCP server. I thought it was a nice idea to use this table as the Source of Truth™ for my playbooks. I introduced a variable expression for `ansible_host` which would look up the IP address in this table. This seemed to work nicely … until I started using `delegate_to`.

Here’s a test:

### inventory.yml

```auto
all:
  hosts:
    zeus:
      # ansible_host: 192.168.1.20
      ansible_ssh_user: martin
    ananas:
      # ansible_host: 192.168.1.167
      ansible_ssh_user: p464v2
  vars:
    ip_addrs:
      zeus: 192.168.1.20
      ananas: 192.168.1.167
    ansible_host: "{{ ip_addrs[inventory_hostname] }}"

```

### delegate-test.yml

```auto
- name: Test delegate_to with computed ansible_host
  hosts: zeus,ananas
  gather_facts: true
  become: false
  tasks:

    - name: Print host and user
      debug:
        msg: "Host: {{ ansible_host }}, User: {{ ansible_ssh_user }}"

    - name: Print host and user with delegate_to
      debug:
        msg: "Host: {{ ansible_host }}, User: {{ ansible_ssh_user }}"
      delegate_to: ananas
      when: inventory_hostname == 'zeus'

    - name: Check connectivity
      ping:

    - name: Check connectivity with delegate_to
      ping:
      delegate_to: ananas
      when: inventory_hostname == 'zeus'

```

The output of this playbook is:

```auto
> ansible-playbook -i inventory.yml delegate-test.yml

PLAY [Test delegate] ********************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************
ok: [zeus]
ok: [ananas]

TASK [Print host and user] **************************************************************************************************
ok: [zeus] => {
    "msg": "Host: 192.168.1.20, User: martin"
}
ok: [ananas] => {
    "msg": "Host: 192.168.1.167, User: p464v2"
}

TASK [Print host and user with delegate_to] *********************************************************************************
ok: [zeus -> ananas({{ ip_addrs[inventory_hostname] }})] => {
    "msg": "Host: 192.168.1.20, User: martin"
}
skipping: [ananas]

TASK [Check connectivity] ***************************************************************************************************
ok: [zeus]
ok: [ananas]

TASK [Check connectivity with delegate_to] **********************************************************************************
skipping: [ananas]
fatal: [zeus -> ananas]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Received disconnect from 192.168.1.20 port 22:2: Too many authentication failures\r\nDisconnected from 192.168.1.20 port 22", "unreachable": true}

PLAY RECAP ******************************************************************************************************************
ananas : ok=3 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0   
zeus : ok=4 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0   

```

In the last task with `delegate_to`, ansible is using the IP address of host `zeus` when connecting to `ananas`.

If I uncomment the constant expressions for `ansible_host` in the `inventory.yml` file, the playbook succeeds.

Is this a bug? Or is it generally not supported to use a variable for `ansible_host`?

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [March 16, 2026, 4:08pm UTC](https://forum.ansible.com/t/problem-with-computed-ansible-host-and-delegate-to/45535/2 "2026-03-16T16:08:42Z")

</div>

`inventory_hostname` ALWAYS reflects the task’s original host, the next best thing is `inventory_hostname_short`, which does reflect the ‘delegated host’.

---

<div class="post-metadata">

**Author:** ![mwilck](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mwilck/32/10470_2.png) [@mwilck](https://forum.ansible.com/u/mwilck)\
**Post date:** [March 16, 2026, 4:38pm UTC](https://forum.ansible.com/t/problem-with-computed-ansible-host-and-delegate-to/45535/3 "2026-03-16T16:38:37Z")

</div>

Wow, and that’s even documented. Thanks a lot!!

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [March 16, 2026, 5:18pm UTC](https://forum.ansible.com/t/problem-with-computed-ansible-host-and-delegate-to/45535/4 "2026-03-16T17:18:14Z")

</div>

Yep, I had to write it down, cause asking me does not scale.

---

<div class="post-metadata">

**Author:** ![mwilck](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mwilck/32/10470_2.png) [@mwilck](https://forum.ansible.com/u/mwilck)\
**Post date:** [March 16, 2026, 6:02pm UTC](https://forum.ansible.com/t/problem-with-computed-ansible-host-and-delegate-to/45535/5 "2026-03-16T18:02:49Z")

</div>

Sorry for not having found it in the docs, and thanks again.

---

<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:** [April 15, 2026, 6:03pm UTC](https://forum.ansible.com/t/problem-with-computed-ansible-host-and-delegate-to/45535/6 "2026-04-15T18:03:38Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
