From 0e1e123d1ac8242907d3f30087e8ea5cf74fcc3f Mon Sep 17 00:00:00 2001 From: Alexandre Le Gall Date: Fri, 22 Sep 2023 17:25:03 +0200 Subject: [PATCH] [EVO] Utiliser l'inventaire d'ansible --- tasks/clients.yml | 20 ++++++++++++++++++++ templates/ansible_inventory.conf.j2 | 15 +++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 tasks/clients.yml create mode 100644 templates/ansible_inventory.conf.j2 diff --git a/tasks/clients.yml b/tasks/clients.yml new file mode 100644 index 0000000..80cf616 --- /dev/null +++ b/tasks/clients.yml @@ -0,0 +1,20 @@ +--- +# tasks file for security + +- name: Générer le fichier d'hôtes en partant de l'inventaire ansible + ansible.builtin.template: + src: ansible_inventory.conf.j2 + dest: /srv/dnsmasq/ansible_inventory.conf.disabled + owner: dnsmasq + group: root + mode: u=rw,g=r,o=r + when: + - dnsmasq_servers | length > 0 + - dnsmasq_host_ipv4 not in dnsmasq_servers + tags: creation + loop: "{{ dnsmasq_servers }}" + loop_control: + loop_var: dnsserver + delegate_to: "{{ dnsserver }}" + become: true + notify: Recharger dnsmasq.service sur les serveurs diff --git a/templates/ansible_inventory.conf.j2 b/templates/ansible_inventory.conf.j2 new file mode 100644 index 0000000..5c1172c --- /dev/null +++ b/templates/ansible_inventory.conf.j2 @@ -0,0 +1,15 @@ +# {{ ansible_managed }} + +{% for ip in dnsmasq_host_ips %} +{% endfor %} + +{% set last_host = groups['all'] | last %} +{% for host in groups['all'] %} +{% set hostnames = [hostvars[host]['inventory_hostname']] + [hostvars[host]['inventory_hostname_short']] + dnsmasq_host_aliases %} +{% for ip in dnsmasq_host_ips %} +{{ ip }} {{ hostnames | select() | join(' ') }} +{% endfor %} +{% if host != last_host %} + +{% endif %} +{% endfor %}