Search code examples
ansibleinventory

Ansible > alias to host in inventory


I have an inventory which, for the purposes of testing, uses the same host for all groups:

[webserver]
127.0.0.1 ansible_user=root ansible_ssh_pass=somepassword

[dbserver]
127.0.0.1 ansible_user=root ansible_ssh_pass=somepassword

[storageserver]
127.0.0.1 ansible_user=root ansible_ssh_pass=somepassword

Is it possible to alias this host?

If I use this:

[localvm]
127.0.0.1 ansible_user=root ansible_ssh_pass=somepassword

[webserver]
localvm   

[dbserver]
localvm   

[storageserver]
localvm   

I get fatal: [localvm]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname localvm: Name or service not known\r\n", "unreachable": true}.

This works:

[localvm]
127.0.0.1 ansible_user=root ansible_ssh_pass=somepassword

[webserver:children]
localvm   

[dbserver:children]
localvm   

[storageserver:children]
localvm   

i.e. I'm specifying localvm as a group (http://docs.ansible.com/ansible/latest/intro_inventory.html ) however it's not what I'm wanting to do. I want to create an alias for 127.0.0.1 called localvm.

How do I do that?


Solution

  • If you want inventory name for the host to be different to actual host name, you can use ansible_host variable. See List of Behavioral Inventory Parameters.

    Like this:

    [webserver]
    localvm ansible_host=127.0.0.1 ansible_user=root ansible_ssh_pass=somepassword
    
    [dbserver]
    localvm
    
    [storageserver]
    localvm
    

    I often define hosts as ungrouped entries and then add them to required groups, like this:

    www1 ansible_host=10.10.10.10
    db1 ansible_host=10.10.10.20
    
    [web]
    www1
    
    [database]
    db1