这是我的示例 YAML 文件
input.yaml
。
name: "My name"
surname: "Bar"
address:
city: "My city"
street: "My street"
number: 1
使用 Ansible,如何将具有值
address.pincode
的 12345
添加到 input.yaml
?如何从 address.number
中删除 input.yaml
?
为了删除,我尝试了这个,它适用于 name,但不适用于 address.city
- name: "Removing key"
debug:
msg: "{{ my_data | ansible.utils.remove_keys(target=['name']) }}"
根据给定的评论和引用的 Ansible 文档,一个最小的示例
---
- hosts: localhost
become: false
gather_facts: false
vars:
input: "{{ lookup('file', 'input.yaml') | from_yaml }}"
tasks:
- debug:
msg: "{{ input }}"
- name: Add ZIP code
set_fact:
updated: "{{ input | combine( { 'address': { 'zip': 'MA 01914' } }, recursive=True ) }}"
- debug:
msg: "{{ updated | ansible.utils.remove_keys(target=['number']) }}"
将提供所请求的行为
TASK [debug] *********************************
ok: [localhost] =>
msg:
address:
city: Arkham
number: 1
street: Main Street
name: J.
surname: Doe
TASK [Add ZIP code] **************************
ok: [localhost]
TASK [debug] ************
ok: [localhost] =>
msg:
address:
city: Arkham
street: Main Street
zip: MA 01914
name: J.
surname: Doe