Ansible hostname alias not in setup

4,783 views
Skip to first unread message

ProfHase

unread,
Apr 14, 2014, 4:14:55 AM4/14/14
to ansible...@googlegroups.com
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 :)

Dick Davies

unread,
Apr 15, 2014, 5:48:43 AM4/15/14
to ansible...@googlegroups.com
Try inventory_hostname .
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/9cff5163-dd72-4285-8465-a69146ee048e%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

ProfHase

unread,
Apr 17, 2014, 9:33:05 AM4/17/14
to ansible...@googlegroups.com, di...@hellooperator.net

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

Brian Coca

unread,
Apr 17, 2014, 9:39:28 AM4/17/14
to ansible...@googlegroups.com
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>]

Tomasz Klosinski

unread,
Aug 22, 2016, 4:43:22 AM8/22/16
to Ansible Project
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

Kai Stian Olstad

unread,
Aug 22, 2016, 6:10:26 AM8/22/16
to ansible...@googlegroups.com
On 22. aug. 2016 10:43, Tomasz Klosinski wrote:
> 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.

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

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages