refactor(systemd-services): streamline service checks for Debian and RedHat OS
This commit is contained in:
@@ -1,61 +1,35 @@
|
||||
---
|
||||
- name: (systemd-services.yml) Check if Service Exists (RedHat based OS)
|
||||
ansible.builtin.shell: 'systemctl list-unit-files | grep -q -e "^{{ item }}.service";'
|
||||
changed_when: "service_exists.rc > 1"
|
||||
failed_when: "service_exists.rc > 1"
|
||||
register: service_exists
|
||||
with_items:
|
||||
- "{{ redhat_services_active_and_started }}"
|
||||
# Collect current service states into ansible_facts.services so the next tasks
|
||||
# only enable/start services when a real state change is needed.
|
||||
- name: (systemd-services.yml) Gather service facts (RedHat based OS)
|
||||
ansible.builtin.service_facts:
|
||||
when:
|
||||
- ansible_facts.os_family == "RedHat"
|
||||
|
||||
#- debug: msg="{{ service_exists.results }}"
|
||||
|
||||
- name: (systemd-services.yml) Check if Service is disabled (RedHat based OS)
|
||||
ansible.builtin.shell: 'systemctl list-unit-files | grep -e "^{{ item.item }}.service" | grep -q "disabled";'
|
||||
register: service_is_enabled
|
||||
changed_when: "service_is_enabled.rc == 0"
|
||||
failed_when: "service_is_enabled.rc > 1"
|
||||
with_items:
|
||||
- "{{ service_exists.results }}"
|
||||
loop_control:
|
||||
label: "{{ item.item }}"
|
||||
when:
|
||||
- item.rc == 0
|
||||
- ansible_facts.os_family == "RedHat"
|
||||
|
||||
#- debug: msg="{{ service_is_enabled.results }}"
|
||||
|
||||
- name: (systemd-services.yml) Enable service
|
||||
ansible.builtin.systemd:
|
||||
name: "{{ item.item.item }}.service"
|
||||
name: "{{ item }}.service"
|
||||
enabled: true
|
||||
with_items:
|
||||
- "{{ service_is_enabled.results }}"
|
||||
loop: "{{ redhat_services_active_and_started }}"
|
||||
loop_control:
|
||||
label: "{{ item.item.item }}"
|
||||
label: "{{ item }}"
|
||||
when:
|
||||
- item.changed
|
||||
|
||||
- name: (systemd-services.yml) Check if Service is active
|
||||
ansible.builtin.command: "systemctl is-active {{ item.item }}.service"
|
||||
register: service_is_active
|
||||
changed_when: 'service_is_active.stdout == "inactive"'
|
||||
failed_when: "service_is_active.rc > 3"
|
||||
with_items:
|
||||
- "{{ service_exists.results }}"
|
||||
loop_control:
|
||||
label: "{{ item.item }}"
|
||||
when:
|
||||
- item.rc == 0
|
||||
- ansible_facts.os_family == "RedHat"
|
||||
- ansible_facts.services is defined
|
||||
- (item ~ '.service') in ansible_facts.services
|
||||
- ansible_facts.services[item ~ '.service'].status is defined
|
||||
- ansible_facts.services[item ~ '.service'].status == "disabled"
|
||||
|
||||
- name: (systemd-services.yml) Start service
|
||||
ansible.builtin.systemd:
|
||||
name: "{{ item.item.item }}.service"
|
||||
name: "{{ item }}.service"
|
||||
state: started
|
||||
with_items:
|
||||
- "{{ service_is_active.results }}"
|
||||
loop: "{{ redhat_services_active_and_started }}"
|
||||
loop_control:
|
||||
label: "{{ item.item.item }}"
|
||||
label: "{{ item }}"
|
||||
when:
|
||||
- item.changed
|
||||
- ansible_facts.os_family == "RedHat"
|
||||
- ansible_facts.services is defined
|
||||
- (item ~ '.service') in ansible_facts.services
|
||||
- ansible_facts.services[item ~ '.service'].state is defined
|
||||
- ansible_facts.services[item ~ '.service'].state != "running"
|
||||
|
||||
Reference in New Issue
Block a user