# Fix the bug for saving Ansible ping results to a local file

**URL:** <https://forum.ansible.com/t/fix-the-bug-for-saving-ansible-ping-results-to-a-local-file/37437>\
**Category:** Ansible Project\
**Created:** [December 6, 2022, 7:53pm UTC](https://forum.ansible.com/t/fix-the-bug-for-saving-ansible-ping-results-to-a-local-file/37437 "2022-12-06T19:53:56Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank\_Ling](https://avatars.discourse-cdn.com/v4/letter/f/c67d28/32.png) [@Frank\_Ling](https://forum.ansible.com/u/Frank_Ling)\
**Post date:** [December 6, 2022, 7:53pm UTC](https://forum.ansible.com/t/fix-the-bug-for-saving-ansible-ping-results-to-a-local-file/37437/1 "2022-12-06T19:53:56Z")

</div>

I need some helps for fixing the bug of saving the Ansible ping results to a local file.

Appreciate your help.

Thanks in advance.

Frank

After running the playbook, I got this weird result:

cat /tmp/file.txt

{“ping”: “pong”, “failed”: false, “changed”: false}

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [December 6, 2022, 8:00pm UTC](https://forum.ansible.com/t/fix-the-bug-for-saving-ansible-ping-results-to-a-local-file/37437/2 "2022-12-06T20:00:21Z")

</div>

Several things here.  
You’re saving the result for each host to the same file. So you will end up with just one result, from the last host.  
And there is something weird with the inventory you use. Follow the suggestion made by the code (use increased verbosity).

---

<div class="post-metadata">

**Author:** ![Frank\_Ling](https://avatars.discourse-cdn.com/v4/letter/f/c67d28/32.png) [@Frank\_Ling](https://forum.ansible.com/u/Frank_Ling)\
**Post date:** [December 6, 2022, 8:09pm UTC](https://forum.ansible.com/t/fix-the-bug-for-saving-ansible-ping-results-to-a-local-file/37437/3 "2022-12-06T20:09:51Z")

</div>

Thanks for the reply.

> > You’re saving the result for each host to the same file.

Yeah, you’re right. I am not sure how to save the output in Ansible to the output in a file like in shell \>\> /tmp/file

> > And there is something weird with the inventory you use…  
> > I had the inventory in /etc/ansible/hosts with:

[test-vms]  
test-vm01  
test-vm02  
test-vm03

Frank

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [December 6, 2022, 8:20pm UTC](https://forum.ansible.com/t/fix-the-bug-for-saving-ansible-ping-results-to-a-local-file/37437/4 "2022-12-06T20:20:53Z")

</div>

Also, it helps when you clarify what you want to see, instead of “I got this weird result”.

What makes it weird? What were you expecting instead?

---

<div class="post-metadata">

**Author:** ![Frank\_Ling](https://avatars.discourse-cdn.com/v4/letter/f/c67d28/32.png) [@Frank\_Ling](https://forum.ansible.com/u/Frank_Ling)\
**Post date:** [December 6, 2022, 8:34pm UTC](https://forum.ansible.com/t/fix-the-bug-for-saving-ansible-ping-results-to-a-local-file/37437/5 "2022-12-06T20:34:47Z")

</div>

> > What makes it weird?  
> > I wanted to save the ansible ping results to /tmp/file.txt so I can email out the result.

After running the playbook, I just got following saved in /tmp/file.txt:

{“ping”: “pong”, “failed”: false, “changed”: false}

> > What were you expecting instead?

I added a few lines in the playbook as:

---

<div class="post-metadata">

**Author:** ![Frank\_Ling](https://avatars.discourse-cdn.com/v4/letter/f/c67d28/32.png) [@Frank\_Ling](https://forum.ansible.com/u/Frank_Ling)\
**Post date:** [December 6, 2022, 9:02pm UTC](https://forum.ansible.com/t/fix-the-bug-for-saving-ansible-ping-results-to-a-local-file/37437/6 "2022-12-06T21:02:01Z")

</div>

Well as a workaround I could use: ansible-playbook ping\_email.yml \>\> /tmp/file.txt to save the output as a file. But I hope there could be a better way.

Thanks.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [December 6, 2022, 9:04pm UTC](https://forum.ansible.com/t/fix-the-bug-for-saving-ansible-ping-results-to-a-local-file/37437/7 "2022-12-06T21:04:37Z")

</div>

Registered results do not contain the full output as seen on the screen. Perhaps you want to set a log file instead, otherwise, you’ll want to use a template and build the output in the format you want. The screen output is controlled by a callback plugin which does extra formatting.

See [https://docs.ansible.com/ansible-core/2.14/reference\_appendices/config.html#default-log-path](https://docs.ansible.com/ansible-core/2.14/reference_appendices/config.html#default-log-path)

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [December 6, 2022, 9:13pm UTC](https://forum.ansible.com/t/fix-the-bug-for-saving-ansible-ping-results-to-a-local-file/37437/8 "2022-12-06T21:13:36Z")

</div>

You could change your dest to  
dest: “/tmp/file-{{ inventory\_hostname }}.txt”  
and concatenate them all, but then they won’t contain the host names.

If you don’t like that, then you could register the ping results and loop through them with a template…

You have an invalid character in your group name, by the way. Change the dash to an underscore (probably).

---

<div class="post-metadata">

**Author:** ![Frank\_Ling](https://avatars.discourse-cdn.com/v4/letter/f/c67d28/32.png) [@Frank\_Ling](https://forum.ansible.com/u/Frank_Ling)\
**Post date:** [December 6, 2022, 9:45pm UTC](https://forum.ansible.com/t/fix-the-bug-for-saving-ansible-ping-results-to-a-local-file/37437/9 "2022-12-06T21:45:57Z")

</div>

Thanks, Matt.

> > Perhaps you want to set a log file instead…Well, I don’t want to have the whole log file that contains all details of different play, but only the output of the play.

> > you’ll want to use a template and build the output in the format you want…  
> > I will look into it.

Frank

---

<div class="post-metadata">

**Author:** ![Frank\_Ling](https://avatars.discourse-cdn.com/v4/letter/f/c67d28/32.png) [@Frank\_Ling](https://forum.ansible.com/u/Frank_Ling)\
**Post date:** [December 6, 2022, 9:49pm UTC](https://forum.ansible.com/t/fix-the-bug-for-saving-ansible-ping-results-to-a-local-file/37437/10 "2022-12-06T21:49:16Z")

</div>

It looks like I need to use a template for the task. But saving the screen output for the playbook run should be a very common task. Hope someone can write a module for doing this.

> > You have an invalid character in your group name, by the way. Change the dash to an underscore (probably).  
> > Yeah, you’re correct.

Thanks.

Frank

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [December 6, 2022, 10:20pm UTC](https://forum.ansible.com/t/fix-the-bug-for-saving-ansible-ping-results-to-a-local-file/37437/11 "2022-12-06T22:20:31Z")

</div>

- hosts: test-vms  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: Ping vms in test-vms  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible.builtin.ping:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: ping\_pong  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: True

- hosts: localhost  
&nbsp;&nbsp;gather\_facts: no  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: Copy the output result to /tmp/file.txt  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible.builtin.template:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: ping.j2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /tmp/file.txt

ping.j2:  
{% for host in groups['test-vms'] %}  
{{ host }}: {{ hostvars[host]['ping\_pong'] }}  
{% endfor %}

---

<div class="post-metadata">

**Author:** ![Frank\_Ling](https://avatars.discourse-cdn.com/v4/letter/f/c67d28/32.png) [@Frank\_Ling](https://forum.ansible.com/u/Frank_Ling)\
**Post date:** [December 7, 2022, 1:36pm UTC](https://forum.ansible.com/t/fix-the-bug-for-saving-ansible-ping-results-to-a-local-file/37437/12 "2022-12-07T13:36:09Z")

</div>

Hi Brian,

Thanks a lot for the help.

Yes, it works as expected.

I really appreciate that.

Frank
