Debug loop of registered with_items

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

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.

Maybe you want something like:

- debug:
    …
  with_items: "{{ check_services_enabled.results }}"

A temporary "- debug: var=check_services_enabled" task should help you
to work out what to do.

-- Abhijit

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