Ansible Playbook循环

Standard Loops

with_items的值是python list数据结构,task会循环读取list列表中的值,key的名称是item。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- hosts: localhost
tasks:
- name: Loops:with_items
debug: msg="name:{{ item }}"
with_items:
- one
- two
- three
- name: Loops:with_items
debug: msg="name:{{ item.name }} value:{{ item.value }}"
with_items:
- {name: one,value: 1}
- {name: two,value: 2}
- {name: three,value: 3}

Nested Loops

with_nested嵌套循环主要实现一对多或多对多的合并。

1
2
3
4
5
6
7
- hosts: localhost
tasks:
- name: Loops:with_nested
debug: msg="name:{{ item[0] }} value:{{ item[1] }}"
with_nested:
- ['A']
- ['a','b','c']
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
TASK [Loops:with_nested] *************************************************
ok: [localhost] => (item=[u'A', u'a']) => {
"changed": false,
"item": [
"A",
"a"
],
"msg": "name:A value:a"
}
ok: [localhost] => (item=[u'A', u'b']) => {
"changed": false,
"item": [
"A",
"b"
],
"msg": "name:A value:b"
}
ok: [localhost] => (item=[u'A', u'c']) => {
"changed": false,
"item": [
"A",
"c"
],
"msg": "name:A value:c"
}

Looping over Files

with_file遍历文件列表中的内容,并将文件中的内容赋值给item。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- hosts: localhost
tasks:
- name: Loops:with_file
debug: msg="{{ item }}"
with_file:
- /root/1
- /root/2

# echo 11111111 > /root/1
# echo 22222222 > /root/2
# ansible-playbook with_file.yaml
TASK [Loops:with_file] *************************************************
ok: [localhost] => (item=11111111) => {
"changed": false,
"item": "11111111",
"msg": "11111111"
}
ok: [localhost] => (item=22222222) => {
"changed": false,
"item": "22222222",
"msg": "22222222"
}

Looping over Fileglobs

with_fileglob以非递归的模式(按通配符)匹配单个目录中的文件(调用python的glob库),并将文件路径赋值给item。

1
2
3
4
5
6
7
8
9
10
- hosts: localhost
tasks:
- name: Loops:with_fileglob
copy: src="{{ item }}" dest="/root/fileglob/"
with_fileglob:
- /etc/ansible/*

TASK [Loops:with_fileglob] ************************************************
changed: [localhost] => (item=/etc/ansible/hosts)
changed: [localhost] => (item=/etc/ansible/ansible.cfg)

Random Choices

with_random_choice从传入的list中随机选择一个,与使用Python random实现原理一样。

1
2
3
4
5
6
7
8
9
10
11
- hosts: localhost
gather_facts: False
tasks:
- name: Loops:with_random_choice
debug: msg="{{ item }}"
with_random_choice:
- "apple"
- "banana"
- "pear"
- "orange"
- "grape"

Do-Until Loops

当执行一个task后,需要在指定的时间内重复检测task的结果是否达到预想结果,如果没有达到预期则退出整个playbook

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- hosts: localhost
gather_facts: False
tasks:
- name: Loops:until
shell: date +%H:%M:%S
register: result
until: result.stdout.startswith("14:24:3")
retries: 10
delay: 5

# for i in {1..10} ;do echo $i > /root/until && sleep 6;done &
# ansible-playbook until.yaml
TASK [Loops:until] ******************************************************************
FAILED - RETRYING: Loops:until (10 retries left).
FAILED - RETRYING: Loops:until (9 retries left).
FAILED - RETRYING: Loops:until (8 retries left).
FAILED - RETRYING: Loops:until (7 retries left).
changed: [localhost] => {"attempts": 5, "changed": true, "cmd": "date +%H:%M:%S", "delta": "0:00:00.002183", "end": "2018-03-20 14:24:30.905557", "rc": 0, "start": "2018-03-20 14:24:30.903374", "stderr": "", "stderr_lines": [], "stdout": "14:24:30", "stdout_lines": ["14:24:30"]}

Using register with a loop

register还可以同时接收多个task的结果,赋值给同一个变量,但是变量的值需要使用jinja2的for循环才能把所有结果显示出来

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
- hosts: localhost
gather_facts: False
tasks:
- name: Loops:until
shell: "{{ item }}"
with_items:
- date
- hostname
- uname
register: ret
- name: Display register loops
debug: msg="{% for i in ret.results %} {{ i.stdout }} {% endfor %}"

TASK [Loops:until] ****************************************************************
changed: [localhost] => (item=date)
changed: [localhost] => (item=hostname)
changed: [localhost] => (item=uname)
TASK [Display register loops] *****************************************************
ok: [localhost] => {
"msg": " Tue Mar 20 13:26:41 CST 2017 ansible Linux "
}
坚持原创技术分享,您的支持将鼓励我继续创作!
0%