防止 Ansible 2 中出现重复的关键警告 [英] Prevent duplicate key warnings in Ansible 2

查看:43
本文介绍了防止 Ansible 2 中出现重复的关键警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的角色中使用了大量 YAML 锚点和引用来将逻辑保持在一个位置,而不是在多个任务中重复我自己.下面是一个非常非常基本的例子.

I use a lot of YAML anchors and references in my roles to keep the logic in a single spot instead of repeating myself in multiple tasks. Following is a very very basic example.

- &sometask
  name: "Some Task"
  some_module: with a lot of parameters
  with_items: list_A

- <<: *sometask
  name: "Some OTHER Task"
  with_items: list_B

这个例子可能没有说明这实际上是如何有用的,但它确实有用.想象一下,您遍历一个 dict 列表,将每个 dict 中的各种键传递给模块,可能具有非常复杂的when"、failed_when"和changed_when"条件.你只是想干.

因此,我没有将整个任务定义两次,而是使用第一个任务的锚点并将其所有内容合并到一个新任务中,然后覆盖不同的部分.效果很好.

So instead of defining the whole task twice, I use an anchor to the first one and merge all its content into a new task, then override the differing pieces. That works fine.

需要说明的是,这是基本的 YAML 功能,与 Ansible 本身无关.

Just to be clear, this is basic YAML functionality and has nothing to do with Ansible itself.

上述定义的结果(以及 Ansible 在解析 YAML 文件时看到的)将评估为:

The result of above definition (and what Ansible sees when it parsed the YAML file) would evaluate to:

- name: "Some Task"
  some_module: with a lot of parameters
  with_items: list_A

- name: "Some Task"
  some_module: with a lot of parameters
  with_items: list_A
  name: "Some OTHER Task"
  with_items: list_B

Ansible 2 现在有一项功能,可以在任务中多次定义键时进行投诉.它仍然有效,但在运行剧本时会产生不必要的噪音:

Ansible 2 now has a feature to complain when keys have been defined multiple times in a task. It still works, but creates unwanted noise when running the playbook:

TASK [Some OTHER Task] *******************************************************
 [WARNING]: While constructing a mapping from /some/file.yml, line 42, column 3, found a duplicate dict key (name).  Using last defined value only.

 [WARNING]: While constructing a mapping from /some/file.yml, line 42, column 3, found a duplicate dict key (with_items).  Using last defined value only.

Ansible 配置允许防止 deprecation_warningscommand_warnings.有没有办法也防止这种警告?

Ansible configuration allows to prevent deprecation_warnings and command_warnings. Is there a way to also prevent this kind of warning?

推荐答案

从 Ansible 2.9.0 开始,这可以通过将 ANSIBLE_DUPLICATE_YAML_DICT_KEY 环境变量设置为 ignore 来实现.此变量的其他可能值是 warn,这是默认值并保留原始行为,以及 error,这会使剧本执行失败.

As of Ansible 2.9.0, this can be achieved by setting the ANSIBLE_DUPLICATE_YAML_DICT_KEY environment variable to ignore. Other possible values for this variable are warn, which is the default and preserves the original behaviour, and error, which makes the playbook execution fail.

有关实施的详细信息,请参阅拉取请求.

See this pull request for details about the implementation.

这篇关于防止 Ansible 2 中出现重复的关键警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆