# How to access the host alias in the inventory

**URL:** <https://forum.ansible.com/t/how-to-access-the-host-alias-in-the-inventory/39466>\
**Category:** Get Help\
**Tags:** variables, inventory\
**Created:** [January 5, 2025, 9:20pm UTC](https://forum.ansible.com/t/how-to-access-the-host-alias-in-the-inventory/39466 "2025-01-05T21:20:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Recopy9779](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@Recopy9779](https://forum.ansible.com/u/Recopy9779)\
**Post date:** [January 5, 2025, 9:20pm UTC](https://forum.ansible.com/t/how-to-access-the-host-alias-in-the-inventory/39466/1 "2025-01-05T21:20:19Z")

</div>

I have the following inventory:

```auto
[all:vars]
ansible_ssh_extra_args="-o IdentitiesOnly=yes"
ansible_user=root
ansible_ssh_private_key_file=/mnt/c/Users/Recopy9779/.ssh/pubkeys/{{ inventory_hostname }}.pub

[homeserver]
home.server.internal ansible_host=192.168.1.2

```

My issue is: the public keys are stored in files that reference the hostnames, e.g. `/mnt/c/Users/Recopy9779/.ssh/pubkeys/home.server.internal.pub` for `home.server.internal` , but `{{ inventory_hostname }}` resolves to the IP (`192.168.1.2`).  
All documentation I have read seems like `{{ inventory_hostname }}` should resolve to `home.server.internal` in this case.  
What am I doing wrong here? Is there a way to access `home.server.internal` in a variable?

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [January 5, 2025, 11:37pm UTC](https://forum.ansible.com/t/how-to-access-the-host-alias-in-the-inventory/39466/2 "2025-01-05T23:37:45Z")

</div>

Hi [Recopy9779](https://forum.ansible.com/u/Recopy9779). With your inventory file, I cannot reproduce the behavior you are describing. This is what I get in debug output:

```auto
<192.168.1.2> ESTABLISH SSH CONNECTION FOR USER: root
<192.168.1.2> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o 'IdentityFile="/mnt/c/Users/Recopy9779/.ssh/pubkeys/home.server.internal.pub"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="root"' -o ConnectTimeout=10 -o IdentitiesOnly=yes -o 'ControlPath="/home/bvitnik/.ansible/cp/f1b6b591d3"' -tt 192.168.1.2 true

```

As you can see, value for `IdentityFile` SSH option is resolved properly. `{{ inventory_hostname }}` does resolve to a name, not to an IP address. `ansible_host` is holding the IP address.

Something else you are not showing here has led you to the wrong conclusion.

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [January 5, 2025, 11:38pm UTC](https://forum.ansible.com/t/how-to-access-the-host-alias-in-the-inventory/39466/3 "2025-01-05T23:38:50Z")

</div>

P.S. You need a private key for SSH authentication, not a public key 😄

---

<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:** [January 6, 2025, 3:45pm UTC](https://forum.ansible.com/t/how-to-access-the-host-alias-in-the-inventory/39466/4 "2025-01-06T15:45:07Z")

</div>

if you ever plan on using delegation, this might break, use `inventory_hostname_short` instead [https://docs.ansible.com/ansible/latest/reference\_appendices/special\_variables.html#term-inventory\_hostname](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#term-inventory_hostname)

---

<div class="post-metadata">

**Author:** ![Recopy9779](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@Recopy9779](https://forum.ansible.com/u/Recopy9779)\
**Post date:** [January 12, 2025, 9:17pm UTC](https://forum.ansible.com/t/how-to-access-the-host-alias-in-the-inventory/39466/5 "2025-01-12T21:17:36Z")

</div>

Somehow restarting WSL fixed it and I am unable to reproduce it now 🤔  
Thanks  
With regard to delegation , I have no idea what that is but I am just trying to get into the Ansible stuff 😉

---

<div class="post-metadata">

**Author:** ![Recopy9779](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@Recopy9779](https://forum.ansible.com/u/Recopy9779)\
**Post date:** [January 12, 2025, 9:19pm UTC](https://forum.ansible.com/t/how-to-access-the-host-alias-in-the-inventory/39466/6 "2025-01-12T21:19:57Z")

</div>

If you give SSH `IdentitiesOnly` and a public key, it will request that and only that key from the agent. This is super helpfull if you have one key per host, like I do and don’t want to trip `MaxAuthTries`

---

<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:** [February 11, 2025, 9:20pm UTC](https://forum.ansible.com/t/how-to-access-the-host-alias-in-the-inventory/39466/7 "2025-02-11T21:20:25Z")

</div>

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