# Debug loop of registered with\_items

**URL:** <https://forum.ansible.com/t/debug-loop-of-registered-with-items/28293>\
**Category:** Ansible Developer\
**Created:** [August 8, 2018, 10:20am UTC](https://forum.ansible.com/t/debug-loop-of-registered-with-items/28293 "2018-08-08T10:20:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![email](https://avatars.discourse-cdn.com/v4/letter/e/54ee81/32.png) [@email](https://forum.ansible.com/u/email)\
**Post date:** [August 8, 2018, 10:20am UTC](https://forum.ansible.com/t/debug-loop-of-registered-with-items/28293/1 "2018-08-08T10:20:04Z")

</div>

Hi all,

I am struggling to find a decent way of going this.

Here’s what I have so far, which obviously doesn’t work

`

- name: Check services are set to start on boot  
service:  
name: “{{ item.name }}”  
enabled: yes  
check\_mode: true  
ignore\_errors: yes  
register: check\_services\_enabled  
with\_items:

- { name: apache2 }

- { name: nginx }

- name: debug  
debug:  
msg: “{% if check\_services\_enabled.changed %}{{check\_services\_enabled.item.name}} not enabled to start at boot{% endif %}”

`

essentially what I am trying to do is check a long list of services, to see if they’re enabled to start at boot. I don’t wish to change any states, I only want to check them.

Currently, I am checking each service individually but I’d like to be able to streamline it down and I think the above is possible.

I am currently struggling to find a way to do a foreach item.name in the debug above. I think I need to create a loop of the with\_items of the service check.

Is anyone able to point me into the right direction? I’ve looked at the docs, but I can only see a basic debug with\_items, but not with\_items from a registered variable.

If there’s a better way of doing this, then I’m all ears!

Thanks in advance

---

<div class="post-metadata">

**Author:** ![email](https://avatars.discourse-cdn.com/v4/letter/e/54ee81/32.png) [@email](https://forum.ansible.com/u/email)\
**Post date:** [August 8, 2018, 10:22am UTC](https://forum.ansible.com/t/debug-loop-of-registered-with-items/28293/2 "2018-08-08T10:22:32Z")

</div>

To help, here’s the output of -vvvv

`

changed: [ubuntu16] =\> (item={u’name’: u’nginx’}) =\> {  
“changed”: true,  
“enabled”: true,  
“invocation”: {  
“module\_args”: {  
“daemon\_reload”: false,  
“enabled”: true,  
“force”: null,  
“masked”: null,  
“name”: “nginx”,  
“no\_block”: false,  
“state”: null,  
“user”: false  
}  
},  
“item”: {  
“name”: “nginx”  
},

`

The above is replicated for each service that is listed in with\_items.

---

<div class="post-metadata">

**Author:** ![Abhijit\_Menon-Sen1](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Abhijit\_Menon-Sen1](https://forum.ansible.com/u/Abhijit_Menon-Sen1)\
**Post date:** [August 8, 2018, 10:38am UTC](https://forum.ansible.com/t/debug-loop-of-registered-with-items/28293/3 "2018-08-08T10:38:05Z")

</div>

Maybe you want something like:

- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;…  
&nbsp;&nbsp;with\_items: "{{ check\_services\_enabled.results }}"

A temporary "- debug: var=check\_services\_enabled" task should help you  
to work out what to do.

-- Abhijit

---

<div class="post-metadata">

**Author:** ![email](https://avatars.discourse-cdn.com/v4/letter/e/54ee81/32.png) [@email](https://forum.ansible.com/u/email)\
**Post date:** [August 8, 2018, 3:04pm UTC](https://forum.ansible.com/t/debug-loop-of-registered-with-items/28293/4 "2018-08-08T15:04:07Z")

</div>

Thanks for your input.

I have got further, but I am not quite there yet.

Here’s my code:

variables:

`

users\_with\_items:

- name: “nginx”
- name: “apache2”
- name: “zabbix-agent”
- name: “postfix”
- name: “redis-server”

`

`

- name: “Check list of services”  
service:  
name: “{{ item.name }}”  
enabled: yes  
check\_mode: true  
ignore\_errors: yes  
with\_items: “{{ users\_with\_items }}”  
when: item.name in ansible\_facts.services  
register: registered\_services

- name: debug  
debug:  
msg: “{% if item.name in ansible\_facts.services %}{% if item.changed %}{{fail}}{{item.name}} does not seem to be set to start at boot time{% else %}{{ok}}{{item.name}} is set to start on boot{% endif %}{% endif %}”  
loop: “{{ registered\_services.results }}”  
loop\_control:  
label: “{{ item.name }}”  
register: registered\_services\_check  
ignore\_errors: yes  
with\_items: “{{ registered\_services.results }}”  
when: item is defined

`

So the principle is there, but I’m getting an error on the debug section of this.

So as zabbix-agent is not installed on the device (which is possible in this use case), this item is skipped from the `Check list of services` section:

`

TASK [reboot-checks : Check list of services] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [ubuntu16] =\> (item={u’name’: u’nginx’})  
ok: [ubuntu16] =\> (item={u’name’: u’apache2’})  
skipping: [ubuntu16] =\> (item={u’name’: u’zabbix-agent’})  
ok: [ubuntu16] =\> (item={u’name’: u’postfix’})  
ok: [ubuntu16] =\> (item={u’name’: u’redis-server’})

`

This is fine. But, it also means so far that the debug is failing. I can not work out how to get the debug to skip the items which are skipped in the registered\_services task:

`

TASK [reboot-checks : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [ubuntu16] =\> (item=nginx) =\> {  
“msg”: “[FAIL] nginx does not seem to be set to start at boot time”  
}  
ok: [ubuntu16] =\> (item=apache2) =\> {  
“msg”: “[OK] apache2 is set to start on boot”  
}  
fatal: [ubuntu16]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘dict object’ has no attribute ‘name’\n\nThe error appears to have been in ‘roles/reboot-checks/tasks/main.yml’: line 44, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: debug\n ^ here\n”}  
…ignoring

`

I’ve tried adding a `when: item not skipped` and other stuff like that but I don’t seem to be able to get there as that item is not present/undefined.

I’ve been trying for the last few hours so maybe I’ve got tunnel vision now but if anyone is able to help out I would be very grateful.

Thanks
