循环

Ansible 提供 loopwith_<lookup>until 关键字来多次执行任务。常用循环的示例包括使用 文件模块 更改多个文件和/或目录的所有权,使用 用户模块 创建多个用户,以及重复轮询步骤,直到达到特定结果。

注意

  • 我们在 Ansible 2.5 中添加了 loop,作为一种更简单的方法来执行循环,但我们建议在大多数情况下使用它。

  • 我们没有弃用使用 with_<lookup> - 该语法在可预见的将来仍然有效。

  • loopwith_<lookup> 是互斥的。虽然可以在 until 下嵌套它们,但这会影响每次循环迭代。

比较循环

  • until 的正常用例与可能失败的任务有关,而 loopwith_<lookup> 用于重复稍微不同的任务。

  • loopwith_<lookup> 会对用作输入的列表中的每个项目运行一次任务,而 until 会重新运行任务,直到满足条件。对于程序员来说,前者是“for 循环”,后者是“while/until 循环”。

  • with_<lookup> 关键字依赖于 查找插件 - 即使 items 也是一个查找。

  • loop 关键字等效于 with_list,是简单循环的最佳选择。

  • loop 关键字不接受字符串作为输入,请参阅 确保循环的列表输入:使用 query 而不是 lookup

  • until 关键字接受一个“结束条件”(返回 TrueFalse 的表达式),该条件是“隐式模板化”的(不需要 {{ }}),通常基于您为任务注册的变量。

  • loop_control 同时影响 loopwith_<lookup>,但不影响 until,后者有自己的配套关键字:retriesdelay

  • 一般来说,从 with_X 迁移到 loop 中涵盖的任何 with_* 使用都可以更新为使用 loop

  • with_items 更改为 loop 时要小心,因为 with_items 执行隐式单级扁平化。您可能需要在 loop 中使用 | flatten(1) 来匹配确切的结果。例如,要获得与

with_items:
  - 1
  - [2,3]
  - 4

相同的输出,您需要

loop: "{{ [1, [2, 3], 4] | flatten(1) }}"
  • 任何需要在循环中使用 lookupwith_* 语句不应转换为使用 loop 关键字。例如,不要执行

loop: "{{ lookup('fileglob', '*.txt', wantlist=True) }}"

,而是保持

with_fileglob: '*.txt'

使用循环

遍历一个简单的列表

重复任务可以写成对简单字符串列表的标准循环。您可以在任务中直接定义列表。

- name: Add several users
  ansible.builtin.user:
    name: "{{ item }}"
    state: present
    groups: "wheel"
  loop:
     - testuser1
     - testuser2

您可以在变量文件中或剧本的“vars”部分定义列表,然后在任务中引用列表的名称。

loop: "{{ somelist }}"

这两个示例中的任何一个都等效于

- name: Add user testuser1
  ansible.builtin.user:
    name: "testuser1"
    state: present
    groups: "wheel"

- name: Add user testuser2
  ansible.builtin.user:
    name: "testuser2"
    state: present
    groups: "wheel"

您可以将列表直接传递给某些插件的参数。大多数打包模块(如 yumapt)都具有此功能。如果可用,将列表传递给参数比循环遍历任务更好。例如

- name: Optimal yum
  ansible.builtin.yum:
    name: "{{ list_of_packages }}"
    state: present

- name: Non-optimal yum, slower and may cause issues with interdependencies
  ansible.builtin.yum:
    name: "{{ item }}"
    state: present
  loop: "{{ list_of_packages }}"

检查 模块文档 以查看是否可以将列表传递给任何特定模块的参数。

遍历一个哈希列表

如果您有一个哈希列表,您可以在循环中引用子键。例如

- name: Add several users
  ansible.builtin.user:
    name: "{{ item.name }}"
    state: present
    groups: "{{ item.groups }}"
  loop:
    - { name: 'testuser1', groups: 'wheel' }
    - { name: 'testuser2', groups: 'root' }

条件 与循环结合使用时,when: 语句对每个项目单独处理。请参阅 使用 when 的基本条件 以获取示例。

遍历一个字典

要遍历字典,请使用 dict2items

- name: Using dict2items
  ansible.builtin.debug:
    msg: "{{ item.key }} - {{ item.value }}"
  loop: "{{ tag_data | dict2items }}"
  vars:
    tag_data:
      Environment: dev
      Application: payment

在这里,我们正在遍历 tag_data 并打印其中的键和值。

使用循环注册变量

您可以将循环的输出注册为一个变量。例如

- name: Register loop output as a variable
  ansible.builtin.shell: "echo {{ item }}"
  loop:
    - "one"
    - "two"
  register: echo

当您在循环中使用register时,放置在变量中的数据结构将包含一个results属性,该属性是模块所有响应的列表。这与在没有循环的情况下使用register返回的数据结构不同。results旁边的changed/failed/skipped属性将表示整体状态。如果至少有一次迭代触发了更改/失败,则changed/failed将为true,而skipped仅当所有迭代都被跳过时才会为true

{
    "changed": true,
    "msg": "All items completed",
    "results": [
        {
            "changed": true,
            "cmd": "echo \"one\" ",
            "delta": "0:00:00.003110",
            "end": "2013-12-19 12:00:05.187153",
            "invocation": {
                "module_args": "echo \"one\"",
                "module_name": "shell"
            },
            "item": "one",
            "rc": 0,
            "start": "2013-12-19 12:00:05.184043",
            "stderr": "",
            "stdout": "one"
        },
        {
            "changed": true,
            "cmd": "echo \"two\" ",
            "delta": "0:00:00.002920",
            "end": "2013-12-19 12:00:05.245502",
            "invocation": {
                "module_args": "echo \"two\"",
                "module_name": "shell"
            },
            "item": "two",
            "rc": 0,
            "start": "2013-12-19 12:00:05.242582",
            "stderr": "",
            "stdout": "two"
        }
    ]
}

随后循环遍历注册的变量以检查结果可能如下所示

- name: Fail if return code is not 0
  ansible.builtin.fail:
    msg: "The command ({{ item.cmd }}) did not have a 0 return code"
  when: item.rc != 0
  loop: "{{ echo.results }}"

在迭代期间,当前项目的运行结果将放置在变量中。

- name: Place the result of the current item in the variable
  ansible.builtin.shell: echo "{{ item }}"
  loop:
    - one
    - two
  register: echo
  changed_when: echo.stdout != "one"

重试任务直到满足条件

版本 1.4 中的新功能。

您可以使用until关键字重试任务,直到满足特定条件。以下是一个示例

- name: Retry a task until a certain condition is met
  ansible.builtin.shell: /usr/bin/foo
  register: result
  until: result.stdout.find("all systems go") != -1
  retries: 5
  delay: 10

此任务最多运行 5 次,每次尝试之间延迟 10 秒。如果任何尝试的结果在其 stdout 中包含“all systems go”,则任务成功。 “retries”的默认值为 3,“delay”为 5。

要查看单个重试的结果,请使用-vv运行剧本。

当您使用until运行任务并将结果注册为变量时,注册的变量将包含一个名为“attempts”的键,用于记录任务的重试次数。

如果没有指定until,任务将一直重试,直到任务成功,但最多重试retries次(版本 2.16 中的新功能)。

您可以将until关键字与loopwith_<lookup>结合使用。 每个循环元素的任务结果都会在变量中注册,并且可以在until条件中使用。 以下是一个示例

- name: Retry combined with a loop
  uri:
    url: "https://{{ item }}.ansible.com"
    method: GET
  register: uri_output
  with_items:
  - "galaxy"
  - "docs"
  - "forum"
  - "www"
  retries: 2
  delay: 1
  until: "uri_output.status == 200"

注意

当您在循环中使用timeout关键字时,它适用于任务操作的每次尝试。 有关更多详细信息,请参阅TASK_TIMEOUT

循环遍历清单

通常,剧本本身就是对清单的循环,但有时您需要一个任务在不同的主机集上执行相同的操作。 要循环遍历清单或其子集,可以使用带有ansible_play_batchgroups变量的常规loop

- name: Show all the hosts in the inventory
  ansible.builtin.debug:
    msg: "{{ item }}"
  loop: "{{ groups['all'] }}"

- name: Show all the hosts in the current play
  ansible.builtin.debug:
    msg: "{{ item }}"
  loop: "{{ ansible_play_batch }}"

还有一个特定的查找插件inventory_hostnames,可以使用以下方式:

- name: Show all the hosts in the inventory
  ansible.builtin.debug:
    msg: "{{ item }}"
  loop: "{{ query('inventory_hostnames', 'all') }}"

- name: Show all the hosts matching the pattern, ie all but the group www
  ansible.builtin.debug:
    msg: "{{ item }}"
  loop: "{{ query('inventory_hostnames', 'all:!www') }}"

有关模式的更多信息,请参阅模式:目标主机和组

确保loop的列表输入:使用query而不是lookup

loop关键字需要一个列表作为输入,但lookup关键字默认情况下返回一个逗号分隔值的字符串。 Ansible 2.5 引入了一个名为query的新 Jinja2 函数,该函数始终返回一个列表,在使用loop关键字时,它提供了一个更简单的界面和更可预测的查找插件输出。

您可以通过使用wantlist=True强制lookup返回一个列表给loop,或者您可以使用query

以下两个示例执行相同的操作。

loop: "{{ query('inventory_hostnames', 'all') }}"

loop: "{{ lookup('inventory_hostnames', 'all', wantlist=True) }}"

向循环添加控制

版本 2.1 中的新功能。

loop_control关键字允许您以有用的方式管理循环。

使用label限制循环输出

版本 2.2 中的新功能。

当循环遍历复杂数据结构时,任务的控制台输出可能非常庞大。 要限制显示的输出,请将label指令与loop_control一起使用。

- name: Create servers
  digital_ocean:
    name: "{{ item.name }}"
    state: present
  loop:
    - name: server1
      disks: 3gb
      ram: 15Gb
      network:
        nic01: 100Gb
        nic02: 10Gb
        ...
  loop_control:
    label: "{{ item.name }}"

此任务的输出将仅显示每个itemname字段,而不是多行{{ item }}变量的全部内容。

注意

这是为了使控制台输出更易读,而不是为了保护敏感数据。 如果loop中存在敏感数据,请在任务上设置no_log: true以防止泄露。

在循环中暂停

版本 2.2 中的新功能。

要控制任务循环中每个项目执行之间的间隔时间(以秒为单位),请将pause指令与loop_control一起使用。

# main.yml
- name: Create servers, pause 3s before creating next
  community.digitalocean.digital_ocean:
    name: "{{ item }}"
    state: present
  loop:
    - server1
    - server2
  loop_control:
    pause: 3

使用index_var跟踪循环进度

版本 2.5 中的新功能。

要跟踪循环中的位置,请将index_var指令与loop_control一起使用。 此指令指定一个变量名称,用于包含当前循环索引。

- name: Count our fruit
  ansible.builtin.debug:
    msg: "{{ item }} with index {{ my_idx }}"
  loop:
    - apple
    - banana
    - pear
  loop_control:
    index_var: my_idx

注意

index_var 是从 0 开始索引的。

扩展的循环变量

版本 2.8 中的新功能。

从 Ansible 2.8 开始,您可以使用循环控制的extended选项获取扩展的循环信息。 此选项将公开以下信息。

变量

描述

ansible_loop.allitems

循环中所有项目的列表

ansible_loop.index

循环的当前迭代次数。 (从 1 开始索引)

ansible_loop.index0

循环的当前迭代次数。 (从 0 开始索引)

ansible_loop.revindex

距循环结束的迭代次数(从 1 开始索引)

ansible_loop.revindex0

距循环结束的迭代次数(从 0 开始索引)

ansible_loop.first

True 如果是第一次迭代

ansible_loop.last

True 如果是最后一次迭代

ansible_loop.length

循环中项目的数量

ansible_loop.previtem

循环前一次迭代中的项目。 第一次迭代时未定义。

ansible_loop.nextitem

循环下一次迭代中的项目。 最后一次迭代时未定义。

loop_control:
  extended: true

注意

使用loop_control.extended时,控制节点将使用更多内存。 这是因为ansible_loop.allitems包含对每个循环的完整循环数据的引用。 在将结果序列化以在主 ansible 进程中的回调插件中显示时,这些引用可能会被取消引用,导致内存使用量增加。

版本 2.14 中的新功能。

要禁用ansible_loop.allitems项目以减少内存消耗,请设置loop_control.extended_allitems: false

loop_control:
  extended: true
  extended_allitems: false

访问loop_var的名称

版本 2.8 中的新功能。

从 Ansible 2.8 开始,您可以使用ansible_loop_var变量获取提供给loop_control.loop_var的值的名称

对于角色作者,在编写允许循环的角色时,您可以通过以下方式收集值,而不是规定所需的loop_var值。

"{{ lookup('vars', ansible_loop_var) }}"

嵌套循环

虽然我们在这些示例中使用的是loop,但with_<lookup>也适用。

循环遍历嵌套列表

“嵌套”循环最简单的方法是避免嵌套循环,只需格式化数据即可获得相同的结果。 您可以使用 Jinja2 表达式来循环遍历复杂的列表。 例如,循环可以组合嵌套列表,从而模拟嵌套循环。

- name: Give users access to multiple databases
  community.mysql.mysql_user:
    name: "{{ item[0] }}"
    priv: "{{ item[1] }}.*:ALL"
    append_privs: true
    password: "foo"
  loop: "{{ ['alice', 'bob'] | product(['clientdb', 'employeedb', 'providerdb']) | list }}"

通过include_tasks堆叠循环

版本 2.1 中的新功能。

您可以使用include_tasks嵌套两个循环任务。 但是,默认情况下,Ansible 为每个循环设置循环变量item。 这意味着内部嵌套循环将覆盖外部循环的item的值。 要避免这种情况,您可以使用loop_control中的loop_var为每个循环指定变量的名称。

# main.yml
- include_tasks: inner.yml
  loop:
    - 1
    - 2
    - 3
  loop_control:
    loop_var: outer_item

# inner.yml
- name: Print outer and inner items
  ansible.builtin.debug:
    msg: "outer item={{ outer_item }} inner item={{ item }}"
  loop:
    - a
    - b
    - c

注意

如果 Ansible 检测到当前循环正在使用一个已经定义的变量,它将引发错误以使任务失败。

Until 和 loop

until条件将应用于loop的每个item

- debug: msg={{item}}
  loop:
    - 1
    - 2
    - 3
  retries: 2
  until: item > 2

这将导致 Ansible 针对前 2 个项目重试两次,然后在第三次尝试时失败,然后在第三个项目上的第一次尝试成功,最终导致任务整体失败。

[started TASK: debug on localhost]
FAILED - RETRYING: [localhost]: debug (2 retries left).Result was: {
    "attempts": 1,
    "changed": false,
    "msg": 1,
    "retries": 3
}
FAILED - RETRYING: [localhost]: debug (1 retries left).Result was: {
    "attempts": 2,
    "changed": false,
    "msg": 1,
    "retries": 3
}
failed: [localhost] (item=1) => {
    "msg": 1
}
FAILED - RETRYING: [localhost]: debug (2 retries left).Result was: {
    "attempts": 1,
    "changed": false,
    "msg": 2,
    "retries": 3
}
FAILED - RETRYING: [localhost]: debug (1 retries left).Result was: {
    "attempts": 2,
    "changed": false,
    "msg": 2,
    "retries": 3
}
failed: [localhost] (item=2) => {
    "msg": 2
}
ok: [localhost] => (item=3) => {
    "msg": 3
}
fatal: [localhost]: FAILED! => {"msg": "One or more items failed"}

从 with_X 迁移到 loop

在大多数情况下,循环最好使用 loop 关键字而不是 with_X 风格的循环。 loop 语法通常最适合使用过滤器而不是更复杂的 querylookup 使用。

以下示例展示了如何将许多常见的 with_ 风格循环转换为 loop 和过滤器。

with_list

with_list 直接替换为 loop

- name: with_list
  ansible.builtin.debug:
    msg: "{{ item }}"
  with_list:
    - one
    - two

- name: with_list -> loop
  ansible.builtin.debug:
    msg: "{{ item }}"
  loop:
    - one
    - two

with_items

with_itemsloopflatten 过滤器替换。

- name: with_items
  ansible.builtin.debug:
    msg: "{{ item }}"
  with_items: "{{ items }}"

- name: with_items -> loop
  ansible.builtin.debug:
    msg: "{{ item }}"
  loop: "{{ items|flatten(levels=1) }}"

with_indexed_items

with_indexed_itemsloopflatten 过滤器和 loop_control.index_var 替换。

- name: with_indexed_items
  ansible.builtin.debug:
    msg: "{{ item.0 }} - {{ item.1 }}"
  with_indexed_items: "{{ items }}"

- name: with_indexed_items -> loop
  ansible.builtin.debug:
    msg: "{{ index }} - {{ item }}"
  loop: "{{ items|flatten(levels=1) }}"
  loop_control:
    index_var: index

with_flattened

with_flattenedloopflatten 过滤器替换。

- name: with_flattened
  ansible.builtin.debug:
    msg: "{{ item }}"
  with_flattened: "{{ items }}"

- name: with_flattened -> loop
  ansible.builtin.debug:
    msg: "{{ item }}"
  loop: "{{ items|flatten }}"

with_together

with_togetherloopzip 过滤器替换。

- name: with_together
  ansible.builtin.debug:
    msg: "{{ item.0 }} - {{ item.1 }}"
  with_together:
    - "{{ list_one }}"
    - "{{ list_two }}"

- name: with_together -> loop
  ansible.builtin.debug:
    msg: "{{ item.0 }} - {{ item.1 }}"
  loop: "{{ list_one|zip(list_two)|list }}"

另一个使用复杂数据的示例

- name: with_together -> loop
  ansible.builtin.debug:
    msg: "{{ item.0 }} - {{ item.1 }} - {{ item.2 }}"
  loop: "{{ data[0]|zip(*data[1:])|list }}"
  vars:
    data:
      - ['a', 'b', 'c']
      - ['d', 'e', 'f']
      - ['g', 'h', 'i']

with_dict

with_dict 可以用 loopdictsortdict2items 过滤器替换。

- name: with_dict
  ansible.builtin.debug:
    msg: "{{ item.key }} - {{ item.value }}"
  with_dict: "{{ dictionary }}"

- name: with_dict -> loop (option 1)
  ansible.builtin.debug:
    msg: "{{ item.key }} - {{ item.value }}"
  loop: "{{ dictionary|dict2items }}"

- name: with_dict -> loop (option 2)
  ansible.builtin.debug:
    msg: "{{ item.0 }} - {{ item.1 }}"
  loop: "{{ dictionary|dictsort }}"

with_sequence

with_sequencelooprange 函数,以及可能的 format 过滤器替换。

- name: with_sequence
  ansible.builtin.debug:
    msg: "{{ item }}"
  with_sequence: start=0 end=4 stride=2 format=testuser%02x

- name: with_sequence -> loop
  ansible.builtin.debug:
    msg: "{{ 'testuser%02x' | format(item) }}"
  loop: "{{ range(0, 4 + 1, 2)|list }}"

循环范围不包括端点。

with_subelements

with_subelementsloopsubelements 过滤器替换。

- name: with_subelements
  ansible.builtin.debug:
    msg: "{{ item.0.name }} - {{ item.1 }}"
  with_subelements:
    - "{{ users }}"
    - mysql.hosts

- name: with_subelements -> loop
  ansible.builtin.debug:
    msg: "{{ item.0.name }} - {{ item.1 }}"
  loop: "{{ users|subelements('mysql.hosts') }}"

with_nested/with_cartesian

with_nestedwith_cartesian 被 loop 和 product 过滤器替换。

- name: with_nested
  ansible.builtin.debug:
    msg: "{{ item.0 }} - {{ item.1 }}"
  with_nested:
    - "{{ list_one }}"
    - "{{ list_two }}"

- name: with_nested -> loop
  ansible.builtin.debug:
    msg: "{{ item.0 }} - {{ item.1 }}"
  loop: "{{ list_one|product(list_two)|list }}"

with_random_choice

with_random_choice 被直接使用 random 过滤器替换,不需要 loop

- name: with_random_choice
  ansible.builtin.debug:
    msg: "{{ item }}"
  with_random_choice: "{{ my_list }}"

- name: with_random_choice -> loop (No loop is needed here)
  ansible.builtin.debug:
    msg: "{{ my_list|random }}"
  tags: random

另请参阅

Ansible 剧本

Playbook 简介

角色

按角色组织 Playbook

一般提示

Playbook 的技巧和窍门

条件

Playbook 中的条件语句

使用变量

关于变量的一切

用户邮件列表

有问题?访问 Google 论坛!

实时聊天

如何加入 Ansible 聊天频道