# how to stuff running services into a host variable?

**URL:** <https://forum.ansible.com/t/how-to-stuff-running-services-into-a-host-variable/31024>\
**Category:** Ansible Project\
**Created:** [August 12, 2019, 7:57pm UTC](https://forum.ansible.com/t/how-to-stuff-running-services-into-a-host-variable/31024 "2019-08-12T19:57:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amos](https://avatars.discourse-cdn.com/v4/letter/a/ebca7d/32.png) [@Amos](https://forum.ansible.com/u/Amos)\
**Post date:** [August 12, 2019, 7:57pm UTC](https://forum.ansible.com/t/how-to-stuff-running-services-into-a-host-variable/31024/1 "2019-08-12T19:57:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 12, 2019, 8:26pm UTC](https://forum.ansible.com/t/how-to-stuff-running-services-into-a-host-variable/31024/2 "2019-08-12T20:26:19Z")

</div>

Try this one

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: populate running services into a list  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;services\_running: "{{ services\_running|default() + [item] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ services.keys() }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: services[item].state == 'running'

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Amos](https://avatars.discourse-cdn.com/v4/letter/a/ebca7d/32.png) [@Amos](https://forum.ansible.com/u/Amos)\
**Post date:** [August 12, 2019, 10:58pm UTC](https://forum.ansible.com/t/how-to-stuff-running-services-into-a-host-variable/31024/3 "2019-08-12T22:58:58Z")

</div>

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’’, …

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 12, 2019, 11:26pm UTC](https://forum.ansible.com/t/how-to-stuff-running-services-into-a-host-variable/31024/4 "2019-08-12T23:26:46Z")

</div>

> \> \> 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'

> seems close, but getting:
> 
> &nbsp;&nbsp;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](https://github.com/vbotka/ansible-examples/blob/master/examples/example-009/service_facts_03.yml)

---

<div class="post-metadata">

**Author:** ![Amos](https://avatars.discourse-cdn.com/v4/letter/a/ebca7d/32.png) [@Amos](https://forum.ansible.com/u/Amos)\
**Post date:** [August 13, 2019, 5:25pm UTC](https://forum.ansible.com/t/how-to-stuff-running-services-into-a-host-variable/31024/5 "2019-08-13T17:25:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![flowerysong1](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@flowerysong1](https://forum.ansible.com/u/flowerysong1)\
**Post date:** [August 13, 2019, 6:09pm UTC](https://forum.ansible.com/t/how-to-stuff-running-services-into-a-host-variable/31024/6 "2019-08-13T18:09:56Z")

</div>

That’s a very roundabout approach.

`

- set\_fact:  
services\_running: “{{ services | dict2items | selectattr(‘value.state’, ‘equalto’, ‘running’) | map(attribute=‘key’) | list }}”  
`

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 13, 2019, 7:28pm UTC](https://forum.ansible.com/t/how-to-stuff-running-services-into-a-host-variable/31024/7 "2019-08-13T19:28:38Z")

</div>

Right. Next option might be the filters below

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;srv\_running: "{{ services|dict2items|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selectattr('value.state', 'match', 'running')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map(attribute='key')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list }}"

Cheers,

&nbsp;&nbsp;-vlado
