Skip to content

Latest commit

 

History

History
394 lines (357 loc) · 12.9 KB

ansible.utils.replace_keys_filter.rst

File metadata and controls

394 lines (357 loc) · 12.9 KB

ansible.utils.replace_keys

Replaces specific keys with their after value from a data recursively.

Version added: 2.5.0

  • This plugin replaces specific keys with their after value from a data recursively.
  • Matching parameter defaults to equals unless matching_parameter is explicitly mentioned.
  • Using the parameters below- data|ansible.utils.replace_keys(target([....]))
Parameter Choices/Defaults Configuration Comments
data
raw / required
This option represents a list of dictionaries or a dictionary with any level of nesting data.
For example config_data|ansible.utils.replace_keys(target([....])), in this case config_data represents this option.
matching_parameter
string
    Choices:
  • starts_with
  • ends_with
  • regex
Specify the matching configuration of target keys and data attributes.
target
list / elements=dictionary / required
Specify the target keys to replace in list of dictionaries format containing before and after key value.
after
string
after attribute key [change to]
before
string
before attribute key [to change]

# example.yaml
# interfaces:
#   - interface_name: eth0
#     enabled: true
#     duplex: auto
#     speed: auto
#   - interface_name: eth1
#     description: Configured by Ansible - Interface 1
#     mtu: 1500
#     speed: auto
#     duplex: auto
#     is_enabled: true
#     vifs:
#     - vlan_id: 100
#       description: Eth1 - VIF 100
#       mtu: 400
#       is_enabled: true
#     - vlan_id: 101
#       description: Eth1 - VIF 101
#       is_enabled: true
#   - interface_name: eth2
#     description: Configured by Ansible - Interface 2 (ADMIN DOWN)
#     mtu: 600
#     is_enabled: false

# Playbook
- name: replace keys with specified keys dict/list to dict
  ansible.builtin.set_fact:
    data: '{{ interfaces }}'
- debug:
    msg: >-
      {{ data|ansible.utils.replace_keys(target=[{'before':'interface_name',
      'after':'name'}, {'before':'is_enabled', 'after':'enabled'}]) }}

# Output
# TASK [replace keys with specified keys dict/list to dict] *************************
# ok: [localhost] => {
#     "ansible_facts": {
#         "data": [
#             {
#                 "duplex": "auto",
#                 "enabled": true,
#                 "interface_name": "eth0",
#                 "speed": "auto"
#             },
#             {
#                 "description": "Configured by Ansible - Interface 1",
#                 "duplex": "auto",
#                 "interface_name": "eth1",
#                 "is_enabled": true,
#                 "mtu": 1500,
#                 "speed": "auto",
#                 "vifs": [
#                     {
#                         "description": "Eth1 - VIF 100",
#                         "is_enabled": true,
#                         "mtu": 400,
#                         "vlan_id": 100
#                     },
#                     {
#                         "description": "Eth1 - VIF 101",
#                         "is_enabled": true,
#                         "vlan_id": 101
#                     }
#                 ]
#             },
#             {
#                 "description": "Configured by Ansible - Interface 2 (ADMIN DOWN)",
#                 "interface_name": "eth2",
#                 "is_enabled": false,
#                 "mtu": 600
#             }
#         ]
#     },
#     "changed": false
# }

# 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:
#   - interface_name: eth0
#     enabled: true
#     duplex: auto
#     speed: auto
#   - interface_name: eth1
#     description: Configured by Ansible - Interface 1
#     mtu: 1500
#     speed: auto
#     duplex: auto
#     is_enabled: true
#     vifs:
#     - vlan_id: 100
#       description: Eth1 - VIF 100
#       mtu: 400
#       is_enabled: true
#     - vlan_id: 101
#       description: Eth1 - VIF 101
#       is_enabled: true
#   - interface_name: eth2
#     description: Configured by Ansible - Interface 2 (ADMIN DOWN)
#     mtu: 600
#     is_enabled: false

# Playbook
- name: replace keys with specified keys dict/list to dict
  ansible.builtin.set_fact:
    data: '{{ interfaces }}'
- debug:
    msg: >-
      {{ data|ansible.utils.replace_keys(target=[{'before':'name',
      'after':'name'}, {'before':'enabled', 'after':'enabled'}],
      matching_parameter= 'ends_with') }}

# Output
# TASK [replace keys with specified keys dict/list to dict] *********************************
# ok: [localhost] => {
#     "ansible_facts": {
#         "data": [
#             {
#                 "duplex": "auto",
#                 "enabled": true,
#                 "interface_name": "eth0",
#                 "speed": "auto"
#             },
#             {
#                 "description": "Configured by Ansible - Interface 1",
#                 "duplex": "auto",
#                 "interface_name": "eth1",
#                 "is_enabled": true,
#                 "mtu": 1500,
#                 "speed": "auto",
#                 "vifs": [
#                     {
#                         "description": "Eth1 - VIF 100",
#                         "is_enabled": true,
#                         "mtu": 400,
#                         "vlan_id": 100
#                     },
#                     {
#                         "description": "Eth1 - VIF 101",
#                         "is_enabled": true,
#                         "vlan_id": 101
#                     }
#                 ]
#             },
#             {
#                 "description": "Configured by Ansible - Interface 2 (ADMIN DOWN)",
#                 "interface_name": "eth2",
#                 "is_enabled": false,
#                 "mtu": 600
#             }
#         ]
#     },
#     "changed": false
# }

# 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"
#         }
#     ]
# }

Authors

  • Sagar Paul (@KB-perByte)

Hint

Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.