Ansible - Define hostname aliases in inventory using ansible_host

Let's say you have servers defined in your default hosts file or your own inventory file, in an INI file such as hosts.ini.

[linux]
server1.example.com
server2.example.com
server3.example.com
[windows]
server4.example.com
server5.example.com
server6.example.com

 

Or in a YAML file, such as hosts.yaml.

linux:
  hosts:
    server1.example.com:
    server2.example.com:
    server3.example.com:
windows:
  hosts:
    server4.example.com:
    server5.example.com:
    server6.example.com:

 

ansible_host can be used to create hostname aliases, such as alias "server1" for "server1.example.com". Here is how you would create hostname aliases in an INI file.

[linux]
server1 ansible_host=server1.example.com
server2 ansible_host=10.17.4.56
server3 ansible_host=server3.example.com
[windows]
server4 ansible_host=server4.example.com
server5 ansible_host=server5.example.com
server6 ansible_host=10.17.4.60

 

And here is how you would create hostname aliases in a YAML file.

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

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |