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 + +## }}}