From f8cffd3b03bddf89b0c29d9e7a9433389535b1f4 Mon Sep 17 00:00:00 2001 From: vincent Date: Sun, 18 Jul 2021 11:39:52 +0200 Subject: [PATCH] add hosts file management --- defaults/main.yml | 5 +++++ tasks/hosts.yml | 8 ++++++++ tasks/main.yml | 1 + templates/etc_hosts.j2 | 19 +++++++++++++++++++ vars/main.yml | 2 ++ 5 files changed, 35 insertions(+) create mode 100644 tasks/hosts.yml create mode 100644 templates/etc_hosts.j2 diff --git a/defaults/main.yml b/defaults/main.yml index 3d29f6c..7b40db5 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -111,3 +111,8 @@ privatekeytodeploy: [] # keyfile: /root/.ssh/id_rsa # privatekey: {{ vault_privatekey }} +# Custom hosts entries to be added +hosts_entries: [] + +# Custom host file snippets to be added +hosts_file_snippets: [] diff --git a/tasks/hosts.yml b/tasks/hosts.yml new file mode 100644 index 0000000..491fd56 --- /dev/null +++ b/tasks/hosts.yml @@ -0,0 +1,8 @@ +- name: Install custom hosts file + become: true + template: + src: etc_hosts.j2 + dest: "{{ hosts_file }}" + owner: root + group: root + mode: 0644 diff --git a/tasks/main.yml b/tasks/main.yml index 6e61f3a..13725bb 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,4 +1,5 @@ #- include_tasks: hostname.yml +- include_tasks: hosts.yml - include_tasks: hostname.yml - include_tasks: locales.yml - include_tasks: time.yml diff --git a/templates/etc_hosts.j2 b/templates/etc_hosts.j2 new file mode 100644 index 0000000..1fe3dac --- /dev/null +++ b/templates/etc_hosts.j2 @@ -0,0 +1,19 @@ +# Hosts file -- Don't edit manually! +# +# {{ ansible_managed }} +{% if hosts_entries|length != 0 %} + +# Custom entries +{% for host in hosts_entries %} +{{ host.ip }} {{ host.name }} {{ host.aliases | default('') | join(' ') }} +{% endfor %} +{% endif %} +{% if hosts_file_snippets %} + +# +# Hosts file snippets +# + +{{ hosts_file_snippets }} + +{% endif %} diff --git a/vars/main.yml b/vars/main.yml index e69de29..6a29bb7 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -0,0 +1,2 @@ +--- +hosts_file: "/etc/hosts"