From ff9606786c88cbbb68db5a9a83916c84809b153a Mon Sep 17 00:00:00 2001 From: Chris Croome <chris@webarchitects.co.uk> Date: Tue, 22 Nov 2022 13:52:22 +0000 Subject: [PATCH] Fix for ansible_managed comment --- tasks/file_present.yml | 5 ++--- templates/ansible_managed.j2 | 1 + templates/unit.j2 | 2 +- vars/main.yml | 3 +++ 4 files changed, 7 insertions(+), 4 deletions(-) create mode 100644 templates/ansible_managed.j2 diff --git a/tasks/file_present.yml b/tasks/file_present.yml index d7ab54f..c3a5b97 100644 --- a/tasks/file_present.yml +++ b/tasks/file_present.yml @@ -75,7 +75,7 @@ - name: Ansible managed comment present at the top of the file ansible.builtin.lineinfile: path: "{{ systemd_file.path }}" - line: "{{ ansible_managed | ansible.builtin.comment }}" + line: "# {{ systemd_ansible_managed }}" state: present insertbefore: BOF @@ -121,8 +121,7 @@ when: >- ( not systemd_file_state.stat.exists | bool ) or - ( systemd_file.state is not defined ) or - ( systemd_file.state == "templated" ) + ( systemd_file.state is defined and systemd_file.state == "templated" ) - name: Include flush handlers task ansible.builtin.include_tasks: flush_handlers.yml diff --git a/templates/ansible_managed.j2 b/templates/ansible_managed.j2 new file mode 100644 index 0000000..ae8da12 --- /dev/null +++ b/templates/ansible_managed.j2 @@ -0,0 +1 @@ +{{- ansible_managed -}} diff --git a/templates/unit.j2 b/templates/unit.j2 index f96a9ef..aef7444 100644 --- a/templates/unit.j2 +++ b/templates/unit.j2 @@ -1,4 +1,4 @@ -{{ ansible_managed | ansible.builtin.comment }} +# {{ systemd_ansible_managed }} {% if systemd_file.comment is defined %} # BEGIN ANSIBLE MANAGED BLOCK diff --git a/vars/main.yml b/vars/main.yml index 0117c04..85a04ea 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,4 +1,7 @@ --- +# https://jpmens.net/2020/09/29/using-ansible-managed/ +# https://github.com/ansible/ansible/issues/53627#issuecomment-471562410 +systemd_ansible_managed: "{{ lookup('ansible.builtin.template', './templates/ansible_managed.j2') | trim }}" systemd_file_backup: "{{ systemd_file.path | ansible.builtin.dirname }}/.{{ systemd_file.path | ansible.builtin.basename }}.{{ ansible_date_time.iso8601_basic_short }}.bak }}" systemd_unit_files_enabled_query: "[?state == 'enabled'].unit_file" systemd_unit_names: "{{ systemd_units | community.general.json_query('[*].name') }}" -- GitLab