new service state handling

This commit is contained in:
2024-08-06 11:30:30 -04:00
parent 6fb410cffd
commit aba39cbad4
7 changed files with 108 additions and 55 deletions

53
tasks/check_services.yml Normal file
View File

@ -0,0 +1,53 @@
---
- name: Capture a list of running services
ansible.builtin.set_fact:
bigboot_systemd_running_services:
"{{ bigboot_systemd_running_services | default([]) + [item['key']] }}"
loop: "{{ ansible_facts['services'] | dict2items }}"
loop_control:
label: "{{ item['key'] }}"
when:
- "'running' in item['value']['state']"
- name: Get the stop timeout value for running services
ansible.builtin.shell:
cmd: |
set -o pipefail
systemctl show {{ item }} | grep TimeoutStopUSec
changed_when: false
register: bigboot_systemd_service_timeout
loop: "{{ bigboot_systemd_running_services }}"
- name: Adding service to a list of services to disable for exceeding timeout threshold
ansible.builtin.set_fact:
bigboot_services_disabled: "{{ bigboot_services_disabled | default([]) + [item['item']] }}"
loop: "{{ bigboot_systemd_service_timeout['results'] }}"
loop_control:
label: "{{ item['item'] }}"
when:
- item['item'] not in bigboot_protected_services
- item['stdout'] | regex_replace('^.*=(.*$)', '\\1') | community.general.to_minutes >= bigboot_service_max_timeout | int
- name: Adding service to a list of services to disable for being incompatible
ansible.builtin.set_fact:
bigboot_services_disabled: "{{ bigboot_services_disabled | default([]) + [item] }}"
loop: "{{ bigboot_incompatible_services }}"
when:
- item not in bigboot_protected_services
- ansible_facts['services'][item] is defined
- ansible_facts['services'][item]['state'] == "running"
- name: Log and disable services
ansible.builtin.include_tasks: tasks/disable_systemd_services.yml
loop: "{{ bigboot_services_disabled }}"
when:
- bigboot_services_disabled is defined
- name: Services disabled notice
ansible.builtin.debug:
msg: >-
The following services were disabled, and will be re-enabled post
Bigboot execution: {{ bigboot_services_disabled | join(', ') }}
when:
- bigboot_services_disabled is defined
- bigboot_services_disabled | length > 0

View File

@ -29,12 +29,10 @@
path: "/boot/initramfs-{{ initramfs_kernel_version }}.img.{{ initramfs_backup_extension }}"
state: absent
- name: Check if disable services log exists
ansible.builtin.stat:
path: "{{ bigboot_disabled_services_log }}"
register: bigboot_disabled_services_log_stat
- name: Check for Bigboot state log and restore services to pre-Bigboot state
ansible.builtin.import_tasks: tasks/restore_services.yml
- name: Remove disabled services log if present
- name: Cleanup previous Bigboot state log if present
ansible.builtin.file:
path: "{{ bigboot_disabled_services_log }}"
state: absent

View File

@ -1,19 +1,21 @@
---
- name: Disabling service for exceeding the timeout threshold
- name: Save service state
ansible.builtin.set_fact:
bigboot_service_to_disable:
service: "{{ ansible_facts['services'][item]['name'] }}"
state: "{{ (ansible_facts['services'][item]['state'] == 'running') | ternary('started', 'stopped') }}"
status: "{{ (ansible_facts['services'][item]['status'] == 'enabled') | ternary('true', 'false') }}"
- name: Disabling service
ansible.builtin.service:
name: "{{ item['item'] }}"
name: "{{ item }}"
state: stopped
enabled: false
- name: Append service to list of disabled services
ansible.builtin.set_fact:
bigboot_systemd_disabled_services:
"{{ bigboot_systemd_disabled_services | default([]) + [item['item']] }}"
- name: Log disabled service to log file
- name: Log disabled service state
ansible.builtin.lineinfile:
path: "{{ bigboot_disabled_services_log }}"
line: "{{ item['item'] }}"
line: "{{ item }},{{ bigboot_service_to_disable['state'] }},{{ bigboot_service_to_disable['status'] }}"
create: true
state: present
owner: root

View File

@ -0,0 +1,25 @@
---
- name: Check for Bigboot service state log presence
ansible.builtin.stat:
path: "{{ bigboot_disabled_services_log }}"
register: bigboot_disabled_services_log_stat
- name: Read state log and restore service state
when:
- bigboot_disabled_services_log_stat['stat']['exists'] | bool
block:
- name: Read service state from log
community.general.read_csv:
path: "{{ bigboot_disabled_services_log }}"
fieldnames: service,state,enabled
delimiter: ','
register: bigboot_service_state_contents
- name: Restore service state
ansible.builtin.service:
name: "{{ item['service'] }}"
state: "{{ item['state'] }}"
enabled: "{{ item['enabled'] | bool }}"
loop: "{{ bigboot_service_state_contents['list'] }}"
loop_control:
label: "{{ item['service'] }}"