From 35b0384e476ecb624a15bace26464fef1e517e76 Mon Sep 17 00:00:00 2001 From: Aaron Guise Date: Thu, 18 Aug 2016 22:51:56 +1200 Subject: [PATCH] Attempt to make the Hosts file setup non-breaking. e.g not wipe out custom entries --- tasks/RedHat.yml | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/tasks/RedHat.yml b/tasks/RedHat.yml index 6ced997..d9069d2 100644 --- a/tasks/RedHat.yml +++ b/tasks/RedHat.yml @@ -54,7 +54,7 @@ enabled: yes tags: ntp -- name: Ensure Hostname/DNS common config +- name: Ensure DNS and SSH common config template: src: "{{ item.src }}" dest: "{{ item.dest }}" @@ -64,10 +64,10 @@ dest: /etc/sysconfig/network, mode: o+rw } - - { src: etc.hosts.j2, - dest: /etc/hosts, - mode: o=rw,g=r,a=r - } + #- { src: etc.hosts.j2, + # dest: /etc/hosts, + # mode: o=rw,g=r,a=r + # } - { src: etc.resolv.conf.j2, dest: /etc/resolv.conf, mode: o=rw,g=r,a=r @@ -80,7 +80,23 @@ - Restart NTPD - Restart SSH tags: dns - + +- name: Ensure hosts file correct + lineinfile: + dest: /etc/hosts + regexp: "^127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4" + line: "127.0.0.1 localhost {{ ansible_hostname }}" + with_items: + - {regexp: "^127.0.0.1 localhost.+", + line: "127.0.0.1 localhost {{ ansible_hostname }}" + } + - {regexp: "^::1.+", + line: "::1 localhost {{ ansible_hostname }}" + } + - {regexp: "^{{ ansible_default_ipv4.address }}.+{{ ansible_default_ipv4.address.split('.')[-1] }}$", + line: "{{ ansible_default_ipv4.address }} {{ ansible_hostname }}.{{ domain }} {{ ansible_hostname }}" + } + - name: Ensure correct permissions (hosts/resolv.conf) file: path: "{{item}}"