ansible笔记(15):循环(二)with_items/with_list/with_together/with_flattened

Nested list (in sequence), for example:

---
- hosts: 192.168.10.2
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_items:
    - [1,2,3]
    - [a,b]

The above example we will combine the previous two syntaxes, define a list, and this list is a list of each item, the equivalent of a large number of small nested list of lists, so that when we use with_items when traversing the list above, what would the effect? We try, information is performed as follows:

 

 

You can see, debug module cycle value of each of the small list are output again, this may not be the same with our imagination, as in the previous example, the case does not list nested list, in accordance with the previous ideas, each will cycle with_items output list (the outermost layer of the large list), that is, according to previous thinking debug each module will be small as a small list of the overall output, the output should not be a small list each element, but the fact is that every small list with_items nest in large list were opened, and the small elements in the list are output, and if we want to list every little as a overall output, how to do it? 
 

We can use with_list keyword, keyword replacement on with_items example in the playbook, so with_list keywords and keyword with_items What difference does it make? The with_items the example of the replacement can not achieve what we wanted to with_list after it? We work together to try and examples playbook as follows:

---
- hosts: 192.168.10.2
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_list:
    - [1,2,3]
    - [a,b]

Shown on the list in the previous embodiment playbook playbook list of examples of the same, are nested lists, simply replace the original keyword as Example To with_list with_items keywords, we look at the implementation of the results, the after execution Example playbook debug output module: the list of individual output.

 

 

 

As shown in the above list of small information with_list after treatment, each nested in a large list as a whole are stored in the variable item, eventually as a small whole debug output, and not the same as the with_items loop output element and a small list after a small list expands leveled.

Previous article there are many examples, in fact, with_items keywords in these examples can be replaced with_list keyword, after the replacement can be executed properly, this is because the list of previous article in the examples are simple list monolayer, when dealing with a simple list of monolayer, with_list and with_items there is no difference, only in dealing with nested list in the previous example, will reflect the difference, the difference is, with_items will be nested inside a small list flare, loop handle all elements of the flare, and with_list will not flatten a nested list, each cycle of treatment will only with_list list (the outermost layer of the list) in. 

In fact, when dealing with such a nested list, if you want to achieve flattening effect, we can use another keyword, it is with_flattened keyword example playbook as follows:

---
- hosts: 192.168.10.2
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_flattened:   #效果和with_items等效
    - [1,2,3]
    - [a,b]

 

Learning a new keyword: with_together, you may be two elements in the list "align merger", for example:

---
- hosts: 192.168.10.2
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_together:
    - [1,2,3]
    - [a,b,c]

In the above example, we define a nested list, listing a large total of two small list, there are three values ​​in each small list, then the process using the keyword with_together nested list, on the results of the following Example playbook :

As can be seen from the above results: 
 the first combined value of the first value in the list in the first and second small list with small output, 
 the second first value and the second small list the second merge small value is output together in the list, 
 the third list of the first small value integrating the third value and the second list with small output, 
 which is aligned with the so-called with_together merge feature, smart as you must have understood. 
 However, the above example, two identical small number of elements in the list, if the number of elements of different small list using with_together alignment merger, what would be the effect? 
 Here is not to be an example, and fast hands to try it. 
 

  

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

Guess you like

Origin www.cnblogs.com/python-wen/p/11608895.html