# Ansible set\_fact with with\_items always prints last item

**URL:** <https://forum.ansible.com/t/ansible-set-fact-with-with-items-always-prints-last-item/33848>\
**Category:** Ansible Project\
**Created:** [September 19, 2020, 10:29am UTC](https://forum.ansible.com/t/ansible-set-fact-with-with-items-always-prints-last-item/33848 "2020-09-19T10:29:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kumaresan\_M](https://avatars.discourse-cdn.com/v4/letter/k/e19b73/32.png) [@Kumaresan\_M](https://forum.ansible.com/u/Kumaresan_M)\
**Post date:** [September 19, 2020, 10:29am UTC](https://forum.ansible.com/t/ansible-set-fact-with-with-items-always-prints-last-item/33848/1 "2020-09-19T10:29:51Z")

</div>

Hi Experts,

I am looking to capture certain service status using ansible the problem is debug prints only last item.Below is the expected output.

Hostname : test

httpd → Running  
ntpd → Running

Below are the playbook.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 19, 2020, 1:58pm UTC](https://forum.ansible.com/t/ansible-set-fact-with-with-items-always-prints-last-item/33848/2 "2020-09-19T13:58:20Z")

</div>

> Hi Experts,
> 
> I am looking to capture certain service status using ansible the problem is debug prints only last item.Below is the  
> expected output.
> 
> Hostname : test
> 
> httpd --\> Running  
> ntpd --\> Running
> 
> Below are the playbook.
> 
> ---  
> - hosts: test  
> become: true  
> gather\_facts: false  
> tasks:  
> - name: Service facts  
> service\_facts:
> 
> ```
> \- set\_fact:
> elk\_app1: &quot;\{\{ ansible\_facts\.services\[item \+ &#39;\.service&#39;\]\.name\}\} Running&quot;
> when: ansible\_facts\.services\[item \+ &#39;\.service&#39;\]\.state == &#39;running&#39;
> with\_items:
> \- httpd
> \- ntpd
> register: foo\_result
> 
> \- debug: msg=&quot;\{\{ foo\_result \}\}&quot;
> 
> ```
> 
> Regards  
> Kumar

Hello Kumar,

if you use set\_fact with a loop, you need to reference the variable on the right hand side:

set\_fact:  
&nbsp;&nbsp;elk\_app1: "{{ elk\_app1 | default('') + ansible\_facts.services[item + '.service'].name + ' Running\n' }}"

The default filter is necessary as the variable is undefined on the first iteration of the loop.

Regards  
&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Kumaresan\_M](https://avatars.discourse-cdn.com/v4/letter/k/e19b73/32.png) [@Kumaresan\_M](https://forum.ansible.com/u/Kumaresan_M)\
**Post date:** [September 21, 2020, 5:12am UTC](https://forum.ansible.com/t/ansible-set-fact-with-with-items-always-prints-last-item/33848/3 "2020-09-21T05:12:28Z")

</div>

Hi Brae,

Thanks for the help was able to get the desired output.But the output are in same line is there way to print new line for each service?

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [test] =\> {  
“msg”: “httpd.service Running\nntpd.service Running\n”  
}

Expected output:

httpd.service Running  
ntpd.service Running

Regards  
Kumar

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 21, 2020, 6:29am UTC](https://forum.ansible.com/t/ansible-set-fact-with-with-items-always-prints-last-item/33848/4 "2020-09-21T06:29:30Z")

</div>

> Hi Brae,
> 
> Thanks for the help was able to get the desired output.But the output are in same line is there way to print new line  
> for each service?
> 
> TASK [debug]  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [test] =\> {  
> "msg": "httpd.service Running\nntpd.service Running\n"  
> }
> 
> Expected output:
> 
> httpd.service Running  
> ntpd.service Running
> 
> Regards  
> Kumar

Hello Kumar,

use the YAML callback plugin, e.g in ansible.cfg:

# Use the YAML callback plugin.  
stdout\_callback = yaml

This changes the output to look like as follows:

ok: [buster-test-box] =\>  
&nbsp;&nbsp;elk\_app1: |-  
&nbsp;&nbsp;&nbsp;&nbsp;sympa.service Running  
&nbsp;&nbsp;&nbsp;&nbsp;wwsympa.service Running

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
