2015-04-14 10 views
11

Ho due compiti ansible come seguevariabili di registro in loop in un Ansible Playbook

tasks: 
- shell: ifconfig -a | sed 's/[ \t].*//;/^\(lo\|\)$/d' 
    register: var1 
- debug: var=var1 

- shell: ethtool -i {{ item }} | grep bus-info | cut -b 16-22 
    with_items: var1.stdout_lines 
    register: var2 
- debug: var=var2 

che viene utilizzato per ottenere un elenco di interfacce in una macchina (linux) e ottenere l'indirizzo del bus per ciascuno. Ho un altro compito come segue in tha stesso playbook

- name: Binding the interfaces 
    shell: echo {{ item.item }} 
    with_flattened: var2.results 
    register: var3 

che mi aspetto di iterare valore da var2 e quindi stampare i numeri di bus.

var2.results è la seguente

"var2": { 
    "changed": true, 
    "msg": "All items completed", 
    "results": [ 
     { 
      "changed": true, 
      "cmd": "ethtool -i br0: | grep bus-info | cut -b 16-22", 
      "delta": "0:00:00.005778", 
      "end": "2015-04-14 20:29:47.122203", 
      "invocation": { 
       "module_args": "ethtool -i br0: | grep bus-info | cut -b 16-22", 
       "module_name": "shell" 
      }, 
      "item": "br0:", 
      "rc": 0, 
      "start": "2015-04-14 20:29:47.116425", 
      "stderr": "", 
      "stdout": "", 
      "warnings": [] 
     }, 
     { 
      "changed": true, 
      "cmd": "ethtool -i enp13s0: | grep bus-info | cut -b 16-22", 
      "delta": "0:00:00.005862", 
      "end": "2015-04-14 20:29:47.359749", 
      "invocation": { 
       "module_args": "ethtool -i enp13s0: | grep bus-info | cut -b 16-22", 
       "module_name": "shell" 
      }, 
      "item": "enp13s0:", 
      "rc": 0, 
      "start": "2015-04-14 20:29:47.353887", 
      "stderr": "", 
      "stdout": "0d:00.0", 
      "warnings": [] 
     }, 
     { 
      "changed": true, 
      "cmd": "ethtool -i enp14s0: | grep bus-info | cut -b 16-22", 
      "delta": "0:00:00.005805", 
      "end": "2015-04-14 20:29:47.576674", 
      "invocation": { 
       "module_args": "ethtool -i enp14s0: | grep bus-info | cut -b 16-22", 
       "module_name": "shell" 
      }, 
      "item": "enp14s0:", 
      "rc": 0, 
      "start": "2015-04-14 20:29:47.570869", 
      "stderr": "", 
      "stdout": "0e:00.0", 
      "warnings": [] 
     }, 
     { 
      "changed": true, 
      "cmd": "ethtool -i enp15s0: | grep bus-info | cut -b 16-22", 
      "delta": "0:00:00.005873", 
      "end": "2015-04-14 20:29:47.875058", 
      "invocation": { 
       "module_args": "ethtool -i enp15s0: | grep bus-info | cut -b 16-22", 
       "module_name": "shell" 
      }, 
      "item": "enp15s0:", 
      "rc": 0, 
      "start": "2015-04-14 20:29:47.869185", 
      "stderr": "", 
      "stdout": "0f:00.0", 
      "warnings": [] 
     }, 
     { 
      "changed": true, 
      "cmd": "ethtool -i enp5s0f1: | grep bus-info | cut -b 16-22", 
      "delta": "0:00:00.005870", 
      "end": "2015-04-14 20:29:48.112027", 
      "invocation": { 
       "module_args": "ethtool -i enp5s0f1: | grep bus-info | cut -b 16-22", 
       "module_name": "shell" 
      }, 
      "item": "enp5s0f1:", 
      "rc": 0, 
      "start": "2015-04-14 20:29:48.106157", 
      "stderr": "", 
      "stdout": "05:00.1", 
      "warnings": [] 
     }, 
     { 
      "changed": true, 
      "cmd": "ethtool -i enp5s0f2: | grep bus-info | cut -b 16-22", 
      "delta": "0:00:00.005863", 
      "end": "2015-04-14 20:29:48.355733", 
      "invocation": { 
       "module_args": "ethtool -i enp5s0f2: | grep bus-info | cut -b 16-22", 
       "module_name": "shell" 
      }, 
      "item": "enp5s0f2:", 
      "rc": 0, 
      "start": "2015-04-14 20:29:48.349870", 
      "stderr": "", 
      "stdout": "05:00.2", 
      "warnings": [] 
     }, 
     { 
      "changed": true, 
      "cmd": "ethtool -i enp5s0f3: | grep bus-info | cut -b 16-22", 
      "delta": "0:00:00.005829", 
      "end": "2015-04-14 20:29:48.591244", 
      "invocation": { 
       "module_args": "ethtool -i enp5s0f3: | grep bus-info | cut -b 16-22", 
       "module_name": "shell" 
      }, 
      "item": "enp5s0f3:", 
      "rc": 0, 
      "start": "2015-04-14 20:29:48.585415", 
      "stderr": "", 
      "stdout": "05:00.3", 
      "warnings": [] 
     }, 
     { 
      "changed": true, 
      "cmd": "ethtool -i enp9s0f0: | grep bus-info | cut -b 16-22", 
      "delta": "0:00:00.005943", 
      "end": "2015-04-14 20:29:48.910992", 
      "invocation": { 
       "module_args": "ethtool -i enp9s0f0: | grep bus-info | cut -b 16-22", 
       "module_name": "shell" 
      }, 
      "item": "enp9s0f0:", 
      "rc": 0, 
      "start": "2015-04-14 20:29:48.905049", 
      "stderr": "", 
      "stdout": "09:00.0", 
      "warnings": [] 
     }, 
     { 
      "changed": true, 
      "cmd": "ethtool -i enp9s0f1: | grep bus-info | cut -b 16-22", 
      "delta": "0:00:00.005863", 
      "end": "2015-04-14 20:29:49.143706", 
      "invocation": { 
       "module_args": "ethtool -i enp9s0f1: | grep bus-info | cut -b 16-22", 
       "module_name": "shell" 
      }, 
      "item": "enp9s0f1:", 
      "rc": 0, 
      "start": "2015-04-14 20:29:49.137843", 
      "stderr": "", 
      "stdout": "09:00.1", 
      "warnings": [] 
     }, 
     { 
      "changed": true, 
      "cmd": "ethtool -i lo: | grep bus-info | cut -b 16-22", 
      "delta": "0:00:00.005856", 
      "end": "2015-04-14 20:29:49.386044", 
      "invocation": { 
       "module_args": "ethtool -i lo: | grep bus-info | cut -b 16-22", 
       "module_name": "shell" 
      }, 
      "item": "lo:", 
      "rc": 0, 
      "start": "2015-04-14 20:29:49.380188", 
      "stderr": "Cannot get driver information: Operation not supported", 
      "stdout": "", 
      "warnings": [] 
     }, 
     { 
      "changed": true, 
      "cmd": "ethtool -i virbr0: | grep bus-info | cut -b 16-22", 
      "delta": "0:00:00.005859", 
      "end": "2015-04-14 20:29:49.632356", 
      "invocation": { 
       "module_args": "ethtool -i virbr0: | grep bus-info | cut -b 16-22", 
       "module_name": "shell" 
      }, 
      "item": "virbr0:", 
      "rc": 0, 
      "start": "2015-04-14 20:29:49.626497", 
      "stderr": "", 
      "stdout": "", 
      "warnings": [] 
     }, 
     { 
      "changed": true, 
      "cmd": "ethtool -i virbr0-nic: | grep bus-info | cut -b 16-22", 
      "delta": "0:00:00.024850", 
      "end": "2015-04-14 20:29:49.901539", 
      "invocation": { 
       "module_args": "ethtool -i virbr0-nic: | grep bus-info | cut -b 16-22", 
       "module_name": "shell" 
      }, 
      "item": "virbr0-nic:", 
      "rc": 0, 
      "start": "2015-04-14 20:29:49.876689", 
      "stderr": "", 
      "stdout": "", 
      "warnings": [] 
     } 
    ] 

Il mio obiettivo è quello di ottenere il valore di stdout in ogni articolo di cui sopra, ad esempio ("stdout": "09: 00.0"). Ho provato a dare qualcosa come

 - name: Binding the interfaces 
     shell: echo {{ item.item.stdout}} 
     with_flattened: var2.results 
#  with_indexed_items: var2.results 
     register: var3 

Ma questo non sta dando i valori del bus in stdout correttamente. Apprezzare l'aiuto nell'elencare la variabile di valore variabile nell'attività come indicato di seguito quando la seconda variabile è e l'elenco indicizzato. Sto cercando di evitare la numerazione dell'indice diretto come item [0] perché il numero di interfacce è dinamico e l'indicizzazione diretta può portare a risultati imprevisti.

Grazie

risposta

20

E 'questo quello che stai cercando:

- hosts: localhost 
    tags: s21 
    gather_facts: no 
    vars: 
    images: 
     - foo 
     - bar 
    tasks: 
    - shell: "echo result-{{item}}" 
     register: "r" 
     with_items: "{{images}}" 

    - debug: var=r 

    - debug: msg="item.item={{item.item}}, item.stdout={{item.stdout}}, item.changed={{item.changed}}" 
     with_items: "{{r.results}}" 

    - debug: msg="Gets printed only if this item changed - {{item}}" 
     when: "{{item.changed == true}}" 
     with_items: "{{r.results}}" 

Fonte: Register variables in with_items loop in Ansible playbook

+0

Questo non risolve il mio problema – user3364247

+0

@ user3364247 si è tentato usando 'with_items' anziché appiattito a loop su 'var2.results'? Puoi pubblicare un nuovo codice? – Kashyap

+0

In realtà la tua soluzione è corretta, ma per ottenere quello che volevo, ha modificato il codice come segue – user3364247

Problemi correlati