Ansible with registers using loop

149 views
Skip to first unread message

pavan reddy

unread,
Jul 21, 2015, 8:50:58 AM7/21/15
to ansible...@googlegroups.com
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

Larry Smith

unread,
Jul 22, 2015, 5:03:58 PM7/22/15
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages