# Troubles with conditions, register and loop

**URL:** <https://forum.ansible.com/t/troubles-with-conditions-register-and-loop/35933>\
**Category:** Ansible Project\
**Created:** [October 27, 2021, 5:48pm UTC](https://forum.ansible.com/t/troubles-with-conditions-register-and-loop/35933 "2021-10-27T17:48:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leonardo](https://avatars.discourse-cdn.com/v4/letter/l/e56c9b/32.png) [@Leonardo](https://forum.ansible.com/u/Leonardo)\
**Post date:** [October 27, 2021, 5:48pm UTC](https://forum.ansible.com/t/troubles-with-conditions-register-and-loop/35933/1 "2021-10-27T17:48:03Z")

</div>

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 }}”

# - debug:

# msg: “Valor de result {{ \_result }}”

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [October 27, 2021, 6:03pm UTC](https://forum.ansible.com/t/troubles-with-conditions-register-and-loop/35933/2 "2021-10-27T18:03:30Z")

</div>

Your "Restarting services services¨ task should be looping over the registered \_results array, not {{ sysservices }}. Your sysservices has no “rc”. \_result should have a list, one item per loop of the registering task. It’s that list you want to process.
