Ansible:使用节点锚点和合并键拆分不同的 yaml 文件 [英] Ansible: Using node anchors and merge keys split over different yaml files

查看:21
本文介绍了Ansible:使用节点锚点和合并键拆分不同的 yaml 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难让 Ansible 使用 YAML 合并键和节点锚点,并且想知道当它们位于不同文件中时它们是否可以工作以及可能的替代方法.

I'm having difficulty getting Ansible to work with YAML merge keys and node anchors and wondering whether these can work when they are in different files and what might be an alternative approach.

我正在尝试定义默认数据结构(参见 vars/default/vars.yaml)并将其合并到更具体的版本(参见 vars/specific/vars.yaml)虽然它们在不同的文件中:

I'm attempting to define default data structure (see vars/default/vars.yaml) and merge this in to a more specific version (see vars/specific/vars.yaml) though these are in different files:

例如,

playbook/
├── my_playbook.yaml
├── tasks
│   └── example.yaml
└── vars
    ├── default
    │   └── vars.yaml
    └── specific
        └── vars.yaml

重现此问题的文件内容如下:

The contents of the files which recreates this issue are as follows:

playbook/my_playbook.yaml

---
- hosts:            "local"
  tasks:
    - include_tasks: "tasks/example.yaml"

playbook/tasks/example.yaml

- name: include default and specific
  include_vars:
    file: "{{item}}"
  with_items:
      - "default/vars.yaml"
      - "specific/vars.yaml"

playbook/vars/default/vars.yaml

---
process_settings: &default_process_settings
    kill_timeout:      "10"
    log_retention:     "5"
    retry_times:       "3"
    alert_email:       "process.alert@testsite.com",
    deploy_server:     "http://testsite.com:8000"

playbook/vars/specific/vars.yaml

---
process_settings:
    <<: *default_process_settings
    heartbeat_rate:    "5"

正是在最后一个文件中,似乎导致了问题.当我运行剧本时:

It's in this last file that appears to be causing the problem. When I run the playbook:

ansible-playbook -i inventory playbook/my_playbook.yaml

我收到以下神秘错误:

TASK [include default and specific] ***********************************************************************************************
ok: [127.0.0.1] => (item=default/vars.yaml)
failed: [127.0.0.1] (item=specific/vars.yaml) => {"ansible_facts": {}, "ansible_included_var_files": [], "changed": false, "failed": true, "item": "specific/vars.yaml", "message": "Syntax Error while loading YAML.


The error appears to have been in 'True': line 4, column 9, but may
be elsewhere in the file depending on the exact syntax problem.

(could not open file to display line)
exception type: <class 
'yaml.composer.ComposerError'>
exception: found undefined alias
  in "<unicode string>", line 4, column 9"}

Ansible 似乎可以从不同的 YAML 文件中获取变量,但使用节点锚点和合并键的 YAML 引用仅在同一个文件中有效.从纯粹的 YAML 角度来看,我认为这是足够合理的.

It seems that Ansible can pick up variable from different YAML files but YAML references using node anchors and merge keys will only work when in the same file. From a purely YAML perspective, this is reasonable enough I suppose.

我怎样才能让它工作?是否有其他方法可以实现这一目标?

How can I get this to work? Is there another approach that can achieve this aim?

推荐答案

我很难让 Ansible 使用 YAML 合并键和节点锚点,并且想知道当它们位于不同文件中时它们是否可以工作以及可能的替代方法.

I'm having difficulty getting Ansible to work with YAML merge keys and node anchors and wondering whether these can work when they are in different files and what might be an alternative approach.

合并键和节点锚不能跨文件使用.它们仅在单个 YAML 文档中有用.

Merge keys and node anchors cannot be used across files. They are only useful within a single YAML document.

我收到以下神秘错误:

异常:发现未定义的别名"似乎准确地描述了问题.

"exception: found undefined alias" seems to accurately describe the problem.

谁能建议这是否/如何工作,或者是否有其他方法可以实现这一目标?

Can anyone suggest whether/how this can work or whether there's another approach that can achieve this aim?

您可以使用 combine 过滤器:

You can use the combine filter:

process_settings: "{{ default_process_settings|combine({'heartbeat_rate':    '5'}) }}"

相同,但可能更易于阅读(并且更易于编写,尤其是在您拥有多个键的情况下):

The same, but perhaps easier to read (and easier to write, especially if you've got more than a single key):

override_process_settings:
    heartbeat_rate:    "5"

process_settings: "{{ default_process_settings|combine(override_process_settings) }}"


这篇关于Ansible:使用节点锚点和合并键拆分不同的 yaml 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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