Ansible hostname alias not in setup

Hi, having the following configuration

[my_hosts]
my_host ansible_ssh_host=123.123.123.123
my_host2 ansible_ssh_host=234.234.234.234

I want to gather some debug information in my templates.

  • How do I acces the alias variable in a playbook/template? ie:debug:
    msg=Myhost is {{ ansible_host_alias }}

    Myhost is my_host

    Myhost is myhost2

I tried to gather facts using ansible -m setup my_host . The variables ansible_hostname , HOSTNAME and HOST contain the machine’s hostname, i.e. echo $HOSTNAME which differs from my ansible alias.

- Is there really no such builtin variable?

- Would a feature request make sense, or should I better try to keep all my aliases in ssh_config?

Thanks :slight_smile:

Try inventory_hostname .

Thanks, that’s it. How did you find out? How do I find variables NOT defined in setup module?

inventory_hostname is a ‘magic’ variable and always present, its not part of fact gathering but inventory definition.

to see if a variable exists for a host you can do:

‘varname’ in hostvars[<host’s inventory name>]

Hello,

Is it possible to access {{ ansible_ssh_host }} variable from inventory in the above example? I need ip addresses to provision VMs before I run Ansible on them.
Thanks in advance,

Tomasz Klosinski

Yes, but ansible_ssh_host is deprecated in version 2.x so use ansible_host instead.