diff --git a/roles/common/tasks/systemd-services_debian_based_OS.yml b/roles/common/tasks/systemd-services_debian_based_OS.yml index 80312d2..3a079a9 100644 --- a/roles/common/tasks/systemd-services_debian_based_OS.yml +++ b/roles/common/tasks/systemd-services_debian_based_OS.yml @@ -1,58 +1,31 @@ --- -- name: (systemd-services.yml) Check if Service Exists (Debian 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: - - "{{ 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 }}" +# 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 (Debian based OS) + ansible.builtin.service_facts: - 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: "{{ debian_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.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: "{{ debian_services_active_and_started }}" loop_control: - label: "{{ item.item.item }}" + label: "{{ item }}" 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" diff --git a/roles/common/tasks/systemd-services_redhat_based_OS.yml b/roles/common/tasks/systemd-services_redhat_based_OS.yml index 023b40d..04cd52a 100644 --- a/roles/common/tasks/systemd-services_redhat_based_OS.yml +++ b/roles/common/tasks/systemd-services_redhat_based_OS.yml @@ -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"