Based on what you are looking for this should do it for you.

---
> - hosts: all
>   gather_facts: true
>   remote_user: remote
>   sudo: true
>   tasks:
>     - name: capture linux_os
>       shell: "{{ item }}"
>       register: linux_os
>       with_items:
>         - 'uname -a'
>         - 'cat /etc/resolv.conf'
>     - name: creating inventory hosts
>       file: path="{{ inventory_hostname }}".txt state=touch
>       delegate_to: localhost
>       sudo: false
>     - name: results
>       lineinfile: dest="{{ inventory_hostname }}".txt line="{{ item.stdout 
> }}"
>       delegate_to: localhost
>       sudo: false
>       with_items: linux_os.results

 

resulting in.

Linux ans-test-3 3.13.0-55-generic #92-Ubuntu SMP Sun Jun 14 18:32:20 UTC 
> 2015 x86_64 x86_64 x86_64 GNU/Linux
> # Dynamic resolv.conf(5) file for glibc resolver(3) generated by 
> resolvconf(8)
> #     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
> nameserver 192.168.70.240
> nameserver 192.168.70.241
> search everythingshouldbevirtual.local


On Tuesday, July 21, 2015 at 8:50:58 AM UTC-4, pavan reddy wrote:
>
> Hi,
>
> below is the playbook.i wanna multiple commands in a single task using 
>  loop (with_items) and copy the stdout of each loop for every hosts in a 
> file.
>
>
> =======================
> ansible --version
> ansible 1.7.2
> ========================
>
> =============================================================
> ---
> - name: All the hosts
>   hosts: all
>   gather_facts: false
>   connection: paramiko
>   tasks:
>    - name: items
>      raw:  "{{ item }}"
>      register: linux_os
>      with_items:
>        - uname -a
>        - netstat -rn
>        
>    - name:  Do something with each result
>      local_action: template src= {{ item.stdout }} 
> dest=/home/openstack/Documents/{{inventory_hostname}}.txt
>       with_items: "{{ linux_os.stdout_lines }}"
>
>
> ====================================================================    
>
>
> Output:
>
> PLAY [All the hosts] 
> **********************************************************
>
> TASK: [sh clock] 
> **************************************************************
> ok: [192.168.1.191] => (item=uname -a)
> ok: [192.168.1.191] => (item=cat /etc/resolv.conf)
>
> TASK: [debug var=linux_os] 
> ****************************************************
> ok: [192.168.1.191] => {
>     "linux_os": {
>         "changed": false,
>         "msg": "All items completed",
>         "results": [
>             {
>                 "invocation": {
>                     "module_args": "uname -a",
>                     "module_name": "raw"
>                 },
>                 "item": "uname -a",
>                 "rc": 0,
>                 "stderr": "",
>                 "stdout": "Linux splunk-server 3.2.0-65-generic #98-Ubuntu 
> SMP Wed Jun 11 20:27:07 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux\n"
>             },
>             {
>                 "invocation": {
>                     "module_args": "cat /etc/resolv.conf",
>                     "module_name": "raw"
>                 },
>                 "item": "cat /etc/resolv.conf",
>                 "rc": 0,
>                 "stderr": "",
>                 "stdout": "# Dynamic resolv.conf(5) file for glibc 
> resolver(3) generated by resolvconf(8)\n#     DO NOT EDIT THIS FILE BY HAND 
> -- YOUR CHANGES WILL BE OVERWRITTEN\nnameserver 127.0.0.1\nsearch 
> localdomain\n"
>             }
>         ]
>     }
> }
>
>
> ==========
> Error:
>
> TASK: [copy] 
> ******************************************************************
> fatal: [192.168.1.191 -> 127.0.0.1] => {'msg': 'AnsibleError: unable to 
> read /home/openstack/Documents/uname', 'failed': True}
> fatal: [192.168.1.191 -> 127.0.0.1] => {'msg': 'One or more items 
> failed.', 'failed': True, 'changed': False, 'results': [{'msg': 
> 'AnsibleError: unable to read /home/openstack/Documents/uname', 'failed': 
> True}]}
>
> FATAL: all hosts have already failed -- aborting
>
> PLAY RECAP 
> ********************************************************************
>            to retry, use: --limit @/root/test.retry
>
> 192.168.1.191              : ok=2    changed=0    unreachable=1    failed=0
>
>
>
> ----------------------------------------------------------------------------------------------
>
> I wanna output as:
>
> Linux splunk-server 3.2.0-65-generic #98-Ubuntu SMP Wed Jun 11 20:27:07 
> UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
> # Dynamic resolv.conf(5) file for glibc resolver(3) generated by 
> resolvconf(8)
> #     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
> nameserver 127.0.0.1
> search localdomain"
>
> --------------------------------------------------------
>
> Thanks 
> Pavan
>
>

-- 
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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/6bb777c6-387e-43c8-b52e-9dc8953a2053%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to