From d8a3e11a9b3ff0a881e5ef3487ede8e3d80077f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gardais=20J=C3=A9r=C3=A9my?= Date: Fri, 12 Jan 2018 15:54:47 +0100 Subject: [PATCH] =?UTF-8?q?Purge=20/etc/hosts=20of=20multiple=20lines=20th?= =?UTF-8?q?at=20contains=20$HOSTNAME=20without=20the=20default=20ipv4=20ip?= =?UTF-8?q?=20address.=20See=E2=80=AF:=20https://bugs.debian.org/cgi-bin/b?= =?UTF-8?q?ugreport.cgi=3Fbug=3D316099?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + README.md | 4 +++- tasks/main.yml | 13 +++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c51bd9..8de1311 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Enhancements * Add example playbook. * localhost line sould not contain hostname information (see https://www.debian.org/doc/manuals/debian-reference/ch05.en.html#_the_hostname_resolution). +* Purge /etc/hosts of multiple lines that contains $HOSTNAME without the default ipv4 ip address. ## v1.0 diff --git a/README.md b/README.md index f3dc420..62e1fa7 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,9 @@ Manage some basics configuration for IPR's servers. ### Hosts - Ensure to have the correct fqdn and hostname in /etc/hosts. - You can define the domain if it's not correct on the remote host. - - You can choose to define the localhost (127.0.0.1) line or/and the ipv4 (lan) line. + - You can choose to define the localhost (127.0.0.1) line content. + - You can choose to define the permanent ip (127.0.0.1) line content. + - All other lines that contains hostname without this permanent ip will be removed. ## Development diff --git a/tasks/main.yml b/tasks/main.yml index 8de9640..6cb4092 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,6 +1,8 @@ --- # tasks file for basics +## Modify /etc/hosts + - name: Set 127.0.0.1 line in hosts file lineinfile: dest: /etc/hosts @@ -20,3 +22,14 @@ backup: yes when: (basics__hosts_ipv4_manage and ansible_default_ipv4.address is defined) + register: basics__register_ipv4_line + +- name: Purge other lines with hostname + lineinfile: + dest: /etc/hosts + state: absent + regexp: '^(?!({{ ansible_default_ipv4.address }})).*{{ ansible_hostname }}' + backup: yes + when: basics__register_ipv4_line.changed + +## }}}