# Append shell results on multiple servers to local file

**URL:** <https://forum.ansible.com/t/append-shell-results-on-multiple-servers-to-local-file/17304>\
**Category:** Ansible Project\
**Created:** [January 18, 2015, 2:04am UTC](https://forum.ansible.com/t/append-shell-results-on-multiple-servers-to-local-file/17304 "2015-01-18T02:04:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jim\_McMahon](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@Jim\_McMahon](https://forum.ansible.com/u/Jim_McMahon)\
**Post date:** [January 18, 2015, 2:04am UTC](https://forum.ansible.com/t/append-shell-results-on-multiple-servers-to-local-file/17304/1 "2015-01-18T02:04:20Z")

</div>

I’m trying to run a shell command on multiple servers and have the results appended to a local file. The way I have it written now doesn’t work. I only get the result of the last server because it runs the first task for each server before moving on to the next task. Hence I get the result of the last server in my file.

How do I get this to work with Ansible? The example here: [http://docs.ansible.com/playbooks\_loops.html](http://docs.ansible.com/playbooks_loops.html) doesn’t really fit my situation because I want the loop to be on inventory hosts.

---

<div class="post-metadata">

**Author:** ![Tom\_Bamford](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@Tom\_Bamford](https://forum.ansible.com/u/Tom_Bamford)\
**Post date:** [January 18, 2015, 4:36am UTC](https://forum.ansible.com/t/append-shell-results-on-multiple-servers-to-local-file/17304/2 "2015-01-18T04:36:34Z")

</div>

Could you perhaps save the results into different files on the local machine (such as `/tmp/local_action.{{ inventory_hostname }}`), then assemble them into a single file as a subsequent task? There is an [assemble module](http://docs.ansible.com/assemble_module.html) that may do the job.

​

---

<div class="post-metadata">

**Author:** ![Jim\_McMahon](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@Jim\_McMahon](https://forum.ansible.com/u/Jim_McMahon)\
**Post date:** [January 18, 2015, 4:40am UTC](https://forum.ansible.com/t/append-shell-results-on-multiple-servers-to-local-file/17304/3 "2015-01-18T04:40:09Z")

</div>

Thanks Tom. That’s what I ended up doing. Unfortunately, I had to run the ansible playbook once for each server. I really wanted to run the playbook in one shot against all servers. I could have just created a for loop using ssh to do it, but I really wanted to see if I could get it to work with Ansible.

Jim McMahon

---

<div class="post-metadata">

**Author:** ![Tom\_Bamford](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@Tom\_Bamford](https://forum.ansible.com/u/Tom_Bamford)\
**Post date:** [January 18, 2015, 4:50am UTC](https://forum.ansible.com/t/append-shell-results-on-multiple-servers-to-local-file/17304/4 "2015-01-18T04:50:36Z")

</div>

Hi Jim

I’m sure ansible could accomplish this for you with a single playbook run. Could you perhaps post your entire playbook?

---

<div class="post-metadata">

**Author:** ![Jim\_McMahon](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@Jim\_McMahon](https://forum.ansible.com/u/Jim_McMahon)\
**Post date:** [January 18, 2015, 5:51am UTC](https://forum.ansible.com/t/append-shell-results-on-multiple-servers-to-local-file/17304/5 "2015-01-18T05:51:01Z")

</div>

Tom,

I put the playbook up on GitHub. You can find it here: [https://github.com/jmcmahon/legacy\_ec2](https://github.com/jmcmahon/legacy_ec2). A link to the ec2-metadata script is in the README.md file.

Thanks,

Jim McMahon

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [January 19, 2015, 2:48pm UTC](https://forum.ansible.com/t/append-shell-results-on-multiple-servers-to-local-file/17304/6 "2015-01-19T14:48:11Z")

</div>

try something like this:  
[https://gist.github.com/bcoca/f72d602c78943d70132c](https://gist.github.com/bcoca/f72d602c78943d70132c)

it uses set\_fact to keep the data per host and then a template to  
iterate over the hosts and print their facts in same file
