一、简介
通常一个任务会做很多事情,如创建大量的用户,安装很多包,或重复的轮询特定的步骤,直到某种结果条件为止,Ansible为我们提供了此支持。下面是关于循环的一些语句。
with_items #标准循环
with_nested #嵌套循环
with_dict #字典循环(Looping over hashed)
with_file #循环文件
with_fileglob #通配符循环文件
with_together #循环映射
with_subelements #子元素循环
with_sequence #生成一系列的数字
with_random_choice #任意选择
#with_first_found #选择第一个找到的,不会用
with_lines #循环程序执行的输出信息with_items:{{command_result.stdout_lines}}
with_flattened #循环列表中的列表的item
二、实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
---
- hosts: local #with_item 和 with_nested 用法
vars:
Sta: false
User: [ "Aheahe","yunzhonghe" ]
tasks:
- name: Standard Loops
debug: msg="{{ item.name }},{{ item.fun }}"
with_items: #生成标准循环
- { name: Loops, fun: xx }
- { name: Conditional, fun: yy }
when: Sta
- name: Nested Loops
debug: msg= "name={{ item[0] }},priv={{ item[1] }}"
with_nested: #嵌套循环
- "{{ User }}" #这里的User 可以直接使用列表。
- [ "Student","Engineer","Friends" ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
---
- hosts: local #with_dict 用法
# vars_files:
# hash.yml
vars:
users:
alice:
name: Alice Appleworth
telephone: 123-456-7890
bob:
name: Bob Bananarama
telephone: 987-654-3210
tasks:
- name: Print phone records
debug: msg="User item.key is item.value.name item.value.telephone "
with_dict: users #循环列表可以查看输出信息。以获取结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
---
- hosts: local #with_file 与 with_fileglob 用法。
vars:
test_file: false
tasks:
- name: test file
shell: echo {{ item }}
with_file: #循环文件的内容
- test #test文件里面为任意内容。
- test1
when: test_file
- name: Test file Glob
shell: echo {{ item }}
with_fileglob: #循环文件的通配符。
- /home/aheahe/playbook/*
1
2
3
4
5
6
7
8
9
10
11
12
---
- hosts: local #with_together用法
vars:
alpha: [ 'a', 'b', 'c', 'd' ]
numbers: [ 1, 2, 3, 4 ]
test: [ 'A', 'B', 'C', 'D' ]
tasks:
- debug: msg="{{ item.0 }} and {{ item.1 }} and {{ item.2 }}"
with_together: #循环映射信息
- "{{alpha}}"
- "{{numbers}}"
- "{{test}}"
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
---
- hosts: local #with_subelement用法
vars:
users:
- name: alice
authorized:
- /tmp/alice/onekey.pub
- /tmp/alice/twokey.pub
mysql:
password: mysql-password
hosts:
- "%"
- "127.0.0.1"
- "::1"
- "localhost"
privs:
- "*.*:SELECT"
- "DB1.*:ALL"
- name: bob
authorized:
- /tmp/bob/id_rsa.pub
mysql:
password: other-mysql-password
hosts:
- "db1"
privs:
- "*.*:SELECT"
- "DB2.*:ALL"
tasks:
- name: Test1
shell: echo "name={{ item.name }}"
with_items: "{{users}}"
- name: Test2
shell: echo "User={{ item.0.name }},key={{ item.1 }}"
with_subelements: #循环子元素。
- users
- authorized
1
2
3
4
5
6
---
- hosts: local
tasks:
- name: Learn loop sequence
debug: msg={{ item }}
with_sequence: start=0 end=6 stride=2
1
2
3
4
5
6
7
8
9
10
---
- hosts: local # with_random_choice 用法
tasks:
- name: Learn Random
debug: msg={{ item }}
with_random_choice:
- "go through the door"
- "drink from the goblet"
- "press the red button"
- "do nothing"
1
2
3
4
5
6
7
8
9
10
11
12
---
- hosts: local #with_lines 用法
tasks:
- name: test result
shell: echo {{ item }}
with_lines: /bin/df -Th
- name: flattened loop demo #with_flatted用法
yum: name={{ item }} state=installed
with_flattened:
- "{{packages_base}}"
- "{{packages_apps}}"
1
2
3
4
5
6
7
----
# file: roles/foo/vars/main.yml
packages_base:
- [ 'foo-package', 'bar-package' ]
packages_apps:
- [ ['one-package', 'two-package' ]]
- [ ['red-package'], ['blue-package']]
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com