Hello,
I am quite new to Ansible, hence knowledge might be a bit rough on the edges. As follows is a simple reproduction example.
inventory.yml:
# Inventory has inlined variables for ease of use.
# In practice these are defined within host_vars.
all:
hosts:
localhost:
ansible_host: 127.0.0.1
ansible_connection: local
myhost:
ansible_host: 192.168.100.186
main.yml:
- name: Example play
gather_facts: false
hosts: myhost
tasks:
- ansible.builtin.shell:
cmd: >-
echo "Delegated from {{ inventory_hostname }} to {{ ansible_host }},"
"target hostname is $(hostnamectl hostname)"
delegate_to: localhost
register: result
- ansible.builtin.debug:
msg: "{{ result.stdout }}"
$ ansible-playbook -i inventory.yml main.yml
PLAY [Example play] ************************************************************************************************************************************************************************
TASK [ansible.builtin.shell] ***************************************************************************************************************************************************************
changed: [myhost -> localhost(127.0.0.1)]
TASK [ansible.builtin.debug] ***************************************************************************************************************************************************************
ok: [myhost] => {
"msg": "Delegated from myhost to 192.168.100.186, target hostname is test-ansible-controller"
}
Expected: {{ ansible_host }} is 127.0.0.1.
Actual: {{ ansible_host }} is 192.168.100.186 , i.e. inventory_hostname.
Having read https://docs.ansible.com/projects/ansible/latest/playbook_guide/playbooks_delegation.html#templating-in-delegation-context , this was surprising to me:
The ansible_host variable and other connection variables, if present, reflects information about the host a task is delegated to, not the inventory_hostname.
All variables except inventory_hostname will now be consumed from this host and not the original task host.
I also encountered ansible_host is incorrect when using delegate_to · Issue #14958 · ansible/ansible · GitHub (old):
So the confusion here is not about connection but display, for templating ansible_host will reflect inventory_hostname.
, as well as Variables not consumed from delegated host - #2 by sivel :
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.
Do I interpret that correctly, that ansible only uses ansible_host variable to establish connection itself, but not for interpolating in templates of this task with delegate_to? In other words: “connection related variables” != template variables used for interpolation?
I cannot use hostvars[somehost], as somehost is dynamic - localhost and myhost in this example - , task has conditional actions depending on the delegated host, like
temp_copy_directory: >-
{{
lookup('env','HOME')
if ansible_host == 'localhost'
else ansible_user_dir
}}/tmp__ansible
So: Is there any way to refer to current delegated ansible host from within task template ?
Thank you very much.
ansible 2.19.4