ActiveState of a systemd service

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

Can you post the unfiltered variable hello

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

Hi @Dick Visser,

https://pastebin.com/Z9KCDJhf

Thanks

Hi @Marian Saldhana,

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

Thanks

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

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

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

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

Hi @Dick Visser,

That gives the below output.

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

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

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

Thanks

Hi @Todd Lewis,

That works.

Thanks a lot.

I meant:

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

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.

I believe this is what you wanted - right?

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.

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 (1.61 KB)