# Variables not consumed from delegated host

**URL:** <https://forum.ansible.com/t/variables-not-consumed-from-delegated-host/40621>\
**Category:** Get Help\
**Tags:** ansible-core\
**Created:** [February 25, 2025, 6:41pm UTC](https://forum.ansible.com/t/variables-not-consumed-from-delegated-host/40621 "2025-02-25T18:41:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kristianheljas](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kristianheljas/32/7702_2.png) [@kristianheljas](https://forum.ansible.com/u/kristianheljas)\
**Post date:** [February 25, 2025, 6:41pm UTC](https://forum.ansible.com/t/variables-not-consumed-from-delegated-host/40621/1 "2025-02-25T18:41:35Z")

</div>

In [delegation docs](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_delegation.html#templating-in-delegation-context) it’s stated that:

> All variables except `inventory_hostname` will now be consumed from this host and not the original task host.

However, this playbook seems to still use variables from original host as illustrated below.

```yaml
- hosts: all
  become: false
  gather_facts: true
  tasks:

    - ansible.builtin.shell:
        cmd: echo "{{ ansible_hostname }}"
      delegate_to: localhost
      register: local_hostname

    - ansible.builtin.debug:
        msg: "{{ local_hostname.stdout }}"

```

```plaintext
TASK [ansible.builtin.shell] *****************************
changed: [webserver-1 -> localhost]
changed: [webserver-2 -> localhost]
changed: [db -> localhost]

TASK [ansible.builtin.debug] *****************************
ok: [webserver-1] => {
    "msg": "webserver-1"
}
ok: [webserver-2] => {
    "msg": "webserver-2"
}
ok: [db] => {
    "msg": "db"
}

```

Am I misunderstanding the documentation or is there docs/ansible issue?

---

<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:** [February 25, 2025, 6:45pm UTC](https://forum.ansible.com/t/variables-not-consumed-from-delegated-host/40621/2 "2025-02-25T18:45:44Z")

</div>

It looks like a documentation issue that needs to be resolved.

delegation really only impacts connection related variables.

The task definition itself is templated under the actual host, but connection related vars are pulled from the delegated host.

---

<div class="post-metadata">

**Author:** ![kristianheljas](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kristianheljas/32/7702_2.png) [@kristianheljas](https://forum.ansible.com/u/kristianheljas)\
**Post date:** [February 26, 2025, 12:32pm UTC](https://forum.ansible.com/t/variables-not-consumed-from-delegated-host/40621/3 "2025-02-26T12:32:44Z")

</div>

Digged a bit deeper and this is actually partially true.

When task executor [resolves connection options](https://github.com/ansible/ansible/blob/59d9737788d63ea4e17b74d3c3ca1a0cf03f89ba/lib/ansible/executor/task_executor.py#L620) it templates them using delegated variables (`cvars` and `templar` both contain delegated variables)

Few lines below, [original variables are restored](https://github.com/ansible/ansible/blob/59d9737788d63ea4e17b74d3c3ca1a0cf03f89ba/lib/ansible/executor/task_executor.py#L621-L624) and it will only update connection variables that are not set , due to check in [ConnectionBase.update\_vars()](https://github.com/ansible/ansible/blob/59d9737788d63ea4e17b74d3c3ca1a0cf03f89ba/lib/ansible/plugins/connection/ __init__.py#L256-L258). These variables are listed in [COMMON\_CONNECTION\_VARS](https://github.com/ansible/ansible/blob/a0495fc31497798a7a833ba7406a9729e1528dd8/lib/ansible/constants.py#L201-L203)

So given following inventory:

```auto
db ansible_host=127.0.0.1 ansible_port=2201 ansible_user='vagrant' ansible_ssh_private_key_file='/home/kristian/projects/ansible-playground/.vagrant/machines/db/virtualbox/private_key'
webserver-1 ansible_host=127.0.0.1 ansible_port=2222 ansible_user='vagrant' ansible_ssh_private_key_file='/home/kristian/projects/ansible-playground/.vagrant/machines/webserver-1/virtualbox/private_key'
webserver-2 ansible_host=127.0.0.1 ansible_port=2200 ansible_user='vagrant' ansible_ssh_private_key_file='/home/kristian/projects/ansible-playground/.vagrant/machines/webserver-2/virtualbox/private_key'

```

- `ansible_port` will remain as-is, since it’s already defined
- `ansible_private_key_file` gets set from delegated host, since it’s set as `ansible_ssh_private_key_file` alias in the inventory.

Will see a bit later if i can come up with some concise description of this to update the docs.

---

<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:** [March 28, 2025, 12:33pm UTC](https://forum.ansible.com/t/variables-not-consumed-from-delegated-host/40621/4 "2025-03-28T12:33:35Z")

</div>

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