多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# [loop][3] ## 标准循环 with_items ~~~ # 模式1 - name: add several users user: name={{ item }} state=present groups=wheel with_items: - testuser1 - testuser2 ~~~ ~~~ # 模式2 - name: add several users user: name={{ item.name }} state=present groups={{ item.groups }} with_items: - { name: 'testuser1', groups: 'wheel' } - { name: 'testuser2', groups: 'root' } ~~~ ## 嵌套循环 ~~~ --- - name: test hosts: masters tasks: - name: give users access to multiple databases command: "echo name={{ item[0] }} priv={{ item[1] }} test={{ item[2] }}" with_nested: - [ 'alice', 'bob' ] - [ 'clientdb', 'employeedb', 'providerdb' ] - [ '1', '2', ] result: changed: [localhost] => (item=[u'alice', u'clientdb', u'1']) changed: [localhost] => (item=[u'alice', u'clientdb', u'2']) changed: [localhost] => (item=[u'alice', u'employeedb', u'1']) changed: [localhost] => (item=[u'alice', u'employeedb', u'2']) changed: [localhost] => (item=[u'alice', u'providerdb', u'1']) changed: [localhost] => (item=[u'alice', u'providerdb', u'2']) changed: [localhost] => (item=[u'bob', u'clientdb', u'1']) changed: [localhost] => (item=[u'bob', u'clientdb', u'2']) changed: [localhost] => (item=[u'bob', u'employeedb', u'1']) changed: [localhost] => (item=[u'bob', u'employeedb', u'2']) changed: [localhost] => (item=[u'bob', u'providerdb', u'1']) changed: [localhost] => (item=[u'bob', u'providerdb', u'2']) ~~~ ## 文件循环(with_file, with_fileglob) with_file 是将每个文件的文件内容作为item的值 with_fileglob 是将每个文件的全路径作为item的值, 在文件目录下是非递归的, 如果是在role里面应用改循环, 默认路径是roles/role_name/files_directory ~~~ - copy: src={{ item }} dest=/etc/fooapp/ owner=root mode=600 with_fileglob: - /playbooks/files/fooapp/* ~~~ ## 字典循环 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 }}" ~~~ ## with_together ~~~ tasks: - command: echo "msg={{ item.0 }} and {{ item.1 }}" with_together: - [ 1, 2, 3 ] - [ 4, 5 ] result: changed: [localhost] => (item=[1, 4]) changed: [localhost] => (item=[2, 5]) changed: [localhost] => (item=[3, None]) ~~~ ## [with_sequence][1] ~~~ --- - 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%02x # 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 # a simpler way to use the sequence plugin # create 4 groups - group: name=group{{ item }} state=present with_sequence: count=4 ~~~ ## 循环一个执行结果 with_lines ~~~ --- - name: test hosts: all tasks: - name: Example of looping over a command result shell: touch /$HOME/{{ item }} with_lines: /usr/bin/cat /home/fg/test with_lines 中的命令永远都是在controller的host上运行, 只有shell命令才会在inventory中指定的机器上运行 ~~~ ## 随机选择 with_random_choice with_random_choice:在提供的list中随机选择一个值 ## [with_inventory_hostnames][2] ~~~ # show all the hosts in the inventory - debug: msg: "{{ item }}" with_items: - "{{ groups['all'] }}" # show all the hosts in the current play - debug: msg: "{{ item }}" with_items: - "{{ ansible_play_batch }}" # show all the hosts in the inventory - debug: msg: "{{ item }}" with_inventory_hostnames: - all # show all the hosts matching the pattern, ie all but the group www - debug: msg: "{{ item }}" with_inventory_hostnames: - all:!www ~~~ [1]:http://docs.ansible.com/ansible/latest/playbooks_loops.html#looping-over-integer-sequences [2]:http://docs.ansible.com/ansible/latest/playbooks_loops.html#looping-over-the-inventory [3]:http://docs.ansible.com/ansible/latest/playbooks_loops.html#