From a2a6f6ae2a121f781f11fd373bc204b77cb54794 Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Wed, 25 Sep 2024 18:07:37 +0200 Subject: [PATCH] Manage customizations of /etc/hosts. --- defaults/main.yml | 1 + tasks/etchosts-customizations.yml | 28 ++++++++++++++++++++++++++++ tasks/main.yml | 2 ++ 3 files changed, 31 insertions(+) create mode 100644 tasks/etchosts-customizations.yml diff --git a/defaults/main.yml b/defaults/main.yml index d39fd61..7736e34 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -18,6 +18,7 @@ grub_cmdline_additional_parameters: "" sysctl_custom_file: /etc/sysctl.d/90-custom-values.conf sysctl_opts_reload: yes sysctl_custom_file_state: present +custom_etc_hosts_entries: "" # Only name and value are mandatory. The others have defaults sysctl_custom_options: [] diff --git a/tasks/etchosts-customizations.yml b/tasks/etchosts-customizations.yml new file mode 100644 index 0000000..b41e3d4 --- /dev/null +++ b/tasks/etchosts-customizations.yml @@ -0,0 +1,28 @@ +--- +- name: etchosts-customizations | Add entries to /etc/hosts + when: custom_etc_hosts_entries | length > 0 + tags: + - etchosts + block: + - name: etchosts-customizations | Add custom entries to /etc/hosts + ansible.builtin.blockinfile: + path: /etc/hosts + marker_begin: 'ansible_etchosts_customizations_start' + marker_end: 'ansible_etchosts_customizations_end' + marker: "# {mark} Customization to bashrc installed via ansible" + block: "{{ custom_etc_hosts_entries }}" + state: present + +- name: etchosts-customizations | Remove entries from /etc/hosts + when: custom_etc_hosts_entries | length == 0 + tags: + - etchosts + block: + - name: etchosts-customizations | Remove custom entries from /etc/hosts + ansible.builtin.blockinfile: + path: /etc/hosts + marker_begin: 'ansible_etchosts_customizations_start' + marker_end: 'ansible_etchosts_customizations_end' + marker: "# {mark} Customization to bashrc installed via ansible" + block: "{{ custom_etc_hosts_entries }}" + state: absent diff --git a/tasks/main.yml b/tasks/main.yml index 139081b..5dad0c4 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -7,6 +7,8 @@ ansible.builtin.import_tasks: locale.yml - name: Set the timezone ansible.builtin.import_tasks: timezone.yml +- name: Manage customizations to the /etc/hosts file + ansible.builtin.import_tasks: etchosts-customizations.yml - name: Sysctl kernel parameters ansible.builtin.import_tasks: sysctl.yml - name: Grub command line parameters