how to stuff running services into a host variable?

28 views
Skip to first unread message

Amos

unread,
Aug 12, 2019, 3:57:36 PM8/12/19
to ansible...@googlegroups.com
I can't seem to get this syntax correct, and at least one reference I found produced more errors.  I'd like to create a list of all running processes.

    - name: populate running services into a list
      set_fact:
        services_running: >
          {%  if (hostvars[inventory_hostname]['services']['{{ item }}']['state'] == 'running')  %}
                 services_running + [ '{{ item }}' ]
          {%  endif  %}
      with_items: "{{ hostvars[inventory_hostname]['services'].keys() }}"


Vladimir Botka

unread,
Aug 12, 2019, 4:26:36 PM8/12/19
to Amos, ansible...@googlegroups.com
Try this one

- name: populate running services into a list
set_fact:
services_running: "{{ services_running|default([]) + [ item ] }}"
loop: "{{ services.keys() }}"
when: services[item].state == 'running'

Cheers,

-vlado

Amos

unread,
Aug 12, 2019, 6:59:21 PM8/12/19
to Vladimir Botka, ansible...@googlegroups.com
seems close, but getting:

  msg: 'Invalid data passed to ''loop'', it requires a list, got this instead: dict_keys([''abrt-xorg.service'', ''sshd-keygen.service'', ''systemd-machine-id-commit.service'', ''iprinit.servic
e'', ''systemd-readahead-collect.service'', ''puppet.service'', ''plymouth-kexec.service'', ''cgdcbxd.service'', ''console-shell.service'', ''rngd.service'', ''sssd-autofs.service'', .....


Vladimir Botka

unread,
Aug 12, 2019, 7:26:56 PM8/12/19
to Amos, ansible...@googlegroups.com
> > > I can't seem to get this syntax correct, and at least one reference I
> > > found
> > > produced more errors. I'd like to create a list of all running
> > > processes.
> > >
> > > - name: populate running services into a list
> > > set_fact:
> > > services_running: >
> > > {% if (hostvars[inventory_hostname]['services']['{{ item
> > > }}']['state'] == 'running') %}
> > > services_running + [ '{{ item }}' ]
> > > {% endif %}
> > > with_items: "{{ hostvars[inventory_hostname]['services'].keys() }}"
> > >

> > Try this one
> >
> > - name: populate running services into a list
> > set_fact:
> > services_running: "{{ services_running|default([]) + [ item ] }}"
> > loop: "{{ services.keys() }}"
> > when: services[item].state == 'running'

On Mon, 12 Aug 2019 17:58:58 -0500
Amos <a.g...@gmail.com> wrote:
> seems close, but getting:
>
> msg: 'Invalid data passed to ''loop'', it requires a list, got this
> instead: dict_keys([''abrt-xorg.service'', ''sshd-keygen.service'',
> ''systemd-machine-id-commit.service'', ''iprinit.servic
> e'', ''systemd-readahead-collect.service'', ''puppet.service'',
> ''plymouth-kexec.service'', ''cgdcbxd.service'', ''console-shell.service'',
> ''rngd.service'', ''sssd-autofs.service'', .....

It works for me with Ansible 2.7.9 and Ubuntu 18.04. YMMV.
https://github.com/vbotka/ansible-examples/blob/master/examples/example-009/service_facts_03.yml

Amos

unread,
Aug 13, 2019, 1:25:51 PM8/13/19
to Vladimir Botka, ansible...@googlegroups.com
This works for me:

    - name: populate service facts
      service_facts:


    - name: populate running services into a list
      set_fact:
        services_running: "{{ services_running|default([]) + [ item ] }}"
      loop: "{{ services.keys() | flatten(1) }}"

      when: services[item].state == 'running'

This is with Ansible 2.8.0 on RHEL 7.6

flowerysong

unread,
Aug 13, 2019, 2:09:56 PM8/13/19
to Ansible Project
That's a very roundabout approach.

- set_fact:
    services_running
:
"{{ services | dict2items | selectattr('value.state', 'equalto', 'running') | map(attribute='key') | list }}"

Vladimir Botka

unread,
Aug 13, 2019, 3:28:51 PM8/13/19
to Amos, ansible...@googlegroups.com
On Tue, 13 Aug 2019 12:25:29 -0500
Amos <a.g...@gmail.com> wrote:
> This works for me:
>
> - name: populate service facts
> service_facts:
>
> - name: populate running services into a list
> set_fact:
> services_running: "{{ services_running|default([]) + [ item ] }}"
> loop: "{{ services.keys() | flatten(1) }}"
> when: services[item].state == 'running'
>
> This is with Ansible 2.8.0 on RHEL 7.6

Right. Next option might be the filters below

- set_fact:
srv_running: "{{ services|dict2items|
selectattr('value.state', 'match', 'running')|
map(attribute='key')|
list }}"

Cheers,

-vlado
Reply all
Reply to author
Forward
0 new messages