Populate /etc/hosts with the primary IPv4 addresses or specified addresses of an inventory group or a list of names and addresses. This will overwrite existing entries for hosts in the specified group. In the event of an IP address left without a hostname it will be commented out.
Either or both:
- An inventory group with one or more hosts. The hosts don't need an
ansible_default_ipv4.addressdefined ifetc_hosts_addressis set. One of those values is required to create an entry in/etc/hosts. - A list of names and addresses.
etc_hosts_group- This is used to select the group to load into/etc/hosts. Defaults toall.etc_hosts_path- The path to the hosts file. Defaults to/etc/hostsetc_hosts_localhost_ipv4_address- The IP address to use in the hosts file for the current node. Defaults to127.0.0.11.etc_hosts_suffix- The suffix to add to the hostname (eg.local). Defaults to nothing (an empty string).etc_hosts_orphaned_message- The message to leave in the comment for orphaned IP addresses. DefaultsOrphaned by Ansible {{ ansible_date_time.iso8601 }}.etc_hosts_list- A list ofdicts withnameandaddresskeys. Defaults to an empty list.etc_hosts_group_list- The same asetc_hosts_listbut for use in group_vars. Defaults to an empty list.etc_hosts_host_list- The same asetc_hosts_listbut for use in host_vars. Defaults to an empty list.
None.
Populate the /etc/hosts file on each machine in group servers with all the IP and hostname pairs of all the machines in group servers.
- hosts: servers
vars:
etc_hosts_group: servers
roles:
- haxwithaxe.etc_hostsPopulate the /etc/hosts file on each host with all the IP and hostname pairs given in etc_hosts_list.
- hosts: all
vars:
etc_hosts_list:
- name: foo
address: 1.2.3.4
- name: bar
address: 2.3.4.5
roles:
- haxwithaxe.etc_hostsPopulate the /etc/hosts file on each machine in group servers with the IP and hostname with suffix pairs of all the machines in group servers.
- hosts: servers
vars:
etc_hosts_group: servers
etc_hosts_suffix: .foo
roles:
- haxwithaxe.etc_hostsAdd the IP addresses of the myhosts group to the local /etc/hosts. Facts need to be gathered for hosts not in the group being modified by etc_hosts.
- name: Gather facts for the group you need IPs from (etc_hosts_group)
hosts: myhosts
tasks:
- name: Add hosts file entries for hosts in mygroup
hosts: localhost
vars:
etc_hosts_group: myhosts
roles:
- haxwithaxe.etc_hostsTo set an IP for a host set etc_hosts_address for the host and that address will be used in other hosts' /etc/hosts. For example:
host_vars/alice.yml
etc_hosts_address: 1.2.3.4GPLv3
Created by haxwithaxe