Append shell results on multiple servers to local file

4,607 views
Skip to first unread message

Jim McMahon

unread,
Jan 17, 2015, 9:04:20 PM1/17/15
to ansible...@googlegroups.com
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 doesn't really fit my situation because I want the loop to be on inventory hosts.


---

- name: get metadata on legacy servers
  shell: > 
    hostname; len=`hostname | wc -c` ; export len; perl -E 'say "=" x ($ENV{"len"} - 1)' ;
    ec2-metadata -i -a -z -s -v
  register: result
  tags: 
    - legacy

- name: copy results to local file
  local_action: copy > 
    content="{{ result.stdout }}"
    dest="/tmp/local_action"
  tags: 
    - legacy

Tom Bamford

unread,
Jan 17, 2015, 11:36:59 PM1/17/15
to ansible...@googlegroups.com

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 that may do the job.


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f980940c-bf3f-416f-896a-d258519e2d6d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Jim McMahon

unread,
Jan 17, 2015, 11:40:14 PM1/17/15
to ansible...@googlegroups.com
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
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/bpjc0Hgw3so/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

Tom Bamford

unread,
Jan 17, 2015, 11:50:59 PM1/17/15
to ansible...@googlegroups.com
Hi Jim

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


Jim McMahon

unread,
Jan 18, 2015, 12:51:54 AM1/18/15
to ansible...@googlegroups.com
Tom,

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

Thanks,

Jim McMahon 


Brian Coca

unread,
Jan 19, 2015, 9:48:14 AM1/19/15
to ansible...@googlegroups.com
try something like this:
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

--
Brian Coca
Reply all
Reply to author
Forward
0 new messages