How to append to existing /etc/hosts files on over 100 servers

Hi,
I am new to Ansible and am looking for a way to push out changes to over 100 servers /etc/hosts file. We don’t want to overwrite the existing one because we lose the top 3 default lines like this on over 100 machines:

Do not remove the following line, or various programs

that require network functionality will fail.

127.0.0.1 ansible localhost.localdomain localhost
#::1 localhost6.localdomain6 localhost6

Is there a way to preserve these lines or possibly rewrite them. All we need is the actual hostname of each of the 100 machines to be preserved or rewritten. In this case “ansible” is the hostname. I have played around with the assemble module and am able to use the existing hosts file for all 100 machines and the new hosts in a separate file to merge them together, but the problems is the line “127.0.0.1 ansible localhost.localdomain localhost” gets updated to the same line on each machine. We need it to be different for each host with the corresponding hostname.

Any help/input is greatly appreciated. I am very excited about the power of this tool, but relatively new to it.

Thanks in advance.

Josh

The “lineinfile” module is available for this purpose, however I much prefer the template module.

You can use “magic” variables like {{ inventory_hostname }} and {{ inventory_hostname_short }} in templates, or also use the hostname fact derived from the remote system.

–Michael