Skip to content
Snippets Groups Projects
Commit b3ef69f8 authored by Jörg Sachse's avatar Jörg Sachse
Browse files

fix: make tasks idempotent wherever possible

parent a1dd0ed0
No related branches found
No related tags found
1 merge request!1Feat GitLab-CI
...@@ -39,18 +39,18 @@ ...@@ -39,18 +39,18 @@
- name: restart exim - name: restart exim
ansible.builtin.systemd: ansible.builtin.systemd:
name: "exim4" name: "exim4"
state: restarted state: reloaded
enabled: true enabled: true
- name: restart postfix - name: restart postfix
ansible.builtin.service: ansible.builtin.service:
name: "postfix" name: "postfix"
state: restarted state: reloaded
- name: restart sshd - name: restart sshd
ansible.builtin.systemd: ansible.builtin.systemd:
name: "sshd" name: "sshd"
state: restarted state: reloaded
- name: reload journald configuration - name: reload journald configuration
ansible.builtin.systemd: ansible.builtin.systemd:
...@@ -60,8 +60,9 @@ ...@@ -60,8 +60,9 @@
- name: restart logrotate.service - name: restart logrotate.service
ansible.builtin.systemd: ansible.builtin.systemd:
name: "logrotate.service" name: "logrotate.service"
state: restarted state: reloaded
when: ansible_os_family == "Debian" when: ansible_os_family == "Debian"
changed_when: false
- name: udev-Regel bekannt machen - name: udev-Regel bekannt machen
ansible.builtin.command: "udevadm control --reload" ansible.builtin.command: "udevadm control --reload"
--- ---
- name: find out if Glances Service is enabled
ansible.builtin.command: systemctl is-enabled glances.service
register: glances_enabled
changed_when: false
failed_when:
- glances_enabled.stdout != "enabled"
- glances_enabled.stdout != "disabled"
# this is idempotent
- name: stop Glances (web-)server - name: stop Glances (web-)server
ansible.builtin.service: ansible.builtin.service:
name: "glances" name: "glances.service"
state: stopped state: stopped
when:
- ansible_os_family == "Debian"
- glances_enabled.stdout != "disabled"
# this is NOT idempotent, so it needs the "changed: false" stanza
- name: disable Glances (web-)server
ansible.builtin.service:
name: "glances.service"
enabled: false enabled: false
when: ansible_os_family == "Debian" when:
- ansible_os_family == "Debian"
- glances_enabled.stdout != "disabled"
changed_when: false
--- ---
# copy module modifies parent directory permissions, when file or directory is copied with owner and group different than root. It is also not idempotent and changes on subsequent runs.
- name: rollout default logrotate config - name: rollout default logrotate config
ansible.builtin.copy: ansible.builtin.copy:
src: "etc/logrotate.conf" src: "etc/logrotate.conf"
...@@ -7,6 +8,7 @@ ...@@ -7,6 +8,7 @@
group: "root" group: "root"
mode: "0644" mode: "0644"
notify: restart logrotate.service notify: restart logrotate.service
tags: [molecule-idempotence-notest]
- name: set custom compression algorithm - name: set custom compression algorithm
ansible.builtin.blockinfile: ansible.builtin.blockinfile:
......
--- ---
# copy module modifies parent directory permissions, when file or directory is copied with owner and group different than root. It is also not idempotent and changes on subsequent runs.
- name: configure NTP - name: configure NTP
ansible.builtin.copy: ansible.builtin.copy:
src: "etc/ntp.conf" src: "etc/ntp.conf"
...@@ -6,3 +7,4 @@ ...@@ -6,3 +7,4 @@
owner: "root" owner: "root"
group: "root" group: "root"
mode: "0644" mode: "0644"
tags: [molecule-idempotence-notest]
--- ---
# enable persistent systemd journalctl logging
# Documentation: zless /usr/share/doc/systemd/README.Debian.gz
- name: enable persistent systemd journalctl logging
block:
- name: create log directory
ansible.builtin.file:
path: "/var/log/journal"
state: directory
mode: "0644"
owner: "root"
group: "systemd-journal"
- name: find out if journald is already logging to a persistent location
ansible.builtin.shell: "systemd-tmpfiles --cat-config | grep '/var/log/journal'"
register: jd_persist
changed_when: false
- name: link directory name to systemd
ansible.builtin.command: systemd-tmpfiles --create --prefix /var/log/journal
when: jd_persist.rc == 0
# Documentation: https://www.freedesktop.org/software/systemd/man/journald.conf.html # Documentation: https://www.freedesktop.org/software/systemd/man/journald.conf.html
- name: configure journald - name: configure journald
ansible.builtin.blockinfile: ansible.builtin.blockinfile:
......
--- ---
- name: check if swap is active
ansible.builtin.command: swapon -s
register: swap_active
changed_when: false
# https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#testing-strings # https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#testing-strings
- name: switch off swap (immediate result for running server, not reboot persistent) - name: switch off swap (immediate result for running server, not reboot persistent)
ansible.builtin.command: swapoff -a ansible.builtin.command: swapoff -va
when: ansible_hostname is not search("validate") when:
- ansible_hostname is not search("validate")
- swap_active.stdout == ""
register: disable_swap
changed_when: disable_swap.stdout not in "swapoff LABEL=swap"
- name: switch off swap (no result for running server, reboot persistent) - name: switch off swap (no result for running server, reboot persistent)
ansible.posix.mount: ansible.posix.mount:
path: "none" path: "none"
fstype: "swap" fstype: "swap"
state: "absent" state: "absent"
when: ansible_hostname is not search("validate") when:
- ansible_hostname is not search("validate")
- swap_active.stdout == ""
...@@ -17,9 +17,11 @@ ...@@ -17,9 +17,11 @@
# import_tasks: configure-network.yml # import_tasks: configure-network.yml
# tags: [network,dns] # tags: [network,dns]
# We don't test for idempotence because these tasks can never be idempotent.
# They are meant to copy fresh Backups of the SSH keys every time they are run.
- name: Server-SSH-Schlüssel sichern - name: Server-SSH-Schlüssel sichern
import_tasks: backup_ssh_hostkeys.yml import_tasks: backup_ssh_hostkeys.yml
tags: [ssh] tags: [ssh, molecule-idempotence-notest]
- name: SLUB-lokales Debian-Repository hinzufügen - name: SLUB-lokales Debian-Repository hinzufügen
import_tasks: configure_package_repositories.yml import_tasks: configure_package_repositories.yml
...@@ -147,6 +149,7 @@ ...@@ -147,6 +149,7 @@
when: ansible_os_family == "RedHat" when: ansible_os_family == "RedHat"
tags: [ntp] tags: [ntp]
# there's no way to get this task to become idempotent, so we have to skip the test
- name: Flush handlers am Ende der Rolle - name: Flush handlers am Ende der Rolle
ansible.builtin.meta: flush_handlers ansible.builtin.meta: flush_handlers
tags: [always] tags: [always, molecule-idempotence-notest]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment