Hi guys,
I’m trying to create a lab file to restart a service only if it’s running.
I have troubles when using variables, register and loop together.
- name: Restart a service only if it’s active.
hosts: LAB
vars:
sysservices: - serviceName: httpd
- serviceName: smb
tasks: - name: Checking if service is running
command: /usr/bin/systemctl is-active “{{ item.serviceName }}”
register: _result
ignore_errors: yes
loop: “{{ sysservices }}” - name: Restarting services services
service:
name: “{{ item.serviceName }}”
state: restarted
when: item.rc == 0 ## I tried: item.rc == 0 , item.stdout == “active”, _result.rc == 0, _result.stdout == “active”. All of them didn’t work
loop: “{{ sysservices }}”