Clean up debug output

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

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'

Thanks a lot, Vladimir
That works.

Thanks