Ansible-core 2.15 迁移指南

本节讨论了 ansible-core 2.14 和 ansible-core 2.15 之间的行为变化。

本指南旨在帮助您更新 Playbook、插件以及 Ansible 基础设施的其他部分,以便它们能与此版本的 Ansible 协同工作。

我们建议您在阅读本页的同时参考 ansible-core 2.15 更新日志 (Changelog),以了解您可能需要进行的更新。

本文档是移植系列指南的一部分。完整的移植指南列表可以在 移植指南 中找到。

Playbook

  • 条件判断 - 由于在 ansible-core 2.15.7 中修复了安全漏洞 CVE-2023-5764,当内嵌模板引用来自不可信源(如模块结果或标记为 !unsafe 的变量)的数据时,包含内嵌模板块的条件表达式可能会失败,并报错“Conditional is marked as unsafe, and cannot be evaluated.”。当引用不可信数据时,带有内嵌模板的条件判断可能成为恶意模板注入的源头,且几乎总是可以在不使用内嵌模板的情况下重写。诸如 whenuntil 等 Playbook 任务条件关键字早已显示警告,不建议在条件中使用内嵌模板;该警告现已扩展至非任务条件,例如 assert 操作。

    - name: task with a module result (always untrusted by Ansible)
      shell: echo "hi mom"
      register: untrusted_result
    
    # don't do it this way...
    # - name: insecure conditional with embedded template consulting untrusted data
    #   assert:
    #     that: '"hi mom" is in {{ untrusted_result.stdout }}'
    
    - name: securely access untrusted values directly as Jinja variables instead
      assert:
        that: '"hi mom" is in untrusted_result.stdout'
    

Handlers(处理器)

正如文档所述,如果定义了多个同名的 handler,则在被通知时执行的是最后添加到 play 中的那个。在 ansible-core 2.15 之前,通过 include_role 任务动态包含到 play 中的 handler 并非如此。此问题已在 ansible-core 2.15 中得到解决,依赖于 ansible-core 2.14 及更早版本行为的用户可能需要相应地调整其 playbook。

关于行为变化的示例,请参考以下内容:

- include_role:
    name: foo
  vars:
    invocation: 1

- block:
   - include_role:
       name: foo
     vars:
       invocation: 2
  when: inventory_hostname == "bar"

- meta: flush_handlers

注意

该示例假设角色 foo 中有一个任务会通知角色 foo 内部名为 foo_handler 的 handler。

注意

由于不同的变量及其值被附加到包含同一个角色的 include_role 任务中,这使得它们成为了不同的角色。

注意

第二次调用 include_role 任务会导致无论 when 条件评估结果如何,都会包含该角色的任务和 handler。when 条件被附加到包裹 include_role 任务的 block 中,因此 when 条件在角色被包含到 play 之后,将应用于该角色中的所有任务和 handler。

flush_handlers 任务运行时,所有主机在第一次调用 include_role 时都通知了 foo_handler。此外,主机 bar(由于 when 限制了所有其他主机)在第二次调用 include_role 期间再次通知了 foo_handler

ansible-core 2.15 中,最后添加到 play 中的名为 foo_handler 的 handler 来自第二次 include_role 调用,因此它附加了 when: inventory_hostname == "bar",导致该 handler 实际上仅在主机 bar 上运行,而在所有其他主机上被跳过。因此,来自主机 bar 的通知已被去重。

ansible-core 2.14 及更早版本中,第一次调用产生的 foo_handler 在所有主机上运行。此外,第二次调用产生的 foo_handler 在主机 bar 上再次运行。

命令行

  • ansible-galaxy search 的返回码现在是 0 而不是 1,并且当结果为空时,标准输出(stdout)为空,以与其他 ansible-galaxy 命令保持一致。

弃用 (Deprecated)

  • 弃用了向 vars: 提供字典列表的做法,建议改为提供一个字典。

    与其使用

    vars:
      - var1: foo
      - var2: bar
    

    请使用

    vars:
      var1: foo
      var2: bar
    

模块

无显著变更

已移除的模块

以下模块不再存在

  • 无显著变更

弃用通知

无显著变更

值得注意的模块变化

无显著变更

插件

无显著变更

移植自定义脚本

无显著变更

网络

无显著变更