YAML:如何覆盖变量
我有一个YAML,如下所示:YAML:如何覆盖变量,yaml,Yaml,我有一个YAML,如下所示: defaults: &defaults username: admin password: admin agents: blueprint: &agent_blueprint version: '1' auth: username: agent password: secret supported_features: - state - l
defaults: &defaults
username: admin
password: admin
agents:
blueprint: &agent_blueprint
version: '1'
auth:
username: agent
password: secret
supported_features:
- state
- lifecycle
- credentials
- backup
- restore
services:
- &blueprint_service
id: '24731fb8-7b84-4f57-914f-c3d55d793dd4'
name: 'blueprint'
plans:
- id: 'bc158c9a-7934-401e-94ab-057082a5073f'
name: 'v1.0-dedicated-xsmall'
free: false
manager:
name: 'director'
settings:
prefix: 'blueprint'
agent: *agent_blueprint
context:
agent: *agent_blueprint
现在,我想添加另一个节点来覆盖服务阵列中的支持的\u功能
节点。所以,我添加了类似这样的内容
development:
<<: *defaults
agents:
blueprint:
<<: *agent_blueprint
supported_features:
- state
- lifecycle
- credentials
services:
- *blueprint_service
开发:
在YAML中没有变量,我认为您对YAML的实际功能有误解
在第二个YAML块中,您可以执行以下操作:
development:
<<: *defaults
无效,因为您覆盖了单个键blueprint
在此之后,您可以:
blueprint: &agent_blueprint
<<: *agent_blueprint
supported_features:
其中,*blueprint\u服务
扩展为:
id: 24731fb8-7b84-4f57-914f-c3d55d793dd4
name: blueprint
plans:
- free: false
id: bc158c9a-7934-401e-94ab-057082a5073f
manager:
name: director
settings:
agent: *agent_blueprint
context:
agent: *agent_blueprint
prefix: blueprint
name: v1.0-dedicated-xsmall
因为在第一个YAML块中定义锚定时,锚定生效。和*代理\u蓝图
,以:
auth: {password: secret, username: agent}
supported_features: [state, lifecycle, credentials, backup, restore]
version: '1'
没有“更新”。解析别名时,别名根据锚定的定义获取其值。我对我的问题进行了一些编辑,以便更容易理解。我有默认的合并节点,因为它下面有更多的节点。我现在补充说。正如您提到的,因为代理下只有一个节点,所以我删除了合并的节点。但是,正如您所说,的合并节点没有更新,如果我需要更新/覆盖这些值,如何正确定义它?@Subhankar使用Python脚本使用我的ruamel.yaml
库更新这些值(最好是简单高效),或者将其制作成模板,然后从那里生成yaml(仅当您的环境已经有这样的模板时才这样做)。锚定/别名机制存在的原因与定义变量值(稍后使用)不同,并且不够灵活,无法满足您的需要。
id: 24731fb8-7b84-4f57-914f-c3d55d793dd4
name: blueprint
plans:
- free: false
id: bc158c9a-7934-401e-94ab-057082a5073f
manager:
name: director
settings:
agent: *agent_blueprint
context:
agent: *agent_blueprint
prefix: blueprint
name: v1.0-dedicated-xsmall
auth: {password: secret, username: agent}
supported_features: [state, lifecycle, credentials, backup, restore]
version: '1'