ansible.utils.remove_keys 过滤器 – 递归删除数据中的特定键。
注意
此过滤器插件是 ansible.utils 集合 (版本 5.1.2) 的一部分。
如果您使用的是 ansible
包,则可能已安装此集合。它不包含在 ansible-core
中。要检查是否已安装它,请运行 ansible-galaxy collection list
。
要安装它,请使用: ansible-galaxy collection install ansible.utils
。
要在剧本中使用它,请指定: ansible.utils.remove_keys
。
ansible.utils 2.5.0 中的新增功能
概要
此插件递归删除提供的數據中的特定鍵。
除非明确提到
matching_parameter
,否则匹配参数默认为等于。使用以下参数 -
data|ansible.utils.remove_keys(target([....]
))
关键字参数
这描述了过滤器的关键字参数。这些是在以下示例中的值 key1=value1
,key2=value2
等: input | ansible.utils.remove_keys(key1=value1, key2=value2, ...)
参数 |
注释 |
---|---|
此选项表示具有任意嵌套级别数据的字典列表或字典。 例如 |
|
指定目标键和数据属性的匹配配置。 选择
|
|
以列表格式指定要删除的目标键。 |
示例
# example.yaml
# interfaces:
# - name: eth0
# enabled: true
# duplex: auto
# speed: auto
# note:
# - Connected green wire
# - name: eth1
# description: Configured by Ansible - Interface 1
# mtu: 1500
# speed: auto
# duplex: auto
# enabled: true
# note:
# - Connected blue wire
# - Configured by Paul
# vifs:
# - vlan_id: 100
# description: Eth1 - VIF 100
# mtu: 400
# enabled: true
# comment: Needs reconfiguration
# - vlan_id: 101
# description: Eth1 - VIF 101
# enabled: true
# - name: eth2
# description: Configured by Ansible - Interface 2 (ADMIN DOWN)
# mtu: 600
# enabled: false
# Playbook
- name: remove multiple keys from a provided data
ansible.builtin.set_fact:
data: '{{ interfaces }}'
- debug:
msg: '{{ data|ansible.utils.remove_keys(target=[''note'', ''comment'']) }}'
# Output
# TASK [remove multiple keys from a provided data] ***************************************
# ok: [localhost] => {
# "ansible_facts": {
# "data": [
# {
# "duplex": "auto",
# "enabled": true,
# "name": "eth0",
# "note": [
# "Connected green wire"
# ],
# "speed": "auto"
# },
# {
# "description": "Configured by Ansible - Interface 1",
# "duplex": "auto",
# "enabled": true,
# "mtu": 1500,
# "name": "eth1",
# "note": [
# "Connected blue wire",
# "Configured by Paul"
# ],
# "speed": "auto",
# "vifs": [
# {
# "comment": "Needs reconfiguration",
# "description": "Eth1 - VIF 100",
# "enabled": true,
# "mtu": 400,
# "vlan_id": 100
# },
# {
# "description": "Eth1 - VIF 101",
# "enabled": true,
# "vlan_id": 101
# }
# ]
# },
# {
# "description": "Configured by Ansible - Interface 2 (ADMIN DOWN)",
# "enabled": false,
# "mtu": 600,
# "name": "eth2"
# }
# ]
# },
# "changed": false
# }
# Read vars_file 'example.yaml'
# TASK [debug] ********************************************
# ok: [localhost] => {
# "msg": [
# {
# "duplex": "auto",
# "enabled": true,
# "name": "eth0",
# "speed": "auto"
# },
# {
# "description": "Configured by Ansible - Interface 1",
# "duplex": "auto",
# "enabled": true,
# "mtu": 1500,
# "name": "eth1",
# "speed": "auto",
# "vifs": [
# {
# "description": "Eth1 - VIF 100",
# "enabled": true,
# "mtu": 400,
# "vlan_id": 100
# },
# {
# "description": "Eth1 - VIF 101",
# "enabled": true,
# "vlan_id": 101
# }
# ]
# },
# {
# "description": "Configured by Ansible - Interface 2 (ADMIN DOWN)",
# "enabled": false,
# "mtu": 600,
# "name": "eth2"
# }
# ]
# }
# example.yaml
# interfaces:
# - name: eth0
# enabled: true
# duplex: auto
# speed: auto
# note:
# - Connected green wire
# - name: eth1
# description: Configured by Ansible - Interface 1
# mtu: 1500
# speed: auto
# duplex: auto
# enabled: true
# note:
# - Connected blue wire
# - Configured by Paul
# vifs:
# - vlan_id: 100
# description: Eth1 - VIF 100
# mtu: 400
# enabled: true
# comment: Needs reconfiguration
# - vlan_id: 101
# description: Eth1 - VIF 101
# enabled: true
# - name: eth2
# description: Configured by Ansible - Interface 2 (ADMIN DOWN)
# mtu: 600
# enabled: false
# Playbook
- name: remove multiple keys from a provided data
ansible.builtin.set_fact:
data: '{{ interfaces }}'
- debug:
msg: >-
{{ data|ansible.utils.remove_keys(target=['^note$', '^comment'],
matching_parameter= 'regex') }}
# Output
# TASK [remove multiple keys from a provided data] ***********************
# ok: [localhost] => {
# "ansible_facts": {
# "data": [
# {
# "duplex": "auto",
# "enabled": true,
# "name": "eth0",
# "note": [
# "Connected green wire"
# ],
# "speed": "auto"
# },
# {
# "description": "Configured by Ansible - Interface 1",
# "duplex": "auto",
# "enabled": true,
# "mtu": 1500,
# "name": "eth1",
# "note": [
# "Connected blue wire",
# "Configured by Paul"
# ],
# "speed": "auto",
# "vifs": [
# {
# "comment": "Needs reconfiguration",
# "description": "Eth1 - VIF 100",
# "enabled": true,
# "mtu": 400,
# "vlan_id": 100
# },
# {
# "description": "Eth1 - VIF 101",
# "enabled": true,
# "vlan_id": 101
# }
# ]
# },
# {
# "description": "Configured by Ansible - Interface 2 (ADMIN DOWN)",
# "enabled": false,
# "mtu": 600,
# "name": "eth2"
# }
# ]
# },
# "changed": false
# }
# Read vars_file 'example.yaml'
# TASK [debug] *****************************************
# ok: [localhost] => {
# "msg": [
# {
# "duplex": "auto",
# "enabled": true,
# "name": "eth0",
# "speed": "auto"
# },
# {
# "description": "Configured by Ansible - Interface 1",
# "duplex": "auto",
# "enabled": true,
# "mtu": 1500,
# "name": "eth1",
# "speed": "auto",
# "vifs": [
# {
# "description": "Eth1 - VIF 100",
# "enabled": true,
# "mtu": 400,
# "vlan_id": 100
# },
# {
# "description": "Eth1 - VIF 101",
# "enabled": true,
# "vlan_id": 101
# }
# ]
# },
# {
# "description": "Configured by Ansible - Interface 2 (ADMIN DOWN)",
# "enabled": false,
# "mtu": 600,
# "name": "eth2"
# }
# ]
# }