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}') }}"


On Monday, December 27, 2021 at 1:32:02 AM UTC-5 [email protected] wrote:

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

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/a7c327fa-6441-4c77-b0bc-be2851f699b5n%40googlegroups.com.

Reply via email to