mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-06-27 18:50:21 -07:00
- Add filter `dict_kv` which returns a single key-value pair dictionary created from two arguments
Example 1
```
- hosts: localhost
gather_facts: false
vars:
myvar: myvalue
tasks:
- debug:
msg: "{{ myvar | dict_kv('thatsmyvar') }}"
OUTPUT:
ok: [localhost] => {
"msg": {
"thatsmyvar": "myvalue"
}
}
```
Example 2
```
- hosts: localhost
gather_facts: false
vars:
common_config:
type: host
database: all
myservers:
- server1
- server2
tasks:
- debug:
msg: "{{ myservers | map('dict_kv', 'server') | map('combine', common_config) }}"
OUTPUT:
ok: [localhost] => {
"msg": [
{
"database": "all",
"server": "server1",
"type": "host"
},
{
"database": "all",
"server": "server2",
"type": "host"
}
]
}
```
(cherry picked from commit 44fd157a2b
)
Co-authored-by: Stanislav German-Evtushenko <ginermail@gmail.com>
This commit is contained in:
parent
b86161886f
commit
659ef811a3
4 changed files with 84 additions and 0 deletions
2
changelogs/fragments/1264-dict_kv-new-filter.yaml
Normal file
2
changelogs/fragments/1264-dict_kv-new-filter.yaml
Normal file
|
@ -0,0 +1,2 @@
|
|||
minor_changes:
|
||||
- "Add new filter plugin ``dict_kv`` which returns a single key-value pair from two arguments. Useful for generating complex dictionaries without using loops. For example ``'value' | community.general.dict_kv('key'))`` evaluates to ``{'key': 'value'}`` (https://github.com/ansible-collections/community.general/pull/1264)."
|
Loading…
Add table
Add a link
Reference in a new issue