From 6f5443985a402965f748c9245c9b5a33c784ffa1 Mon Sep 17 00:00:00 2001 From: "pulsar89.5" Date: Fri, 21 Oct 2022 15:01:23 +0200 Subject: [PATCH] =?UTF-8?q?[INFO]=20Cr=C3=A9er=20le=20role?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 60 ++++++++++++++++++++++++++++++++++++++++++++-- defaults/main.yml | 5 ++++ handlers/main.yml | 2 ++ meta/main.yml | 2 +- tasks/main.yml | 9 +++++++ templates/hosts.j2 | 28 ++++++++++++++++++++++ 6 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 defaults/main.yml create mode 100644 handlers/main.yml create mode 100644 tasks/main.yml create mode 100644 templates/hosts.j2 diff --git a/README.md b/README.md index 3edfbf7..e293de8 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,59 @@ -# role_modele +# role_etc_hosts -Modèle \ No newline at end of file +Gérer le fichier /etc/hosts + +## Variables + +### etc_hosts_local + +Liste de nom d'hôte attaché à 127.0.0.1. + +Valeur par défaut: aucune + +### etc_hosts_specifics + +Liste de ligne à ajouter à `/etc/hosts`. + +Valeur par défaut: aucune + +--- + +## Exemple d'utilisation + +### inventory.yml + +```yaml +--- + +all: + hosts: + host1.ykn.local: + host2.ykn.local: + children: + dnsservers: + hosts: + dnsmasq1.ykn.local: + dnsmasq2.ykn.local: +``` + +### host_vars/host1.ykn.local.yml + +```yaml +--- + +etc_hosts_local: + - www.ykn.fr + +etc_hosts_specifics: + - 192.168.50.200 gitea.ykn.fr +``` + +### playbook.yml + +```yaml +--- + +- hosts: 'all' + roles: + - name: etc_hosts +``` diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..e39a917 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,5 @@ +--- +# defaults file for etc_hosts + +etc_hosts_local: [] +etc_hosts_specifics: [] diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..522595c --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,2 @@ +--- +# handlers file for etc_hosts diff --git a/meta/main.yml b/meta/main.yml index c58bebf..818c0dc 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -1,7 +1,7 @@ galaxy_info: namespace: ykn author: pulsar89.5 - description: Rôle modèle + description: Gérer le fichier /etc/hosts license: GPL-3.0-or-later diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..29615f8 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,9 @@ +--- +# tasks file for etc_hosts + +- name: Déployer le fichier /etc/hosts + ansible.builtin.template: + mode: u=rw,g=r,o=r + src: hosts.j2 + dest: /etc/hosts + become: true diff --git a/templates/hosts.j2 b/templates/hosts.j2 new file mode 100644 index 0000000..9b2c23c --- /dev/null +++ b/templates/hosts.j2 @@ -0,0 +1,28 @@ +# {{ ansible_managed }} + +{%- if inventory_hostname != inventory_hostname_short %} + {%- set host = inventory_hostname ~ ' ' ~ inventory_hostname_short %} +{%- else %} + {% set host = inventory_hostname_short %} +{%- endif %} + +{%- if etc_hosts_local | length > 0 %} + {% set line = host ~ ' ' ~ (etc_hosts_local | join(' ')) %} +{%- else %} + {% set line = host %} +{%- endif +%} + +127.0.0.1 {{ line }} localhost + +# The following lines are desirable for IPv6 capable hosts +::1 {{ line }} localhost ip6-localhost ip6-loopback +fe00::0 ip6-localnet +ff00::0 ip6-mcastprefix +ff02::1 ip6-allnodes +ff02::2 ip6-allrouters +ff02::3 ip6-allhosts + +{% if etc_hosts_specifics | length > 0 %} +# The following lines are specified by instance configuration +{{ etc_hosts_specifics | join('\n') }} +{% endif %} -- 2.39.5