Hostname to ip mapping

I want to create similar mapping as in /etc/hosts , but local to use in Ansible, so that all the hostnames are independent of DNS or /etc/hosts and can be stored directly inside VCS.

The motivation for this, usage of hostnames inside playbooks and cli, but using IPs(only if defined) for connections.

Currently I’m using kludgy inventory group for each host like this:

[host1]
192.168.0.1

[host2]
192.168.0.2:4400 host_var=hello

[host3]
192.168.0.3 other_var=world

[host_group:children]
host1
host2
host3

I wonder, it there a better way to do this?

It would be nice to be able to do this:

[host_group]

host1=192.168.0.1
host2=192.168.0.2:4400 host_var=hello
host3=192.168.0.3 other_var=world

If IP is assigned to host, then it’s made available through ‘inventory_ip’, similar to ‘inventory_hostname’. And Ansible should use the defined IP internally for creating connections to hosts.

Regards,
–janis

Not *entirely* sure I understand the use case, but if you want to
connect by IP, I still find it useful to have real names for hosts.

This works in 0.8 and would contact machines by IP but still allow you
to refer to them by name.

[webservers]
whatever_name ansible_ssh_host=192.168.10.50

You may also wish to consider writing your own external inventory
script to provide your ideal way to organize your host configurations
-- it's pretty easy to do and there are several examples in the
plugins directory for doing this.

If you set the variables ansible_ssh_host and ansible_ssh_user on
particular hosts, in an external script, you can control how each host
alias is connected to.

--Michael

ansible_ssh_host is not working as expected, I filled a ticket about this.