diff --git a/tasks/file_present.yml b/tasks/file_present.yml index d7ab54f125f4230462a3ce11c634df111a1766c4..c3a5b979e894454e6418b5a8bcc9def12fa51cbe 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 0000000000000000000000000000000000000000..ae8da12714841917f956a6a0b407d81c13346949 --- /dev/null +++ b/templates/ansible_managed.j2 @@ -0,0 +1 @@ +{{- ansible_managed -}} diff --git a/templates/unit.j2 b/templates/unit.j2 index f96a9ef593f9e3c08fbbee20ee0375a8056d432d..aef74443abdfdc78392c0116e4df2bed7f2faf66 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 0117c0469843f15f37d3b7b4a6c849123c71811e..85a04ea187ba0136ab7180f4eb13069bbd71dc6e 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') }}"