Question:
I have two ansible tasks as follows
1 2 3 4 5 6 7 8 9 10 |
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 |
which is used to get a list of interfaces in a machine (linux) and get the bus address for each. I have one more task as follows in tha same playbook
1 2 3 4 5 |
- name: Binding the interfaces shell: echo {{ item.item }} with_flattened: var2.results register: var3 |
which I expect to iterate over value from var2 and then print the bus numbers.
var2.results is as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
"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": [] } ] |
My objective is to get the value of stdout in each item above for example (“stdout”: “09:00.0”) . I tried giving something like
1 2 3 4 5 6 |
- name: Binding the interfaces shell: echo {{ item.item.stdout}} with_flattened: var2.results # with_indexed_items: var2.results register: var3 |
But this is not giving the bus values in stdout correctly. Appreciate help in listing the variable of variable value in task as given below when the second variable is and indexed list. I am trying to avoid direct index numbering such as item[0] because the number of interfaces are dynamic and direct indexing may result in unexpected outcomes.
Thanks
Answer:
Is this what you’re looking for:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
- 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}}" |