Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible - Access inventory alias

Tags:

ansible

Having an inventory file like:

[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? I.e.:

    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.

like image 753
ProfHase85 Avatar asked Apr 10 '14 09:04

ProfHase85


People also ask

How do I change my default inventory in Ansible?

The default location for inventory is a file called /etc/ansible/hosts . You can specify a different inventory file at the command line using the -i <path> option.

What is Host_vars and Group_vars in Ansible?

The host_vars is a similar folder to group_vars in the repository structure. It contains data models that apply to individual hosts/devices in the hosts. ini file. Hence, there is a YAML file created per device containing specific information about that device.


1 Answers

The variable I was searching for is a built in feature: inventory_hostname

Ansible documentation about inventory_hostname and inventory_hostname_short is found from chapter Magic Variables, and How To Access Information About Other Hosts.

Original question: https://groups.google.com/forum/#!topic/ansible-project/Oa5YXjHecIw

like image 53
ProfHase85 Avatar answered Sep 25 '22 16:09

ProfHase85