1. Standard loop
With_items parameters achieved, item will go to polling with_items this array values.
Defined normal_loos.yaml:
--- - hosts: all gather_facts: False tasks: - name: debug loops debug: msg="name ------> {{ item }}" with_items: - one - two
Results of the:
Of course, it supports key-value pairs ways:
defined normal2_loops.yaml:
--- - hosts: all gather_facts: False tasks: - name: debug loops debug: msg="name ------> {{ item.key }} value ------> {{ item.value }}" with_items: - {key: "one", value: "VALUE1"} - {key: "two", value: "VALUE2"}
Results of the:
2. Nested Loops
The combined to achieve a plurality of main body of the loop.
Defined nest_loop.yaml:
--- - hosts: all gather_facts: False tasks: - name: debug loops debug: msg="name->{{ item[0] }} value->{{ item[1] }}" with_nested: - ['A'] - ['a','b','b']
Results of the:
Explanation:
with_nested is an array.
Item [0] represents the first element of the array, i.e. [ 'A']
Item [. 1] indicates that the second element of the array, i.e. [ 'a', 'b ',' b ']
then these two elements are circular.
3. hash loops
Standard loop support is an array, a list of python is similar.
In fact, the use of a nested loop [{}, {}] This form of the list of sets of dictionary,
the dictionary can directly support the hash loop form, but to follow yaml disguised form.
Definitions: hash_loop.yaml
--- - hosts: all gather_facts: Fasle vars: user: kebi: name: zhangrongkai addr: luotian maoxian: name: songlixing addr: xiaochang tasks: - name: debug loops debug: msg="name--->{{ item.key }} value--->{{ item.value.name }} addr---{{ item.value.addr }}" with_dict: "{{ user }}"
Convert it to the format in python dictionary:
user = { "kebi": { "name": "zhangrongkai", "addr": "luotian" }, "maoxian": { "name": "songlixing", "addr": "xiaochang" } }
item = user, key = [kebi, maoxian], and then take the attribute value.
Note that: with_dict: "{{user} }"
prior to 2.0, is used in this format: with_dict: user
otherwise an error:
4. File Configuration loops
At some point, we need to do something for a certain type of file, this time you can use fileglob this parameter.
By this fileglob able to match the corresponding file, and then can perform a corresponding operation.
Definitions: file_loop.yaml
--- - hosts: all tasks: - name: debug loops debug: msg="filename--->{{ item }}" with_fileglob: - /tmp/*.py
Results of the:
Within the ansible is actually calling python module glob, then proceed as follows:
glob.glob (. "/ Tmp / * Py")
The random selection loops
Randomly selecting a value item in the array.
Definitions: random_loop.yaml
--- - hosts: all tasks: - name: debug loops debug: msg="name--->{{ item }}" with_random_choice: - "test1" - "test2" - "test3"
Results of the:
In fact, the interior is the use of random python module.
6. Analyzing conditions
Sometimes, we need to perform an operation, the operation may fail, we want to try to perform again after 5 seconds,
this operation was repeated five times, if not enough to quit.
Definitions: case_loop.yaml
--- - hosts: all tasks: - name: debug loops shell: cat /etc/hostname register: host until: host.stdout.startswith("centos") retries: 5 delay: 5
Results of the:
Explanation:
name: Debug Loops # task name, nonessential
shell: cat / etc / hostname # operation
register: host # operating result is assigned to Host
an until: host.stdout.startswith ( "CentOS") # established until ...
retries: 5 # repeated five times
delay: 5 # repeated once every five seconds
if the operation failed, the result is the following:
7. Priority documents matching Loops
File priority matches are matched from top to bottom according to your incoming variable or file, if the match to a file, it will be as a value {{item}} with this document.
--- - hosts: all tasks: - name: debug loops debug: msg="file--->{{ item }}" with_first_found: - "{{ ansible_distribution }}.yaml" - "default.yaml
8.register Loops
Before receiving the result register defined by shell operations ret.
It was received before a data, but now does receive more.
Definitions: register_loop.yaml
--- - hosts: all tasks: - name: debug loops shell: "{{ item }}" with_items: - hostname - uname register: ret - name: display loops debug: msg="{% for i in ret.results %} {{ i.stdout }} {% endfor %}"
Results of the: