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

Add default state present for files

parent faf5222d
No related branches found
No related tags found
No related merge requests found
Pipeline #24428 passed
......@@ -2,11 +2,11 @@
[![pipeline status](https://git.coop/webarch/systemd/badges/main/pipeline.svg)](https://git.coop/webarch/systemd/-/commits/main)
An Ansible role for configuring systemd services on Debian, this role has been designed to be as generic as possible in order to enable to it be used to configure any systemd service.
An Ansible role for configuring systemd services on Debian, this role has been designed to be as generic as possible in order to enable to it be used to configure any systemd service, by default it configures `systemd-timesyncd`.
## Role variables
See the [defaults/main.yml](defaults/main.yml) file for the default variables.
See the [defaults/main.yml](defaults/main.yml) file for the default variables, these are described below.
### systemd
......@@ -14,7 +14,7 @@ Set the `systemd` variable to `false` to prevent any tasks in this role being ru
### systemd_timesyncd_reboot
When the `systemd_timesyncd_reboot` variable is set to `true` servers which have incorrect clocks will be rebooted by this role in order to correct their clocks, this variable defaults to `false`.
When the `systemd_timesyncd_reboot` variable is set to `true` servers which have incorrect clocks will be rebooted by this role in order to correct their clocks, it defaults to `false`.
### systemd_units
......@@ -33,7 +33,7 @@ systemd_units:
conf:
Time:
NTP: 0.pool.ntp.org 1.pool.ntp.org 3.pool.ntp.org 2.pool.ntp.org
state: templated
state: present
pkgs:
- systemd-timesyncd
state: present
......@@ -42,17 +42,18 @@ systemd_units:
The only required variables is `name`, see the [meta/argument_specs.yml](meta/argument_specs.yml) for the variable types.
For each service required `.deb` packages, the state of the service and the files to be created / amended and their content in YAML can be specified.
For each service required `.deb` packages, the state of the service and the files to be created / amended and their content as YAML can be specified.
Files are read using the [JC ini parser](https://kellyjonbrazil.github.io/jc/docs/parsers/ini) and only updated if the `conf` is to be changed.
Files can optionally have one of three optional states set:
Files can optionally have one of four optional states set:
* `absent` - the file will be deleted.
* `edited` - the existing file will be edited using the [Ansible ini module](https://docs.ansible.com/ansible/latest/collections/community/general/ini_file_module.html).
* `present` - if the file exists it will be edited using the [Ansible ini module](https://docs.ansible.com/ansible/latest/collections/community/general/ini_file_module.html), if not it will be created using the [templates/unit.j2](templates/unit.j2) template.
* `templated` - the file will be created if it does not exist or updated if it already exists using the [templates/unit.j2](templates/unit.j2) template.
If the `state` is not set it defaults to `templated`.
If the `state` is not set it defaults to `present`.
The `edited` option can not remove variables and, unlike the `templated` option, it preserves existing comments.
......
......@@ -2,7 +2,7 @@
argument_specs:
main:
author: Chris Croome
description: Ansible role for configuring some systemd services on Debian
description: Ansible role for configuring systemd services on Debian.
short_description: The main entry point for the systemd role.
options:
systemd:
......@@ -28,7 +28,7 @@ argument_specs:
conf:
type: dict
required: false
description: A dictionary of the values of the systemd unit file.
description: A dictionary of the values for the systemd unit file.
path:
type: str
required: true
......@@ -40,6 +40,7 @@ argument_specs:
choices:
- absent
- edited
- present
- templated
name:
type: str
......
......@@ -16,7 +16,7 @@
ansible.builtin.assert:
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."
fail_msg: "The {{ systemd_file.path }} file needs to exist before it can be edited, set state to present or templated for the file to be created."
when:
- systemd_file.state is defined
- not systemd_file_state.stat.exists | bool
......@@ -90,7 +90,7 @@
when:
- systemd_file_state.stat.exists | bool
- systemd_file.state is defined
- systemd_file.state == "edited"
- systemd_file.state is regex('^edited|present$')
- name: Systemd unit file templated
block:
......
......@@ -36,7 +36,7 @@
- systemd_file.state is defined
- systemd_file.state == "absent"
- name: Include the systemd unit file edited or templated tasks
- name: Include the systemd unit file edited, present or templated tasks
ansible.builtin.include_tasks: file_present.yml
loop: "{{ systemd_unit.files }}"
loop_control:
......@@ -44,8 +44,7 @@
label: "{{ systemd_file.path | basename }}"
when: >-
( systemd_file.state is not defined ) or
( systemd_file.state == "edited" ) or
( systemd_file.state == "templated" )
( systemd_file.state is regex('^edited|present|templated$') )
- name: Unit state set
ansible.builtin.systemd:
......
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