--- # 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 }}.service" enabled: true loop: "{{ debian_services_active_and_started }}" loop_control: label: "{{ item }}" when: - 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" - not ('lxc_guest' in group_names and item in ['haveged', 'ntpsec', 'ntp']) - name: (systemd-services.yml) Start service ansible.builtin.systemd: name: "{{ item }}.service" state: started loop: "{{ debian_services_active_and_started }}" loop_control: label: "{{ item }}" when: - 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" - not ('lxc_guest' in group_names and item in ['haveged', 'ntpsec', 'ntp'])