我在我的角色中使用了大量的YAML錨點和引用來將邏輯保留在一個地方,而不是在多個任務中重複自己。以下是一個非常基本的例子。在Ansible 2中防止重複鍵警告
- &sometask
name: "Some Task"
some_module: with a lot of parameters
with_items: list_A
- <<: *sometask
name: "Some OTHER Task"
with_items: list_B
這個例子可能不會顯示這個實際上是如何是有用的,但它是。想象一下,你循環了一系列的字典,將每個字典中的各個鍵傳遞給模塊,可能有相當複雜的「when」,「failed_when」和「changed_when」條件。你只是想幹。
因此,而不是定義兩次全年任務的,我用一個錨的第一個和它的所有內容合併成一個新的任務,然後覆蓋不同部分。這工作正常。
需要說明的是,這是基本的YAML功能,與Ansible本身無關。
上述定義(什麼Ansible時看到在解析YAML文件)的結果將評估爲:
- 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現在有一個特點,當鑰匙已經在任務被定義多次抱怨。它仍然有效,但在運行時,劇本產生不必要的噪音:
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_warnings
和command_warnings
。有沒有辦法來防止這種警告?
這裏有趣的是我正要提交一個拉請求來使這個確切的警告致命,因爲它掩蓋了劇本中的實際錯誤。現在我想知道是否應該有某種' - 警告是致命的'標誌,而不是...... – larsks