An Ansible role to set a host's hostname and manage it's aliases along with additional host entries.
You should override this spec at the host_vars level. These settings apply to a specific host, and the hostname will be set with the Ansible hostname module based on the hosts_hostname.hostname variable. /etc/hosts will update the loopback IPv4 and IPv6 entries with the appropriate hostname and FQDN from this spec. Additional IPs can be added with add_ip
and will add the aliases specified under hosts_hostname.alias
to those additional host entries. If you don't want the same aliases applied to the additional IPs, you would have to use the hosts_host spec below to achieve that rather than hosts_hostname.
`hosts_hostname`:
`hostname`: ''
`domain`: ''
`alias`: [] #Optional - list of other names you'd like to able to resolve to this IP
`add_ip`: [] #Optional - list of additional ips the host you're configuring might have. This will add all of your listed aliases to the host entry
`alias_loopback`: False #Required; Whether or not to add the host's entries to the loopback ip (e.g. 127.0.0.1 or ::1)
This spec is for additional hosts you would like defined in your host files and could be used at the group_vars level. The variables available are quite similar to those above with the exception of comment
which can add a comment above a host entry.
`hosts_host`:
- `name`: '' #a name for this host entry i.e. 'my db server'
`hostname`: '' #short hostname i.e. not fqdn
`domain`: '' #dns domain of host
`ip_addr`: ''
`comment`: '' #Optional - if you'd like a comment in your host file above this host entry
`alias`: [] #Optional - list of other names you'd like to be able to resolve to this IP
/etc/ansible/host_vars/myserver1/hosts.yml:
---
hosts_hostname:
hostname: myserver1
domain: foo.bar.com
alias:
- webserver1.foo.bar.com
- webserver1
- www.foo.bar.com
- www
- db1.foo.bar.com
- db1
add_ip:
- 192.168.122.50
- 192.168.122.51
- 192.168.122.52
alias_loopback: True
/etc/ansible/group_vars/servers/hosts.yml:
---
hosts_host:
- name: Some Other Webserver
hostname: myserver2
domain: foo.bar.com
ip_addr: 192.168.122.60
comment: 'The other webserver that does stuff'
alias:
- intranet
- intranet.foo.bar.com
- helpdesk
- helpdesk.foo.bar.com
- name: Another App server
hostname: appserver2
domain: foo.bar.com
ip_addr: 192.168.122.70
comment: 'App server 1'
alias: []
This will set server1's hostname and configure it's hostfile with the variables passed from host_vars and group_vars
- hosts: server1
roles:
- ahuffman.hosts
The /etc/hosts file produced by the above example would look like:
# Ansible managed: /etc/ansible/roles/hosts/templates/hosts.j2 modified on .......
127.0.0.1 myserver1.foo.bar.com myserver1 localhost localhost.localdomain localhost4 localhost4.localdomain4 webserver1.foo.bar.com webserver1 www.foo.bar.com www db1.foo.bar.com db1
::1 myserver1.foo.bar.com myserver1 localhost localhost.localdomain localhost6 localhost6.localdomain6 webserver1.foo.bar.com webserver1 www.foo.bar.com www db1.foo.bar.com db1
192.168.122.50 myserver1.foo.bar.com myserver1 localhost localhost.localdomain webserver1.foo.bar.com webserver1 www.foo.bar.com www db1.foo.bar.com db1
192.168.122.51 myserver1.foo.bar.com myserver1 localhost localhost.localdomain webserver1.foo.bar.com webserver1 www.foo.bar.com www db1.foo.bar.com db1
192.168.122.52 myserver1.foo.bar.com myserver1 localhost localhost.localdomain webserver1.foo.bar.com webserver1 www.foo.bar.com www db1.foo.bar.com db1
# The other webserver that does stuff
192.168.122.60 myserver2.foo.bar.com myserver2 intranet intranet.foo.bar.com helpdesk helpdesk.foo.bar.com
# App server 1
192.168.122.70 appserver2.foo.bar.com appserver2