From 96b6e8dc2d2e4b89c98ba16353ab0d37b2e84d42 Mon Sep 17 00:00:00 2001 From: Charlie Mordant Date: Fri, 18 Aug 2023 14:48:11 +0200 Subject: [PATCH] remove molecule entries that are not supported anymore --- README.md | 2 +- galaxy.yml | 2 +- molecule/default/molecule.yml | 2 -- molecule/parallels/converge.yml | 2 ++ pyproject.toml | 2 +- tasks/host.yml | 43 +++++++++++++++++++++++---------- tasks/host_entries.yml | 1 + 7 files changed, 36 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 2552583..8d56e6a 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Take a look at the [molecule tests](./molecule/default/converge.yml) tests and t Dependencies ------------ -[ansible.freeipa collection](https://github.com/freeipa/ansible-freeipa) +See [requirements collections](./requirements-collections.yml) and [requirements standalone](./requirements-standalone.yml) License ------- diff --git a/galaxy.yml b/galaxy.yml index 8e0b9c1..cea3b81 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -1,7 +1,7 @@ --- namespace: "tcharl" name: "etchost_append" -version: "1.0.1" +version: "2.0.1" description: "Appends entries in etc/hosts file" authors: diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 4106cdf..46535b1 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -3,9 +3,7 @@ dependency: name: galaxy options: role-file: requirements.yml - roles-path: ${MOLECULE_PROJECT_DIRECTORY}/../community requirements-file: requirements-collections.yml - collections-path: ${MOLECULE_PROJECT_DIRECTORY}/../community-collections driver: name: vagrant provider: diff --git a/molecule/parallels/converge.yml b/molecule/parallels/converge.yml index 03b1e9a..1251a1b 100644 --- a/molecule/parallels/converge.yml +++ b/molecule/parallels/converge.yml @@ -5,8 +5,10 @@ hosts_entries: - name: idm.osgiliath.test ip: 192.168.122.1 + # replace: true too lazy for side effect : converge once, then change the IP in etc/host, the re-converge - name: infra.osgiliath.test ip: 192.168.122.2 + # replace: true too lazy for side effect - name: poc.osgiliath.test ip: 192.168.122.2 roles: diff --git a/pyproject.toml b/pyproject.toml index a76f5c7..11c00ab 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "tcharl.etchost_append" -version = "0.1.0" +version = "2.0.1" description = "Appends entries in /etc/hosts file" authors = ["Charlie Mordant "] license = "Apache2" diff --git a/tasks/host.yml b/tasks/host.yml index 1adaa72..0efdd27 100644 --- a/tasks/host.yml +++ b/tasks/host.yml @@ -1,29 +1,49 @@ --- -- name: Host | check ip is in hostfile +- name: Host | Remove ip and hostname in hostfile if flag is set and hostname is the unique entry {{ hostname }} + ansible.builtin.replace: + path: /etc/hosts + regexp: '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s{{ hostname.replace("\.", "\\.") }}$' + replace: '' + when: + - replace is defined + - replace + become: yes + +- name: Host | check ip is in hostfile {{ ip }} ansible.builtin.shell: > set -o pipefail && - cat /etc/hosts | grep -Ec '{{ ip.replace('\.', '\\.') }}' + cat /etc/hosts | grep -Ec '{{ ip.replace('\.', '\\.') }}\s+' register: hosts_ip_entry_exists ignore_errors: true failed_when: false changed_when: false +- name: Host | Remove hostname in hostfile if flag is set + ansible.builtin.replace: + path: /etc/hosts + regexp: '^(.*)\s+{{ hostname.replace("\.", "\\.") }}(.*)$' + replace: '\1\2' + when: + - replace is defined + - replace + become: yes + - name: Host | check ip and hostname is in hostfile ansible.builtin.shell: > set -o pipefail && - cat /etc/hosts | grep -Ec '{{ ip.replace('\.', '\\.') }}.*{{ hostname.replace('\.', '\\.') }}' + cat /etc/hosts | grep -Ec '{{ ip.replace('\.', '\\.') }}\s.*{{ hostname.replace('\.', '\\.') }}' register: hosts_entry_exists ignore_errors: true failed_when: false changed_when: false -- name: "Host | add host in hosts file if ip entry does not exists {{ hostname }}" +- name: "Host | add host in hosts file if ip entry exists and hostname not present {{ hostname }}" ansible.builtin.lineinfile: path: /etc/hosts - regexp: '^{{ ip }}(.*)$' + regexp: '^{{ ip.replace("\.", "\\.") }}\s(.*)$' backrefs: yes - line: '{{ ip }} {{ hostname }}\1' + line: '{{ ip }} {{ hostname }} \1' owner: root group: root mode: '0644' @@ -46,10 +66,7 @@ become: true changed_when: false # IPV6 have tendency to add some new entries every time - when: > - (hosts_ip_entry_exists is failed or - hosts_ip_entry_exists.rc == 1 or - hosts_ip_entry_exists.stdout == 0) and - (hosts_entry_exists.rc == 1 or - hosts_entry_exists.stdout == 0) and - (ansible_facts['virtualization_type'] is not defined or ansible_facts['virtualization_type'] not in ['container']) + when: + - hosts_ip_entry_exists.rc == 1 or hosts_ip_entry_exists.stdout == 0 + - hosts_entry_exists.rc == 1 or hosts_entry_exists.stdout == 0 + - ansible_facts['virtualization_type'] is not defined or ansible_facts['virtualization_type'] not in ['container'] diff --git a/tasks/host_entries.yml b/tasks/host_entries.yml index 5593295..a865aa4 100644 --- a/tasks/host_entries.yml +++ b/tasks/host_entries.yml @@ -5,6 +5,7 @@ vars: hostname: "{{ entry.name }}" ip: "{{ entry.ip }}" + replace: "{{ entry.replace | default(false) }}" loop: "{{ hosts_entries | default([]) }}" loop_control: loop_var: entry