Ansible:使用节点定位和合并键在不同yaml文件上拆分

Ansible:使用节点定位和合并键在不同yaml文件上拆分,ansible,yaml,Ansible,Yaml,我很难让Ansible使用YAML合并键和节点锚,我想知道当它们在不同的文件中时,它们是否可以工作,还有什么替代方法 我正在尝试定义默认数据结构(请参见vars/default/vars.yaml),并将其合并到更具体的版本(请参见vars/specific/vars.yaml),尽管它们位于不同的文件中: 比如说, playbook/ ├── my_playbook.yaml ├── tasks │   └── example.yaml └── vars ├── default

我很难让Ansible使用YAML合并键和节点锚,我想知道当它们在不同的文件中时,它们是否可以工作,还有什么替代方法

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

比如说,

playbook/
├── my_playbook.yaml
├── tasks
│   └── example.yaml
└── vars
    ├── default
    │   └── vars.yaml
    └── specific
        └── vars.yaml
重新创建此问题的文件的内容如下:

playbook/my_playbook.yaml

---
- hosts:            "local"
  tasks:
    - include_tasks: "tasks/example.yaml"
- name: include default and specific
  include_vars:
    file: "{{item}}"
  with_items:
      - "default/vars.yaml"
      - "specific/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"
---
process_settings:
    <<: *default_process_settings
    heartbeat_rate:    "5"
playbook/tasks/example.yaml

---
- hosts:            "local"
  tasks:
    - include_tasks: "tasks/example.yaml"
- name: include default and specific
  include_vars:
    file: "{{item}}"
  with_items:
      - "default/vars.yaml"
      - "specific/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"
---
process_settings:
    <<: *default_process_settings
    heartbeat_rate:    "5"
剧本/vars/default/vars.yaml

---
- hosts:            "local"
  tasks:
    - include_tasks: "tasks/example.yaml"
- name: include default and specific
  include_vars:
    file: "{{item}}"
  with_items:
      - "default/vars.yaml"
      - "specific/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"
---
process_settings:
    <<: *default_process_settings
    heartbeat_rate:    "5"
剧本/vars/specific/vars.yaml

---
- hosts:            "local"
  tasks:
    - include_tasks: "tasks/example.yaml"
- name: include default and specific
  include_vars:
    file: "{{item}}"
  with_items:
      - "default/vars.yaml"
      - "specific/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"
---
process_settings:
    <<: *default_process_settings
    heartbeat_rate:    "5"
我得到了以下神秘的错误:

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"}
任务[包括默认和特定]***********************************************************************************************
确定:[127.0.0.1]=>(项=默认值/vars.yaml)
失败:[127.0.0.1](item=specific/vars.yaml)=>{“ansible_事实”:{},“ansible_包含的_var_文件”:[],“更改”:false,“失败”:true,“item”:“specific/vars.yaml”,“消息”:加载YAML时出现语法错误。\n\n\n错误似乎出现在'True'中:第4行第9列,但可能\n出现在文件的其他位置,具体取决于语法问题。\n\n(无法打开文件以显示行)\n异常类型:\n异常:在\“\”第4行第9列找到未定义的别名\n
Ansible似乎可以从不同的YAML文件中提取变量,但是使用节点锚和合并键的YAML引用只有在同一个文件中才能工作。从纯YAML的角度来看,我认为这是合理的

我怎样才能让它发挥作用?有没有其他方法可以实现这个目标

我很难让Ansible使用YAML合并键和节点锚,我想知道当它们在不同的文件中时,它们是否可以工作,还有什么替代方法

合并键和节点定位不能跨文件使用。它们仅在单个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"}
“异常:发现未定义的别名”似乎准确地描述了问题

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

您可以使用
组合
过滤器:

process_settings: "{{ default_process_settings|combine({'heartbeat_rate':    '5'}) }}"
同样,但可能更容易阅读(也更容易编写,特别是如果您有多个单键):

虽然没有提到任何关于多个文档的内容

但是:

alias节点使用以前未在文档中出现的锚点是错误的


因此,如果别名在同一个文件中,但引用了不同的文档,并且您试图引用不同文件中文档中的锚点,则该别名无效。

这是一个不错的解决方案。遗憾的是,该解决方案不够通用,无法使用相同的变量名(即
进程设置):{{process_settings | combine({'heartbeat_rate':'5'}}}{/code>)不会导致递归错误,但我想这是允许延迟求值的折衷。