lee
(Lee)
December 27, 2021, 6:30am
1
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.
lee
(Lee)
December 27, 2021, 10:20am
4
lee
(Lee)
December 27, 2021, 10:21am
5
Hi @Marian Saldhana,
I need a loop as I want to add more services and machines.
Thanks
utoddl
(Todd Lewis)
December 27, 2021, 4:53pm
6
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}’) }}”
utoddl
(Todd Lewis)
December 27, 2021, 4:55pm
7
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')
lee
(Lee)
December 28, 2021, 2:30am
9
Hi @Dick Visser,
That gives the below output.
-----------------%<-----------------
TASK [debug] *******************************************************************
ok: [localhost] => {
"results|json_query(' .status.ActiveState')": ""
}
-----------------%<-----------------
Thanks
Dick_Visser
(Dick Visser)
December 28, 2021, 11:26am
11
I meant:
- debug: var=hello|json_query('results .status.ActiveState')
lee
(Lee)
December 28, 2021, 11:57am
12
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.
Dick_Visser
(Dick Visser)
December 28, 2021, 12:29pm
13
I believe this is what you wanted - right?
lee
(Lee)
December 29, 2021, 5:30am
14
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
(Lee)
January 25, 2022, 7:30am
15
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)