Skip to content
Snippets Groups Projects
Commit 097a5b54 authored by Chris Croome's avatar Chris Croome
Browse files

Use the comment filter and other updates

parent 9fb89f3b
No related branches found
No related tags found
No related merge requests found
Pipeline #24412 passed
......@@ -81,8 +81,4 @@ If you use this role please use a tagged release, see [the release notes](https:
## License
This role is released under the same terms as Ansible itself, the [GNU GENERAL PUBLIC LICENSE, Version 3](LICENSE).
## Author
Copyright [Chris Croome](https://git.coop/chris).
This role is released under [the same terms as Ansible itself](https://github.com/ansible/ansible/blob/devel/COPYING), the [GNU GPLv3](LICENSE).
......@@ -5,9 +5,15 @@ systemd_units:
- name: systemd-timesyncd
files:
- path: /etc/systemd/timesyncd.conf
comment: |
Entries in this file show the compile time defaults.
You can change settings by editing this file.
Defaults can be restored by simply deleting this file.
See timesyncd.conf(5) for details.
conf:
Time:
NTP: 1.pool.ntp.org 0.pool.ntp.org 3.pool.ntp.org 2.pool.ntp.org
state: edited
pkgs:
- systemd-timesyncd
...
......@@ -6,7 +6,7 @@ galaxy_info:
description: Ansible role for configuring some systemd services on Debian
company: Webarchitects Co-operative
license: GNU General Public License v3.0 (GPLv3)
min_ansible_version: "2.11"
min_ansible_version: "2.13"
platforms:
- name: debian
versions:
......
......@@ -2,6 +2,12 @@
- name: Systemd checks
block:
- name: Ensure that Ansible is version 2.13 or greater
ansible.builtin.assert:
that:
- ansible_version.full is version('2.13.0', '>=')
quiet: "{% if ansible_verbosity == 0 %}true{% else %}false{% endif %}"
- name: List systemd unit files
ansible.builtin.command: systemctl list-unit-files
check_mode: false
......
......@@ -17,9 +17,18 @@
register: systemd_file_absent
when: systemd_file_absent.stat.exists | bool
- name: Include flush handlers task
ansible.builtin.include_tasks: flush_handlers.yml
when: systemd_file_absent.changed | bool
- name: Include check and flush handler tasks when files have changed
block:
- name: Include flush handlers task
ansible.builtin.include_tasks: flush_handlers.yml
- name: Include check tasks
ansible.builtin.include_tasks: checks.yml
when:
- systemd_file_absent is defined
- systemd_file_absent.changed | bool
tags:
- systemd
......
......@@ -23,6 +23,12 @@
loop_control:
loop_var: systemd_variable_pair
notify: Reload systemd
register: systemd_file_edited
- name: Debug systemd_file_edited variable
ansible.builtin.debug:
var: systemd_file_edited
verbosity: 2
- name: Vim modelines comment present at the end of the file
ansible.builtin.lineinfile:
......
......@@ -17,7 +17,9 @@
that:
- systemd_file.state != "edited"
fail_msg: "The {{ systemd_file.path }} file needs to exist before it can be edited, set state to templated for the file to be created."
when: not systemd_file_state.stat.exists | bool
when:
- systemd_file.state is defined
- not systemd_file_state.stat.exists | bool
- name: Systemd unit file read and backed up
block:
......@@ -63,10 +65,7 @@
- name: Comment present at the top of the systemd unit file
ansible.builtin.blockinfile:
path: "{{ systemd_file.path }}"
block: |
{% for systemd_comment_line in systemd_file.comment.split('\n') %}
# {{ systemd_comment_line }}
{% endfor %}
block: "{{ systemd_file.comment | ansible.builtin.comment }}"
insertbefore: BOF
mode: "0644"
owner: root
......@@ -114,6 +113,13 @@
register: systemd_file_present
notify: Reload systemd
- name: Add systemd unit to the list of services to restart
ansible.builtin.set_fact:
systemd_units_restarted: "{{ systemd_units_restarted + [systemd_unit.name] }}"
when:
- systemd_file_present.changed | bool
- systemd_unit.name not in systemd_units_restarted
when: >-
( not systemd_file_state.stat.exists | bool ) or
( systemd_file.state is not defined ) or
......
......@@ -11,6 +11,7 @@
ansible.builtin.systemd:
name: "{{ systemd_unit.name }}"
state: stopped
register: systemd_unit_stopped
when: systemd_unit_file_name in systemd_unit_files_enabled
vars:
systemd_unit_file_name: "{{ systemd_unit.name }}.service"
......@@ -19,10 +20,17 @@
ansible.builtin.apt:
pkg: "{{ systemd_unit.pkgs }}"
state: absent
register: systemd_unit_absent
when:
- systemd_unit.pkgs is defined
- systemd_unit.pkgs | length > 0
- name: Include check tasks
ansible.builtin.include_tasks: checks.yml
when: >-
( systemd_unit_stopped.changed | bool ) or
( systemd_unit_absent.changed | bool )
- name: Systemd unit files absent
ansible.builtin.include_tasks: file_absent.yml
loop: "{{ systemd_unit.files }}"
......
......@@ -11,10 +11,17 @@
ansible.builtin.apt:
pkg: "{{ systemd_unit.pkgs }}"
state: present
register: systemd_pgks_present
when:
- systemd_unit.pkgs is defined
- systemd_unit.pkgs | length > 0
- name: Include check tasks
ansible.builtin.include_tasks: checks.yml
when:
- systemd_pgks_present is defined
- systemd_pgks_present.changed | bool
- name: Include the systemd unit file absent tasks
ansible.builtin.include_tasks: file_absent.yml
loop: "{{ systemd_unit.files }}"
......@@ -41,6 +48,16 @@
name: "{{ systemd_unit.name }}"
state: "{{ systemd_unit.unit_state | default('started') }}"
- name: Systemd services restarted
ansible.builtin.systemd:
service: "{{ systemd_service }}"
state: restarted
loop: "{{ systemd_units_restarted }}"
loop_control:
loop_var: systemd_service
when:
- systemd_units_restarted != []
tags:
- systemd
...
# {{ ansible_managed }}
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See timesyncd.conf(5) for details.
[Time]
NTP={% for server in systemd_timesyncd_servers %}{{ server }}{% if not loop.last %} {% endif %}{% endfor %}
#FallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048
#ConnectionRetrySec=30
#SaveIntervalSec=60
# vim: ft=systemd:
......@@ -2,9 +2,7 @@
{% if systemd_file.comment is defined %}
# BEGIN ANSIBLE MANAGED BLOCK
{% for systemd_comment_line in systemd_file.comment.split('\n') %}
# {{ systemd_comment_line }}
{% endfor %}
{{ systemd_file.comment | ansible.builtin.comment }}
# END ANSIBLE MANAGED BLOCK
{% endif %}
......
......@@ -2,4 +2,5 @@
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') }}"
systemd_units_restarted: []
...
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment