Skip to content

Commit

Permalink
remove molecule entries that are not supported anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
Tcharl committed Aug 18, 2023
1 parent 21ef977 commit 96b6e8d
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand Down
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 0 additions & 2 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions molecule/parallels/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <cmordant1@gmail.com>"]
license = "Apache2"
Expand Down
43 changes: 30 additions & 13 deletions tasks/host.yml
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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']
1 change: 1 addition & 0 deletions tasks/host_entries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 96b6e8d

Please sign in to comment.