在Ansible 2.5以前,playbook通过不同的循环语句以实现不同的循环,这些语句使用with_作为前缀。这些语法目前仍然兼容,但在未来的某个时间点,会逐步废弃。
1、with_items
- hosts: test vars: data: - user0 - user1 - user2 tasks: - name: "with_items" debug: msg: "{{ item }}" with_items: "{{ data }}"
2、with_nested
tasks: - name: debug loops debug: msg="name is {{ item[0] }} vaule is {{ item[1] }} num is {{ item[2] }}" with_nested: - ['alice','bob'] - ['a','b','c'] - ['1','2','3'] # with_neted会将三个子列表拼成笛卡尔积,然后进行遍历,赋值给item变量 # 拼成的 第一个列表['alice',a,1] 第二个列表['alice',a,2] 第三个列表['alice',a,3] 第四个列表['alice',b,1] 第五个列表['alice',b,2]
3、with_dict
# 假如有如下变量内容: 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 }}"
4、with_fileglob
- hosts: test tasks: - name: Make key directory file: path: /root/.sshkeys state: directory mode: 0700 owner: root group: root - name: Upload public keys copy: src: "{{ item }}" dest: /root/.sshkeys mode: 0600 owner: root group: root with_fileglob: - /root/.ssh/*.pub - name: Assemble keys into authorized_keys file assemble: src: /root/.sshkeys dest: /root/.ssh/authorized_keys mode: 0600 owner: root group: root
5、with_lines
with_lines循环结构会让你在控制主机上执行任意命令,并对命令的输出进行逐行迭代。假设我们有一个 文件test.txt包含如下行:
egon lin big egon small egon
我们可以通过如下方法进行逐行输出:
[root@lb workspace]# cat 3.yaml - hosts: group5 tasks: - name: print all names debug: msg="{{ item }}" with_lines: - cat test.txt
6、with_subelement
假如现在需要遍历一个用户列表,并创建每个用户,而且还需要为每个用户配置以特定的SSH key登录。变量文件内容如下:
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"
playbook中定义如下:
tasks: - user: name={{ item.name }} state=present generate_ssh_key=yes with_items: "{{users}}" - authorized_key: "user={{ item.0.name }} key='{{ lookup('file', item.1) }}'" with_subelements: - users - authorized
也可以遍历嵌套的子列表:
- name: Setup MySQL users mysql_user: name={{ item.0.name }} password={{ item.0.mysql.password }} host={{ item.1 }} priv={{ item.0.mysql.privs | join('/') }} with_subelements: - users - mysql.hosts
7 with_sequence
- hosts: all tasks: # create groups - group: name=evens state=present - group: name=odds state=present # create some test users - user: name={{ item }} state=present groups=evens with_sequence: start=0 end=32 format=testuser%02d # create a series of directories with even numbers for some reason - file: dest=/var/stuff/{{ item }} state=directory with_sequence: start=4 end=16 stride=2 # stride用于指定步长 # a simpler way to use the sequence plugin # create 4 groups - group: name=group{{ item }} state=present with_sequence: count=4
8、with_random_choice
从列表中随机取一个值:
- debug: msg={{ item }} with_random_choice: - "go through the door" - "drink from the goblet" - "press the red button" - "do nothing"
9、do-Util循环
- action: shell /usr/bin/foo register: result until: result.stdout.find("all systems go") != -1 retries: 5 delay: 10
重复执行shell模块,当shell模块执行的命令输出内容包含"all systems go"的时候停止。重试5次,延迟时间10秒。retries默认值为3,delay默认值为5。任务的返回值为最后一次循环的返回结果。# 4.10 with_together
10、with_together
示例:
- hosts: webservers remote_user: root vars: alpha: [ 'a','b','c','d'] numbers: [ 1,2,3,4 ] tasks: - debug: msg="{{ item.0 }} and {{ item.1 }}" with_together: - "{{ alpha }}" - "{{ numbers }}" # 输出的结果为: ok: [192.168.1.65] => (item=['a', 1]) => { "item": [ "a", 1 ], "msg": "a and 1" } ok: [192.168.1.65] => (item=['b', 2]) => { "item": [ "b", 2 ], "msg": "b and 2" } ok: [192.168.1.65] => (item=['c', 3]) => { "item": [ "c", 3 ], "msg": "c and 3" } ok: [192.168.1.65] => (item=['d', 4]) => { "item": [ "d", 4 ], "msg": "d and 4" }