# ActiveState of a systemd service

**URL:** <https://forum.ansible.com/t/activestate-of-a-systemd-service/36156>\
**Category:** Ansible Project\
**Created:** [December 27, 2021, 6:30am UTC](https://forum.ansible.com/t/activestate-of-a-systemd-service/36156 "2021-12-27T06:30:00Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![lee](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lee/32/4857_2.png) [@lee](https://forum.ansible.com/u/lee)\
**Post date:** [December 27, 2021, 6:30am UTC](https://forum.ansible.com/t/activestate-of-a-systemd-service/36156/1 "2021-12-27T06:30:00Z")

</div>

Hi,

I am trying to write a playbook that displays the ActiveState of some  
services on a machine. I tried various tips from google but could not  
get the information I wanted, which is the "service.ActiveState" .  
Below is the bare minimum script.

-----------------%\<-----------------

- hosts: localhost  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;- name: Get Service Status  
&nbsp;&nbsp;&nbsp;&nbsp;ansible.builtin.systemd:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: '{{ item }}.service'  
&nbsp;&nbsp;&nbsp;&nbsp;register: hello  
&nbsp;&nbsp;&nbsp;&nbsp;loop:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- postfix  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- rsyslog  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- sshd  
&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ hello.results | to\_nice\_json |  
json\_query('status.ActiveState') }}"

-----------------%\<-----------------

The output is

-----------------%\<-----------------

PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [Get Service Status] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item=postfix)  
ok: [localhost] =\> (item=rsyslog)  
ok: [localhost] =\> (item=sshd)

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=3 changed=0 unreachable=0  
failed=0 skipped=0 rescued=0 ignored=0

-----------------%\<-----------------

Kindly advice

Thanks

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [December 27, 2021, 8:17am UTC](https://forum.ansible.com/t/activestate-of-a-systemd-service/36156/2 "2021-12-27T08:17:46Z")

</div>

Can you post the unfiltered variable hello

---

<div class="post-metadata">

**Author:** ![Marian\_Saldhana](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Marian\_Saldhana](https://forum.ansible.com/u/Marian_Saldhana)\
**Post date:** [December 27, 2021, 9:18am UTC](https://forum.ansible.com/t/activestate-of-a-systemd-service/36156/3 "2021-12-27T09:18:26Z")

</div>

You can try something like this if it suits your requirements.

---

<div class="post-metadata">

**Author:** ![lee](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lee/32/4857_2.png) [@lee](https://forum.ansible.com/u/lee)\
**Post date:** [December 27, 2021, 10:20am UTC](https://forum.ansible.com/t/activestate-of-a-systemd-service/36156/4 "2021-12-27T10:20:00Z")

</div>

Hi @Dick Visser,

[https://pastebin.com/Z9KCDJhf](https://pastebin.com/Z9KCDJhf)

Thanks

---

<div class="post-metadata">

**Author:** ![lee](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lee/32/4857_2.png) [@lee](https://forum.ansible.com/u/lee)\
**Post date:** [December 27, 2021, 10:21am UTC](https://forum.ansible.com/t/activestate-of-a-systemd-service/36156/5 "2021-12-27T10:21:00Z")

</div>

Hi @Marian Saldhana,

I need a loop as I want to add more services and machines.

Thanks

---

<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:** [December 27, 2021, 4:53pm UTC](https://forum.ansible.com/t/activestate-of-a-systemd-service/36156/6 "2021-12-27T16:53:21Z")

</div>

You probably want something like this:

- hosts: localhost  
tasks:

- name: Get Service Status  
ansible.builtin.systemd:  
name: ‘{{ item }}.service’  
register: hello  
loop:

- postfix

- rsyslog

- sshd

- name: Dump ActiveState  
debug:  
msg: “{{ hello.results | json\_query(‘[\*].{name: name, state: status.ActiveState}’) }}”

---

<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:** [December 27, 2021, 4:55pm UTC](https://forum.ansible.com/t/activestate-of-a-systemd-service/36156/7 "2021-12-27T16:55:44Z")

</div>

which produces:  
TASK [Dump ActiveState] \*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: [  
{  
“name”: “postfix.service”,  
“state”: “active”  
},  
{  
“name”: “rsyslog.service”,  
“state”: “active”  
},  
{  
“name”: “sshd.service”,  
“state”: “active”  
}  
]  
}

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [December 27, 2021, 8:07pm UTC](https://forum.ansible.com/t/activestate-of-a-systemd-service/36156/8 "2021-12-27T20:07:21Z")

</div>

the result var is a list, so if you want a list of a specific key:

- debug: var=results|json\_query('.status.ActiveState')

---

<div class="post-metadata">

**Author:** ![lee](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lee/32/4857_2.png) [@lee](https://forum.ansible.com/u/lee)\
**Post date:** [December 28, 2021, 2:30am UTC](https://forum.ansible.com/t/activestate-of-a-systemd-service/36156/9 "2021-12-28T02:30:00Z")

</div>

Hi @Dick Visser,

That gives the below output.

-----------------%\<-----------------

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"results|json\_query('.status.ActiveState')": ""  
}

-----------------%\<-----------------

Thanks

---

<div class="post-metadata">

**Author:** ![lee](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lee/32/4857_2.png) [@lee](https://forum.ansible.com/u/lee)\
**Post date:** [December 28, 2021, 2:30am UTC](https://forum.ansible.com/t/activestate-of-a-systemd-service/36156/10 "2021-12-28T02:30:00Z")

</div>

Hi @Todd Lewis,

That works.

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [December 28, 2021, 11:26am UTC](https://forum.ansible.com/t/activestate-of-a-systemd-service/36156/11 "2021-12-28T11:26:41Z")

</div>

I meant:

- debug: var=hello|json\_query('results.status.ActiveState')

---

<div class="post-metadata">

**Author:** ![lee](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lee/32/4857_2.png) [@lee](https://forum.ansible.com/u/lee)\
**Post date:** [December 28, 2021, 11:57am UTC](https://forum.ansible.com/t/activestate-of-a-systemd-service/36156/12 "2021-12-28T11:57:00Z")

</div>

Hi @Dick Visser,

That gives the below output.

-----------------%\<-----------------

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"hello|json\_query('results.status.ActiveState')": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"active",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"active",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"active"  
&nbsp;&nbsp;&nbsp;&nbsp;]  
}

-----------------%\<-----------------

I will explore more, thanks a lot.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [December 28, 2021, 12:29pm UTC](https://forum.ansible.com/t/activestate-of-a-systemd-service/36156/13 "2021-12-28T12:29:51Z")

</div>

I believe this is what you wanted - right?

---

<div class="post-metadata">

**Author:** ![lee](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lee/32/4857_2.png) [@lee](https://forum.ansible.com/u/lee)\
**Post date:** [December 29, 2021, 5:30am UTC](https://forum.ansible.com/t/activestate-of-a-systemd-service/36156/14 "2021-12-29T05:30:00Z")

</div>

Hi @Dick Visser,

In my code, I wrote only for the state value, but I also wanted the  
name value of the service.  
I thought I would figure out the name part once I get the state part working.

My current status is

-----------------%\<-----------------  
&nbsp;&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ hello.results |  
json\_query('[?status.ActiveState!=`active`].{name: name, state:  
status.ActiveState}') | to\_nice\_json }}"  
-----------------%\<-----------------

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![lee](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lee/32/4857_2.png) [@lee](https://forum.ansible.com/u/lee)\
**Post date:** [January 25, 2022, 7:30am UTC](https://forum.ansible.com/t/activestate-of-a-systemd-service/36156/15 "2022-01-25T07:30:00Z")

</div>

Hi,

My current status is attached.  
Very hackish and not 100% Ansible.  
It takes 1/2 hour to complete.  
Any suggestions are welcome.

Thanks a lot.

> **(attachments)**
>
> [get.services.list.yml](https://forum.ansible.com/uploads/short-url/5sSNOgw4CcYqEq3nFUOAnbpZsK.yml) (1.61 KB)
