ActiveState of a systemd service

1,063 views
Skip to first unread message

Thomas Stephen Lee

unread,
Dec 27, 2021, 1:32:02 AM12/27/21
to Ansible Project Mailing List
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
tasks:
- name: Get Service Status
ansible.builtin.systemd:
name: '{{ item }}.service'
register: hello
loop:
- postfix
- rsyslog
- sshd
- debug:
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

---
Lee

Dick Visser

unread,
Dec 27, 2021, 3:18:06 AM12/27/21
to ansible...@googlegroups.com
Can you post the unfiltered variable hello

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAG7s96V2RgCu6LrKt06DKigdH-_DA0n%2Bp8Tsv9s6UB4205__BQ%40mail.gmail.com.

--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Marian Saldhana

unread,
Dec 27, 2021, 4:18:48 AM12/27/21
to ansible...@googlegroups.com
You can try something like this if it suits your requirements.
+++

- name: Get ypbind service status

  ansible.builtin.systemd:

    name: "ypbind"

  register: ypbind_status

 

- debug:

    var: ypbind_status.status.ActiveState

 

- name: Get smbd service status

  ansible.builtin.systemd:

    name: "smbd"

  register: smbd_status

 

- debug:

    var: smbd_status.status.ActiveState


+++


Output

+++


 

TASK [authentication_service_tab3 : debug] **********************************************************************************************

ok: [192.168.43.237] => {

    "ypbind_status.status.ActiveState": "inactive"

}

ok: [192.168.43.6] => {

    "ypbind_status.status.ActiveState": "inactive"

}

 


Thomas Stephen Lee

unread,
Dec 27, 2021, 5:21:31 AM12/27/21
to Ansible Project Mailing List
Hi @Dick Visser,

https://pastebin.com/Z9KCDJhf

Thanks

---
Lee

On Mon, Dec 27, 2021 at 1:48 PM Dick Visser <dick....@geant.org> wrote:
>
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAL8fbwOSnpe%3D_zn4fxm5X7Sy3ofcHDdrUWMjm5YnvEM%3DZeAtNQ%40mail.gmail.com.

Thomas Stephen Lee

unread,
Dec 27, 2021, 5:21:47 AM12/27/21
to Ansible Project Mailing List
Hi @Marian Saldhana,

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

Thanks
---
Lee
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CANDC7%2BDEW9-dG1fcjS-T2RE4%3DY6U3-nGs%2BNvoh8pDXb0uy788A%40mail.gmail.com.

Todd Lewis

unread,
Dec 27, 2021, 11:53:21 AM12/27/21
to Ansible Project
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}') }}"

Todd Lewis

unread,
Dec 27, 2021, 11:55:44 AM12/27/21
to Ansible Project
which produces:
TASK [Dump ActiveState] **********
ok: [localhost] => {
    "msg": [
        {
            "name": "postfix.service",
            "state": "active"
        },
        {
            "name": "rsyslog.service",
            "state": "active"
        },
        {
            "name": "sshd.service",
            "state": "active"
        }
    ]
}

Dick Visser

unread,
Dec 27, 2021, 3:07:47 PM12/27/21
to ansible...@googlegroups.com
On Mon, 27 Dec 2021 at 07:32, Thomas Stephen Lee <lee....@gmail.com> wrote:

> - debug:
> msg: "{{ hello.results | to_nice_json |
> json_query('status.ActiveState') }}"

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


- debug: var=results|json_query('[].status.ActiveState')




--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Thomas Stephen Lee

unread,
Dec 27, 2021, 9:31:10 PM12/27/21
to Ansible Project Mailing List
Hi @Todd Lewis,

That works.

Thanks a lot.

---
Lee
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/b7f79cd9-8356-481a-aad4-ee8d3df60c92n%40googlegroups.com.

Thomas Stephen Lee

unread,
Dec 27, 2021, 9:31:18 PM12/27/21
to Ansible Project Mailing List
Hi @Dick Visser,

That gives the below output.

-----------------%<-----------------

TASK [debug] *******************************************************************
ok: [localhost] => {
"results|json_query('[].status.ActiveState')": ""
}

-----------------%<-----------------


Thanks

---
Lee
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAL8fbwNzq_4RhS4rtsTxHhWbPq8SRBMM3_1hpV30mHMFyp6sFA%40mail.gmail.com.

Dick Visser

unread,
Dec 28, 2021, 6:27:03 AM12/28/21
to ansible...@googlegroups.com
I meant:

- debug: var=hello|json_query('results[].status.ActiveState')
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAG7s96VGmsZ8OEhoNFtuEr4q1dmrK0pd6pKGpsspwCi4WQgjxg%40mail.gmail.com.

Thomas Stephen Lee

unread,
Dec 28, 2021, 6:58:25 AM12/28/21
to Ansible Project Mailing List
Hi @Dick Visser,

That gives the below output.

-----------------%<-----------------

TASK [debug] *******************************************************************
ok: [localhost] => {
"hello|json_query('results[].status.ActiveState')": [
"active",
"active",
"active"
]
}

-----------------%<-----------------

I will explore more, thanks a lot.

---
Lee
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAL8fbwOQZTegyfbzNwr1gZY0i_6g_4femX91-k7uAsqx3R8qGA%40mail.gmail.com.

Dick Visser

unread,
Dec 28, 2021, 7:30:14 AM12/28/21
to ansible...@googlegroups.com
I believe this is what you wanted - right?
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAG7s96VmOhOVO7xMdFGnZ_WnfeDZV36LwZNn1OLwG%2BVp%3Duhcqw%40mail.gmail.com.

Thomas Stephen Lee

unread,
Dec 29, 2021, 12:30:44 AM12/29/21
to Ansible Project Mailing List
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

-----------------%<-----------------
debug:
msg: "{{ hello.results |
json_query('[?status.ActiveState!=`active`].{name: name, state:
status.ActiveState}') | to_nice_json }}"
-----------------%<-----------------

Thanks a lot.

---
Lee
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAL8fbwMn%2Bh%3D_f-aGUn9SmJCaYkKkjaKO6zN%2BJqN1gmWUcbAhWw%40mail.gmail.com.

Thomas Stephen Lee

unread,
Jan 25, 2022, 2:32:08 AM1/25/22
to Ansible Project Mailing List
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.

---
Lee

get.services.list.yml
Reply all
Reply to author
Forward
0 new messages