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