Clean up debug output

113 views
Skip to first unread message

Thomas Stephen Lee

unread,
Dec 23, 2021, 3:53:54 AM12/23/21
to Ansible Project Mailing List
Hi,

I have a small Ansible playbook.

-----------------%<-----------------
- hosts: localhost
  tasks:
    - name: Populate service facts
      ansible.builtin.service_facts:

    - name: Print service facts
      loop:
      - postfix
      - rsyslog
      - sshd
      ansible.builtin.debug:
        var: ansible_facts.services['{{ item }}.service']['state']
-----------------%<-----------------

The output is

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

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

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

TASK [Populate service facts] **************************************************
ok: [localhost]

TASK [Print service facts] *****************************************************
ok: [localhost] => (item=postfix) => {
    "ansible_facts.services['postfix.service']['state']": "running",
    "ansible_loop_var": "item",
    "item": "postfix"
}
ok: [localhost] => (item=rsyslog) => {
    "ansible_facts.services['rsyslog.service']['state']": "running",
    "ansible_loop_var": "item",
    "item": "rsyslog"
}
ok: [localhost] => (item=sshd) => {
    "ansible_facts.services['sshd.service']['state']": "running",
    "ansible_loop_var": "item",
    "item": "sshd"
}

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

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

What I want is

TASK [Print service facts]

should print only 

"ansible_facts.services['rsyslog.service']['state']": "running"

The other three should be hidden.

Any easy trick for this ?

Thanks

---
Lee

Vladimir Botka

unread,
Dec 23, 2021, 5:36:13 AM12/23/21
to Thomas Stephen Lee, ansible...@googlegroups.com
On Thu, 23 Dec 2021 14:22:00 +0530
Thomas Stephen Lee <lee....@gmail.com> wrote:

> - hosts: localhost
> tasks:
> - name: Populate service facts
> ansible.builtin.service_facts:
>
> - name: Print service facts
> loop:
> - postfix
> - rsyslog
> - sshd
> ansible.builtin.debug:
> var: ansible_facts.services['{{ item }}.service']['state']
> ...
> should print only
>
> "ansible_facts.services['rsyslog.service']['state']": "running"

Try

- name: Print service facts
ansible.builtin.debug:
msg: '{{ _sr }}: {{ _st }}'
loop:
- postfix
- rsyslog
- sshd
vars:
_sr: 'ansible_facts.services["{{ item }}.service"]["state"]'
_st: '{{ ansible_facts.services[item ~ ".service"].state }}'

The single/double quotes of the results are reversed. If this is a
problem you'll have to escape them.

ok: [localhost] => (item=sshd) =>
msg: 'ansible_facts.services["sshd.service"]["state"]: active'

--
Vladimir Botka

Thomas Stephen Lee

unread,
Dec 23, 2021, 7:00:09 AM12/23/21
to Vladimir Botka, Ansible Project Mailing List
Thanks a lot, Vladimir
That works.

Thanks

---
Lee
Reply all
Reply to author
Forward
0 new messages