EOX GitLab Instance

Skip to content
Snippets Groups Projects
Commit f460230c authored by Karl Grube's avatar Karl Grube
Browse files

cleaned up the etc_hosts file

parent 4bdb9952
No related branches found
No related tags found
No related merge requests found
---
- name: block of etc host config
block:
- name: Determine service interface
set_fact:
current_host_service_interface: "{{ item.keys() | difference(['service']) | first }}"
with_items: "{{ my_interfaces }}"
when:
- "'service' in item"
- item.service == True
delegate_to: "{{prometheus_peer}}"
- name: Capture IPv6 address of the current host
set_fact:
current_host_ipv6: "{{ hostvars[inventory_hostname]['ansible_' + current_host_service_interface]['ipv6'] | selectattr('scope', 'equalto', 'global') | map(attribute='address') | first }}"
when:
- "'ansible_' + current_host_service_interface in hostvars[inventory_hostname]"
- "'ipv6' in hostvars[inventory_hostname]['ansible_' + current_host_service_interface]"
delegate_to: "{{prometheus_peer}}"
- name: Update /etc/hosts with IPv6 address of current host
lineinfile:
path: /etc/hosts
line: "{{ current_host_ipv6 }} {{ prometheus_peer }}"
state: present
regex: "^.*{{ prometheus_peer }}.*"
when:
- current_host_ipv6 is defined
- current_host_ipv6 != ''
- name: reset current_host_ipv6
set_fact:
current_host_ipv6: ''
tags: etc_hosts
####BEGIN on the hellsing group, specific lines in /etc/hosts file
#- name: Determine service interface
# set_fact:
# service_interface: "{{ item.keys() | difference(['service']) | first }}"
# with_items: "{{ my_interfaces }}"
# when:
# - "'service' in item"
# - item.service == True
##debugging
#- name: Debug service interface
# debug:
# msg: "Service interface: {{ service_interface }}"
#
#- name: Capture IPv6 address of the current host
# set_fact:
# current_host_ipv6: "{{ hostvars[inventory_hostname]['ansible_' + service_interface]['ipv6'] | selectattr('scope', 'equalto', 'global') | map(attribute='address') | first }}"
# when:
# - "'ansible_' + service_interface in hostvars[inventory_hostname]"
# - "'ipv6' in hostvars[inventory_hostname]['ansible_' + service_interface]"
##debugging
#- name: Debug ipv6 current host
# debug:
# msg: "ipv6 {{ inventory_hostname }}: {{ current_host_ipv6 }}"
#
#
#
#- name: Determine the other host in the hellsing group
# set_fact:
# other_host: "{{ prometheus_peers | reject('equalto', inventory_hostname) | first }}"
# when: groups['hellsing'] | length < 3
# run_once: true
##debugging
#- name: Debug ipv6 current host
# debug:
# msg: "The other host is: {{ other_host }}"
#
#- name: Gather facts from other hellsing hosts
# setup:
# delegate_to: "{{ other_host }}"
# run_once: true
# register: remote_facts
#
#- name: Set fact for remote host
# set_fact:
# other_host_facts: "{{ remote_facts.ansible_facts }}"
#
#- name: Capture IPv6 address of the other host now
# set_fact:
# other_host_ipv6: "{{ other_host_facts['ansible_' + service_interface]['ipv6'] | selectattr('scope', 'equalto', 'global') | map(attribute='address') | first }}"
# when:
# - "'ansible_' + service_interface in other_host_facts"
# - "'ipv6' in other_host_facts['ansible_' + service_interface]"
##debugging
#- name: Debug ipv6 current host
# debug:
# msg: "The other host has the IP: {{ other_host_ipv6 }}"
#
##### Update the hosts file now
#- name: Update /etc/hosts with IPv6 address of current host
# lineinfile:
# path: /etc/hosts
# line: "{{ current_host_ipv6 }} {{ inventory_hostname }}"
# state: present
# when:
# - current_host_ipv6 is defined
# - current_host_ipv6 != ''
#
#- name: Update /etc/hosts with IPv6 address of the other host
# lineinfile:
# path: /etc/hosts
# line: "{{ other_host_ipv6 }} {{ other_host }}"
# state: present
# when:
# - other_host_ipv6 is defined
# - other_host_ipv6 != ''
# - other_host is defined
#
####END of the script for adding specific lines on an Hellsing server
---
###BEGIN on the hellsing group, specific lines in /etc/hosts file
- name: Determine service interface
set_fact:
service_interface: "{{ item.keys() | difference(['service']) | first }}"
loop: "{{ my_interfaces }}"
when:
- "'service' in item"
- item.service == True
run_once: true
#debugging
- name: Debug service interface
debug:
msg: "Service interface: {{ service_interface }}"
- name: Capture IPv6 address of the current host
set_fact:
current_host_ipv6: "{{ hostvars[inventory_hostname]['ansible_' + service_interface]['ipv6'] | selectattr('scope', 'equalto', 'global') | map(attribute='address') | first }}"
when:
- "'ansible_' + service_interface in hostvars[inventory_hostname]"
- "'ipv6' in hostvars[inventory_hostname]['ansible_' + service_interface]"
#debugging
- name: Debug ipv6 current host
debug:
msg: "ipv6 {{ inventory_hostname }}: {{ current_host_ipv6 }}"
- name: Determine the other host in the hellsing group
set_fact:
other_host: "{{ groups['hellsing'] | reject('equalto', inventory_hostname) | first }}"
when: groups['hellsing'] | length < 3
run_once: true
#debugging
- name: Debug ipv6 current host
debug:
msg: "The other host is: {{ other_host }}"
- name: Gather facts from other hellsing hosts
setup:
delegate_to: "{{ other_host }}"
run_once: true
register: remote_facts
- name: Set fact for remote host
set_fact:
other_host_facts: "{{ remote_facts.ansible_facts }}"
- name: Capture IPv6 address of the other host now
set_fact:
other_host_ipv6: "{{ other_host_facts['ansible_' + service_interface]['ipv6'] | selectattr('scope', 'equalto', 'global') | map(attribute='address') | first }}"
when:
- "'ansible_' + service_interface in other_host_facts"
- "'ipv6' in other_host_facts['ansible_' + service_interface]"
#debugging
- name: Debug ipv6 current host
debug:
msg: "The other host has the IP: {{ other_host_ipv6 }}"
#### Update the hosts file now
- name: Update /etc/hosts with IPv6 address of current host
lineinfile:
path: /etc/hosts
line: "{{ current_host_ipv6 }} {{ inventory_hostname }}"
state: present
when:
- current_host_ipv6 is defined
- current_host_ipv6 != ''
- name: Update /etc/hosts with IPv6 address of the other host
lineinfile:
path: /etc/hosts
line: "{{ other_host_ipv6 }} {{ other_host }}"
state: present
when:
- other_host_ipv6 is defined
- other_host_ipv6 != ''
- other_host is defined
###END of the script for adding specific lines on an Hellsing server
\ No newline at end of file
......@@ -55,6 +55,13 @@
description: 'Prometheus server'
tags: label
- import_tasks: hellsing_hosts_ipv6.yml
tags: hosts_ipv6
when: use_service_vrf and inventory_hostname in groups['hellsing']
\ No newline at end of file
- include_tasks: etc_hosts.yml
tags: etc_hosts
loop_control:
loop_var: prometheus_peer
with_items: "{{prometheus_peers}}"
when:
- use_service_vrf is defined
- prometheus_peers is defined
- my_interfaces is defined
- prometheus_peer != inventory_hostname
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment