Фильтр community.general.replace_keys — замена определённых ключей в списке словарей
Примечание
Этот плагин фильтра является частью коллекции community.general (версия 12.6.1).
Возможно, эта коллекция уже установлена, если вы используете пакет ansible. Она не входит в ansible-core. Чтобы проверить, установлена ли она, выполните ansible-galaxy collection list.
Для установки используйте: ansible-galaxy collection install community.general.
Чтобы использовать её в плейбуке, укажите: community.general.replace_keys.
Добавлено в community.general 9.1.0
Краткое описание
- Этот фильтр заменяет указанные ключи в заданном списке словарей.
Входные данные
Здесь описаны входные данные фильтра — значение перед | community.general.replace_keys.
Параметр | Комментарии |
|---|---|
Input list / элементы=dictionary / обязательный | Список словарей. Ключи верхнего уровня должны быть строками. |
Именованные параметры
Здесь описаны именованные параметры фильтра. Это значения key1=value1, key2=value2 и так далее в следующем примере: input | community.general.replace_keys(key1=value1, key2=value2, ...)
Параметр | Комментарии |
|---|---|
matching_parameter string | Задаёт способ сопоставления целевых ключей. Варианты:
|
target list / элементы=dictionary / обязательный | Список словарей с атрибутами Значение |
|
after string |
Значение, на которое заменяется совпавший ключ. |
|
before string |
Ключ или шаблон ключа для замены. Интерпретация Для ключа, которому соответствуют несколько |
Примеры
- l:
- {k0_x0: A0, k1_x1: B0, k2_x2: [C0], k3_x3: foo}
- {k0_x0: A1, k1_x1: B1, k2_x2: [C1], k3_x3: bar}
# 1) By default, replace keys that are equal any of the attributes before.
- t:
- {before: k0_x0, after: a0}
- {before: k1_x1, after: a1}
r: "{{ l | community.general.replace_keys(target=t) }}"
# 2) Replace keys that starts with any of the attributes before.
- t:
- {before: k0, after: a0}
- {before: k1, after: a1}
r: "{{ l | community.general.replace_keys(target=t, matching_parameter='starts_with') }}"
# 3) Replace keys that ends with any of the attributes before.
- t:
- {before: x0, after: a0}
- {before: x1, after: a1}
r: "{{ l | community.general.replace_keys(target=t, matching_parameter='ends_with') }}"
# 4) Replace keys that match any regex of the attributes before.
- t:
- {before: "^.*0_x.*$", after: a0}
- {before: "^.*1_x.*$", after: a1}
r: "{{ l | community.general.replace_keys(target=t, matching_parameter='regex') }}"
# The results of above examples 1-4 are all the same.
- r:
- {a0: A0, a1: B0, k2_x2: [C0], k3_x3: foo}
- {a0: A1, a1: B1, k2_x2: [C1], k3_x3: bar}
# 5) If more keys match the same attribute before the last one will be used.
- t:
- {before: "^.*_x.*$", after: X}
r: "{{ l | community.general.replace_keys(target=t, matching_parameter='regex') }}"
# gives
- r:
- X: foo
- X: bar
# 6) If there are items with equal attribute before the first one will be used.
- t:
- {before: "^.*_x.*$", after: X}
- {before: "^.*_x.*$", after: Y}
r: "{{ l | community.general.replace_keys(target=t, matching_parameter='regex') }}"
# gives
- r:
- X: foo
- X: bar
# 7) If there are more matches for a key the first one will be used.
- l:
- {aaa1: A, bbb1: B, ccc1: C}
- {aaa2: D, bbb2: E, ccc2: F}
- t:
- {before: a, after: X}
- {before: aa, after: Y}
r: "{{ l | community.general.replace_keys(target=t, matching_parameter='starts_with') }}"
# gives
- r:
- {X: A, bbb1: B, ccc1: C}
- {X: D, bbb2: E, ccc2: F}
Возвращаемое значение
Ключ | Описание |
|---|---|
Возвращаемое значение list / элементы=dictionary | Список словарей с заменёнными ключами. Возвращается: success |
Ссылки на коллекцию
© 2012–2018 Michael DeHaan
© 2018–2025 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/projects/ansible/latest/collections/community/general/replace_keys_filter.html