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

查看:97
本文介绍了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"

剧本/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"

剧本/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.\n\n\nThe error appears to have been in 'True': line 4, column 9, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\n(could not open file to display line)\nexception type: <class 
'yaml.composer.ComposerError'>\nexception: found undefined alias\n  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.

我收到以下神秘错误:

I get the following cryptic error:

异常:找到未定义的别名"似乎可以准确地描述问题.

"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 过滤器:

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天全站免登陆