ansible.utils.keep_keys 过滤器 – 递归地从数据中保留特定的键。
注意
此过滤器插件是 ansible.utils 集合(版本 5.1.2)的一部分。
如果您正在使用 ansible
包,您可能已经安装了这个集合。它不包含在 ansible-core
中。要检查它是否已安装,请运行 ansible-galaxy collection list
。
要安装它,请使用:ansible-galaxy collection install ansible.utils
。
要在 playbook 中使用它,请指定:ansible.utils.keep_keys
。
ansible.utils 2.5.0 中的新增功能
概要
此插件递归地仅保留提供的指定数据的键。
匹配参数默认为等于,除非显式提及
matching_parameter
。使用以下参数 -
data|ansible.utils.keep_keys(target([....]
))
关键字参数
这描述了过滤器的关键字参数。这些是在以下示例中 key1=value1
、key2=value2
等值:input | ansible.utils.keep_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: keep selective keys from dict/list of dict data
ansible.builtin.set_fact:
data: '{{ interfaces }}'
- debug:
msg: >-
{{ data|ansible.utils.keep_keys(target=['description', 'name', 'mtu',
'duplex', 'enabled', 'vifs', 'vlan_id']) }}
# Output
# TASK [keep selective keys from python dict/list of dict] ****************************************************************************************
# 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"
# },
# {
# "description": "Configured by Ansible - Interface 1",
# "duplex": "auto",
# "enabled": true,
# "mtu": 1500,
# "name": "eth1",
# "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"
# }
# ]
# }
# Playbook
- name: keep selective keys from dict/list of dict data
ansible.builtin.set_fact:
data: "{{ interfaces }}"
- debug:
msg: "{{ data|ansible.utils.keep_keys(target=['desc', 'name'], matching_parameter= 'starts_with') }}"
# Output
# TASK [keep selective keys from python dict/list of dict] **************************
# 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": [
# {
# "name": "eth0"
# },
# {
# "description": "Configured by Ansible - Interface 1",
# "name": "eth1",
# "vifs": [
# {
# "description": "Eth1 - VIF 100"
# },
# {
# "description": "Eth1 - VIF 101"
# }
# ]
# },
# {
# "description": "Configured by Ansible - Interface 2 (ADMIN DOWN)",
# "name": "eth2"
# }
# ]
# }