Bootstrap FreeKB - Ansible - Define hostname aliases in inventory using ansible_host
Ansible - Define hostname aliases in inventory using ansible_host

Updated:   |  Ansible articles

Tasks are run against target servers. Some Ansible documentation refers to the target servers as "hosts".

 

After a clean install of Ansible, the "inventory" directive in ansible.cfg is commented out, like this.

#inventory = /path/to/hosts

 

In this scenario, the default hosts file is /etc/ansible/hosts and the default hosts file is completely commented out. If you were to issue command ansible all -m ping, the following would be displayed. Likewise, if you were to uncomment the "inventory" directive in ansible.cfg without defining your inventory, the following would be displayed.

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

 

Typically, target servers are defined in the default hosts file or your own inventory file. Sometimes, the "inventory" directive in ansible.cfg is uncommented and updated to point to the directory where the default hosts file or your own inventory file will be located.

Additionally, Ansible uses inventory plugins to parse inventory. For example, ansible.cfg may have the following.

[inventory]
enable_plugins = host_list, yaml, ini

 

For example, the yaml inventory_plugin allows you to define target servers in a YAML default hosts file or your own inventory file. For example, let's say you have an default hosts file or your own inventory file named inventory.yml that contains target systems, perhaps something like this.

ansible_host can be used to create hostname aliases, such as alias "server1" for "server1.example.com".

linux:
  hosts:
    server1:
      ansible_host: server1.example.com
    server2:
      ansisle_host: 10.17.4.56
    server3:
      ansible_host: server3.example.com
windows:
  hosts:
    server4:
      ansible_host: server4.example.com
    server5:
      ansible_host: server5.example.com
    server6:
      ansible_host: 10.17.4.60

 

Here is how you can run the example.yml playbook using the target systems specified in inventory.yml.

ansible-playbook example.yml --inventory /path/to/inventory.yml

 

inventory_hostname should contain the hostname alias and ansible_host should contain whatever you have following ansible_host in your inventory file.

---
- hosts: all
  tasks:
  - debug:
      msg: inventory_hostname = {{ inventory_hostname }}

  - debug:
      msg: ansible_host = {{ ansible_host }}
...

 

Running this playbook should return something like this.

TASK [debug]
ok: [server1] => {
    "msg: "inventory_hostname = server1"
}
ok: [server2] => {
    "msg: "inventory_hostname = server2"
}
ok: [server3] => {
    "msg: "inventory_hostname = server3"
}

ok: [server1] => {
    "msg": "ansible_host = server1.example.com"
}
ok: [server2] => {
    "msg": "ansible_host = 10.17.4.56"
}
ok: [server3] => {
    "msg": "ansible_host = server3.example.com"
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 9c5aa4 in the box below so that we can be sure you are a human.