Ability to use a loop and multiple variables

I use " shell: " to get some data by going through " with_items: " and registering it as another variable. Later, using " lineinfile: ", I try to apply the contents of an earlier variable, but I can not use " {{variable.stdout}} " because it shows as undefined in " c_items: "

Is there any way to say that for the variable.stdout parameter "do not look in with_items: "

--- - include_vars: /root/template.yml - name: Getting MAC shell: "cat /sys/class/net/{{item.name}}/address" register: mac with_items: - "{{ interfaces_ipv4 }}" - name: Setting MAC lineinfile: state=present dest=/etc/sysconfig/network-scripts/ifcfg-{{item.name}} regexp='^HWADDR=.*' line="HWADDR={{mac.stdout}}" with_items: - "{{ interfaces_ipv4 }}" tags: - set_mac 

Variable File Contents

 #/root/tempplate.yml - name: ens35 bootproto: dhcp - name: ens34 bootproto: none 

While doing:

TASK: [mac | MAC Setup] ************************************************ ****** fatal: [192.168.211.146] => One or more variables undefined: the 'dict' object does not have the 'stdout' attribute

FATAL: all hosts are already crashing - interruption

+5
source share
1 answer

register works a bit differently when used inside loops (see here ). In this case, your variable will have a results element, which is a list with the result of each iteration as elements. Each item in this list will also have an item , with the item repeating itself.

For instance:

 mac: { msg: "All items completed", results: [ { changed: True, stdout: "some_stdout", item: { name: "some_name1" } }, { changed: True, stdout: "some_stdout2", item: { name: "some_name2" } } ] } 

Then you can iterate over this instead of the original list:

 - name: Setting MAC lineinfile: state=present dest=/etc/sysconfig/network-scripts/ifcfg-{{item.item.name}} regexp='^HWADDR=.*' line="HWADDR={{item.stdout}}" with_items: mac.results tags: - set_mac 
+8
source

Source: https://habr.com/ru/post/1206326/


All Articles