💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
# 循环 # [loop](http://docs.ansible.com/ansible/latest/playbooks_loops.html#) ## 标准循环 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](http://docs.ansible.com/ansible/latest/playbooks_loops.html#looping-over-integer-sequences) ``` --- - 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](http://docs.ansible.com/ansible/latest/playbooks_loops.html#looping-over-the-inventory) ``` # 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 ```