Ansible form the list or separate the dictionary - ansible

I have the below dicotionary value
{
"1application": "abc",
"2service": "def",
"3service": ghi",
"4application": "jkl",
"5service": "mno",
"6application": "abc",
"7service": "def",
"8service": ghi"
}
From this I have to form a list like below, starting from first application to next application I have to form one list, and so on
[
"1application": "abc",
"2service": "def",
"3service": ghi",
]
[
"4application": "jkl",
"5service": "mno",
]
[
"6application": "abc",
"7service": "def",
"8service": ghi"
]

The playbook below
shell> cat playbook.yml
- hosts: localhost
vars:
my_dict:
{"1application": "abc",
"2service": "def",
"3service": "ghi",
"4application": "jkl",
"5service": "mno",
"6application": "abc",
"7service": "def",
"8service": "ghi"}
tasks:
- set_fact:
my_list: "{{ my_list|default([]) +
[dict(my_range_keys|
zip(my_range_keys|
map('extract', my_dict)))] }}"
loop: "{{ my_idxs }}"
loop_control:
extended: true
vars:
my_keys: "{{ my_dict.keys()|list|sort }}"
my_len: "{{ my_keys|length }}"
my_apps: "{{ my_keys|select('match', '^(\\d+)application(.*)$')|list }}"
my_idxs: "{{ my_apps|map('regex_replace', '^(\\d+)(.*)$', '\\1')|list }}"
my_last: "{{ ansible_loop.nextitem|default(my_len|int + 1) }}"
my_range: "{{ range(item|int - 1, my_last|int - 1)|list }}"
my_range_keys: "{{ my_range|map('extract', my_keys)|list }}"
- debug:
var: my_list
gives
my_list:
- 1application: abc
2service: def
3service: ghi
- 4application: jkl
5service: mno
- 6application: abc
7service: def
8service: ghi
The previous solution works only with a single-digit prefix. With multiple digits, the sort function doesn't work anymore. In this case, we have to create the sorted list of the keys. For example, the playbook
shell> cat playbook.yml
- hosts: localhost
vars:
my_dict:
{"1application": "abc",
"2service": "def",
"3service": "ghi",
"4application": "jkl",
"5service": "mno",
"6application": "abc",
"7service": "def",
"8service": "ghi",
"9application": "prs",
"10service": "stu",
"11service": "vxy"}
tasks:
- set_fact:
my_keys_idx: "{{ my_keys_idx|default([]) +
[{'idx': my_idx|int, 'key': item}] }}"
loop: "{{ my_dict.keys()|list }}"
vars:
my_idx: "{{ item|regex_replace('^(\\d+)(.*)$', '\\1') }}"
- debug:
var: my_keys_idx
- set_fact:
my_keys: "{{ my_keys_idx|
sort(attribute='idx')|
map(attribute='key')|
list }}"
- debug:
var: my_keys
gives
my_keys:
- 1application
- 2service
- 3service
- 4application
- 5service
- 6application
- 7service
- 8service
- 9application
- 10service
- 11service
Then the task below works as expected
- set_fact:
my_list: "{{ my_list|default([]) +
[dict(my_range_keys|
zip(my_range_keys|
map('extract', my_dict)))] }}"
loop: "{{ my_idxs }}"
loop_control:
extended: true
vars:
# my_keys: "{{ my_dict.keys()|list|sort }}"
my_len: "{{ my_keys|length }}"
my_apps: "{{ my_keys|select('match', '^(\\d+)application(.*)$')|list }}"
my_idxs: "{{ my_apps|map('regex_replace', '^(\\d+)(.*)$', '\\1')|list }}"
my_last: "{{ ansible_loop.nextitem|default(my_len|int + 1) }}"
my_range: "{{ range(item|int - 1, my_last|int - 1)|list }}"
my_range_keys: "{{ my_range|map('extract', my_keys)|list }}"
- debug:
var: my_list
gives
my_list:
- 1application: abc
2service: def
3service: ghi
- 4application: jkl
5service: mno
- 6application: abc
7service: def
8service: ghi
- 10service: stu
11service: vxy
9application: prs

Related

JMESPathError in json_query filter: Unknown function: regex_search()

Here is my playbook:
- hosts: localhost
vars:
{
"result": [
{
"_ref": "vlan/ZG5zLnZsYW4kLmNvbS5pbmZvYmxveC5kbnMudmxhbl92aWV3JElORlJBTEFCLjEuNDA5NC4xMQ:LAB1/test1/11",
"id": 11,
"name": "test1",
"parent": {
"_ref": "vlanview/ZG5zLnZsYW5fdmlldyRJTkZSQUxBQi4xLjQwOTQ:LAB1/1/4094"
}
},
{
"_ref": "vlan/ZG5zLnZsYW4kLmNvbS5pbmZvYmxveC5kbnMudmxhbl92aWV3JFNDTEFCLU9PQi4xLjQwOTQuMTE:LAB2/test1/11",
"id": 11,
"name": "test1,
"parent": {
"_ref": "vlanview/ZG5zLnZsYW5fdmlldyRTQ0xBQi1PT0IuMS40MDk0:LAB2/1/4094"
}
}
]
}
tasks:
- set_fact:
var1: "{{result|json_query(jquery)}}"
vars:
jquery: "[].{vlan_view: _ref|regex_search('(?<=:)[^/]*'), vlan_id: id, vlan_name: name}"
- debug: msg={{var1}}
Which errors with:
fatal: [localhost]: FAILED! => {"msg": "JMESPathError in json_query filter plugin:\nUnknown function: regex_search()"}
My desired output
[
{
"vlan_view": LAB1,
"vlan_id": 11,
"vlan_name": "test1"
},
{
"vlan_id": 11,
"vlan_name": "test1",
"vlan_view": "LAB2"
}
]
You cannot do regex operation in JMESPath, as per this issue on their tracker.
And you surely cannot use a Jinja filter as a JMESPath function, as the error is pointing out.
So, you will have to achieve this with Jinja filters and Ansible alone.
And with a loop, it is definitely possible to create a list corresponding to your desired output:
- set_fact:
var1: "{{ var1 | default([]) + [_vlan] }}"
loop: "{{ result }}"
loop_control:
label: "{{ item.id }}"
vars:
_vlan:
vlan_id: "{{ item.name }}"
vlan_name: "{{ item.id }}"
vlan_view: >-
{{
item.parent._ref
| regex_search(':(.*?)\/', '\1')
| first
}}
Given the two tasks:
- set_fact:
var1: "{{ var1 | default([]) + [_vlan] }}"
loop: "{{ result }}"
loop_control:
label: "{{ item.id }}"
vars:
_vlan:
vlan_id: "{{ item.name }}"
vlan_name: "{{ item.id }}"
vlan_view: >-
{{
item.parent._ref
| regex_search(':(.*?)\/', '\1')
| first
}}
- debug:
var: var1
This will yield:
TASK [set_fact] ***************************************************************
ok: [localhost] => (item=11)
ok: [localhost] => (item=11)
TASK [debug] ******************************************************************
ok: [localhost] =>
var1:
- vlan_id: test1
vlan_name: '11'
vlan_view: LAB1
- vlan_id: test1
vlan_name: '11'
vlan_view: LAB2
Get the attributes vlan_view
vlan_view: "{{ result|map(attribute='_ref')|
map('split', ':')|map('last')|
map('split', '/')|map('first')|
map('community.general.dict_kv', 'vlan_view')|
list }}"
gives
vlan_view:
- vlan_view: LAB1
- vlan_view: LAB2
Then use json_query to get the other attributes and combine the dictionaries
var1: "{{ result|json_query('[].{vlan_id: id, vlan_name: name}')|
zip(vlan_view)|map('combine')|list }}"
gives the expected result
var1:
- vlan_id: 11
vlan_name: test1
vlan_view: LAB1
- vlan_id: 11
vlan_name: test1
vlan_view: LAB2
Example of a complete playbook (simplified for testing)
- hosts: localhost
vars:
result:
- _ref: vlan/ZG5z...4xMQ:LAB1/test1/11
id: 11
name: test1
parent:
_ref: vlanview/ZG5zL...wOTQ:LAB1/1/4094
- _ref: vlan/ZG5zL...uMTE:LAB2/test1/11
id: 11
name: test1
parent:
_ref: vlanview/ZG5zL...MDk0:LAB2/1/4094
vlan_view: "{{ result|map(attribute='_ref')|
map('split', ':')|map('last')|
map('split', '/')|map('first')|
map('community.general.dict_kv', 'vlan_view')|
list }}"
var1: "{{ result|json_query('[].{vlan_id: id, vlan_name: name}')|
zip(vlan_view)|map('combine')|list }}"
tasks:
- debug:
var: vlan_view
- debug:
var: var1

Create dictionary keys from variable and add list of IPs as values

I would like to create a dictionary and populated from variables both the keys and values. The values will be a list of IPs.
If the key is already present in the dictionary, then, I need to add the IP to the existing list.
Here is the input data, which is a list of list.
parse1.json:
[
[
"/RAD33G4099/mobile_app/pl_crad.la-dc.vivo.com_80",
"properties",
"members",
"/Common/10.50.100.25:80",
"session"
],
[
"/RAD33G4099/mobile_app/pl_crad.la-dc.vivo.com_80",
"properties",
"members",
"/Common/10.50.100.39:80",
"session"
],
[
"/RAD33G4099/mobile_app/pl_crad.la-dc.vivo.com_80",
"properties",
"members",
"/Common/10.50.100.49:80",
"session"
],
[
"/RAD33G4099/mobile_app/pl_crad.la-dc.vivo.com_443",
"properties",
"members",
"/Common/10.50.100.18:80",
"session"
],
[
"/RAD33G4099/mobile_app/pl_crad.la-dc.vivo.com_443",
"properties",
"members",
"/Common/10.50.100.28:80",
"session"
],
[
"/RAD33G4099/mobile_app/pl_crad.la-dc.vivo.com_443",
"properties",
"members",
"/Common/10.50.100.48:80",
"session"
]
]
Here is my trial at solving it:
- set_fact:
my_dic: {}
disabled_host: "{{ lookup('file', 'parse1.json') }}"
- name: add new key / value pairs to dict
set_fact:
my_dict_var: "{{ my_dict_var + [ my_dic | combine ( { name + '_' + app_name : host } , recursive=True) ] }}"
loop: "{{ disabled_host }}"
vars:
name: "{{ item[0] | regex_replace('^\/.*\/(.*)$', '\\1') }}"
app_name: "{{ item[0] | regex_replace('^\/.*\/(.*)\/.*$', '\\1') }}"
host: "{{ item[3] | regex_replace('^\/.*\/(.*):.*$', '\\1') }}"
The actual output is
ok: [localhost] => {
"msg": [
{
"pl_crad.la-dc.vivo.com_80_mobile_app": "10.50.100.25"
},
{
"pl_crad.la-dc.vivo.com_80_mobile_app": "10.50.100.39"
},
{
"pl_crad.la-dc.vivo.com_80_mobile_app": "10.50.100.49"
},
{
"pl_crad.la-dc.vivo.com_443_mobile_app": "10.50.100.18"
},
{
"pl_crad.la-dc.vivo.com_443_mobile_app": "10.50.100.28"
},
{
"pl_crad.la-dc.vivo.com_443_mobile_app": "10.50.100.48"
}
]
}
But, I would like to create a list of IPs if the key is same.
My desired output is
{
"pl_crad.la-dc.vivo.com_80_mobile_app": [
"10.50.100.25",
"10.50.100.39",
"10.50.100.49"
],
"pl_crad.la-dc.vivo.com_443_mobile_app": [
"10.50.100.18",
"10.50.100.28",
"10.50.100.48"
]
}
You don't have to go the hard way and use regex, you can just split those strings, and / or, use basename and dirname.
Using dirname and basename:
- set_fact:
disabled_hosts_ips: >-
{{
disabled_hosts_ips | default({})
| combine({
name: (disabled_hosts_ips | default({}))[name] | default([])
+ [ip]
})
}}
loop: "{{ disabled_hosts }}"
vars:
name: "{{ item[0] | basename ~ '_' ~ item[0] | dirname | basename }}"
ip: "{{ (item[3] | basename).split(':')[0] }}"
Using split:
- set_fact:
disabled_hosts_ips: >-
{{
disabled_hosts_ips | default({})
| combine({
name: (disabled_hosts_ips | default({}))[name] | default([])
+ [ip]
})
}}
loop: "{{ disabled_hosts }}"
vars:
name: "{{ item[0].split('/')[-2:] | reverse | join('_') }}"
ip: "{{ item[3].split('/')[-1].split(':')[0] }}"
Those both give:
disabled_hosts_ips:
pl_crad.la-dc.vivo.com_443_mobile_app:
- 10.50.100.18
- 10.50.100.28
- 10.50.100.48
pl_crad.la-dc.vivo.com_80_mobile_app:
- 10.50.100.25
- 10.50.100.39
- 10.50.100.49
For example, using the variable my_dict_var
my_dict_groups: "{{ dict(my_dict_keys|zip(my_dict_vals)) }}"
my_dict_keys: "{{ dh_groups|map('first')|list }}"
my_dict_vals: "{{ dh_groups|map('last')|map('map', attribute='value')|list }}"
dh_groups: "{{ my_dict_var|map('dict2items')|flatten|groupby('key') }}"
gives
my_dict_groups:
pl_crad.la-dc.vivo.com_443_mobile_app:
- 10.50.100.18
- 10.50.100.28
- 10.50.100.48
pl_crad.la-dc.vivo.com_80_mobile_app:
- 10.50.100.25
- 10.50.100.39
- 10.50.100.49
The complete set of the declarations is below
my_dict_groups: "{{ dict(my_dict_keys|zip(my_dict_vals)) }}"
my_dict_keys: "{{ dh_groups|map('first')|list }}"
my_dict_vals: "{{ dh_groups|map('last')|map('map', attribute='value')|list }}"
dh_groups: "{{ dh_keys|zip(dh_vals)|map('combine')|groupby('key') }}"
dh_vals: "{{ disabled_host|
map(attribute=3)|
map('split', '/')|map('last')|
map('split', ':')|map('first')|
map('community.general.dict_kv', 'value')|
list }}"
dh_keys: "{{ disabled_host|
map(attribute=0)|
map('regex_replace', dh_regex, dh_replace)|
map('community.general.dict_kv', 'key')|
list }}"
dh_regex: '^/.*?/(.*?)/(.*)$'
dh_replace: '\2_\1'
disabled_host: "{{ lookup('file', 'parse1.json') }}"
Details
In both cases use the filter groupby and create the list
dh_groups:
- - pl_crad.la-dc.vivo.com_443_mobile_app
- - key: pl_crad.la-dc.vivo.com_443_mobile_app
value: 10.50.100.18
- key: pl_crad.la-dc.vivo.com_443_mobile_app
value: 10.50.100.28
- key: pl_crad.la-dc.vivo.com_443_mobile_app
value: 10.50.100.48
- - pl_crad.la-dc.vivo.com_80_mobile_app
- - key: pl_crad.la-dc.vivo.com_80_mobile_app
value: 10.50.100.25
- key: pl_crad.la-dc.vivo.com_80_mobile_app
value: 10.50.100.39
- key: pl_crad.la-dc.vivo.com_80_mobile_app
value: 10.50.100.49
Create the lists of the keys my_dict_keys and values my_dict_vals, and create the dictionary my_dict_groups
my_dict_keys:
- pl_crad.la-dc.vivo.com_443_mobile_app
- pl_crad.la-dc.vivo.com_80_mobile_app
my_dict_vals:
- - 10.50.100.18
- 10.50.100.28
- 10.50.100.48
- - 10.50.100.25
- 10.50.100.39
- 10.50.100.49
For example, put the declarations into the playbook vars
- hosts: localhost
vars:
my_dict_groups: "{{ dict(my_dict_keys|zip(my_dict_vals)) }}"
my_dict_keys: "{{ dh_groups|map('first')|list }}"
my_dict_vals: "{{ dh_groups|map('last')|map('map', attribute='value')|list }}"
# dh_groups: "{{ my_dict_var|map('dict2items')|flatten|groupby('key') }}"
dh_groups: "{{ dh_keys|zip(dh_vals)|map('combine')|groupby('key') }}"
dh_vals: "{{ disabled_host|
map(attribute=3)|
map('split', '/')|
map('last')|map('split', ':')|map('first')|
map('community.general.dict_kv', 'value')|
list }}"
dh_keys: "{{ disabled_host|
map(attribute=0)|
map('regex_replace', dh_regex, dh_replace)|
map('community.general.dict_kv', 'key')|
list }}"
dh_regex: '^/.*?/(.*?)/(.*)$'
dh_replace: '\2_\1'
disabled_host: "{{ lookup('file', 'parse1.json') }}"
# my_dict_var:
# - pl_crad.la-dc.vivo.com_80_mobile_app: 10.50.100.25
# - pl_crad.la-dc.vivo.com_80_mobile_app: 10.50.100.39
# - pl_crad.la-dc.vivo.com_80_mobile_app: 10.50.100.49
# - pl_crad.la-dc.vivo.com_443_mobile_app: 10.50.100.18
# - pl_crad.la-dc.vivo.com_443_mobile_app: 10.50.100.28
# - pl_crad.la-dc.vivo.com_443_mobile_app: 10.50.100.48
tasks:
- debug:
var: my_dict_groups

How to parse text with ansible using a nested list/dict?

I have a text file that needs to be parsed so i can use it via a REST API with ansible.
10.0.0.0/16 Building-A
10.1.0.0/16 Building-A
10.2.0.0/16 Building-B
10.3.0.0/16 Building-B
I need to convert this text to something like this:
{
"parsed":[
{
"Building-A":[
"10.0.0.0/16",
"10.1.0.0/16"
]
},
{
"Building-B":[
"10.2.0.0/16",
"10.3.0.0/16"
]
}
]
}
Currently i run the following playbook to test the textparsing, without success. The list created is not unique.
- name: Test
hosts: localhost
tasks:
- name: Combine
ansible.builtin.set_fact:
parsed: "{{ (parsed | default([])) | union( [{item.split()[1]: item.split()[0] }] ) }}"
loop: "{{ lookup('file','hostgroups.txt').strip().splitlines() }}"
- name: Debug
ansible.builtin.debug:
var: parsed
ok: [localhost] => {
"parsed": [
{
"Building-A": "10.0.0.0/16"
},
{
"Building-A": "10.1.0.0/16"
},
{
"Building-B": "10.2.0.0/16"
},
{
"Building-B": "10.3.0.0/16"
}
]
}
Thanks for the tip with the helper variables and the groupby filter. Here is the final playbook:
- name: Test
hosts: localhost
# strategy: free
tasks:
- name: Get List
ansible.builtin.set_fact:
parsed_list: "{{ parsed_list | default([]) + [_item] }}"
loop: "{{ lookup('file','hostgroups.txt').strip().splitlines() }}"
vars:
_list: "{{ item.split() }}"
_item: "{{ {'Name': _list[1], 'Subnet': _list[0] } }}"
- name: Debug parsed_list
ansible.builtin.debug:
var: parsed_list
- name: Group parsed_list
ansible.builtin.set_fact.set_fact:
parsed_group: "{{ parsed_group | default([]) + [{_key: _value}] }}"
loop: "{{ parsed_list | groupby('Name') }}"
vars:
_key: "{{ item.0 }}"
_value: "{{ item.1 | map(attribute='Subnet') | list }}"
- name: Debug parsed_group
ansible.builtin.debug:
var: parsed_group
Parse the content, e.g.
- set_fact:
parsed_list: "{{ parsed_list|d([]) + [_item] }}"
loop: "{{ lookup('file','hostgroups.txt').splitlines() }}"
vars:
_array: "{{ item.split() }}"
_item: "{{ {'building': _array.1, 'ip': _array.0} }}"
gives
parsed_list:
- building: Building-A
ip: 10.0.0.0/16
- building: Building-A
ip: 10.1.0.0/16
- building: Building-B
ip: 10.2.0.0/16
- building: Building-B
ip: 10.3.0.0/16
Then, use filter groupby and create the list
- set_fact:
parsed: "{{ parsed|d([]) + [{_key: _val}] }}"
loop: "{{ parsed_list|groupby('building') }}"
vars:
_key: "{{ item.0 }}"
_val: "{{ item.1|map(attribute='ip')|list }}"
gives
parsed:
- Building-A:
- 10.0.0.0/16
- 10.1.0.0/16
- Building-B:
- 10.2.0.0/16
- 10.3.0.0/16
In some cases, a dictionary might be a better structure, e.g.
- set_fact:
parsed_dict: "{{ parsed_dict|d({})|combine({_key: _val}) }}"
loop: "{{ parsed_list|groupby('building') }}"
vars:
_key: "{{ item.0 }}"
_val: "{{ item.1|map(attribute='ip')|list }}"
gives
parsed_dict:
Building-A:
- 10.0.0.0/16
- 10.1.0.0/16
Building-B:
- 10.2.0.0/16
- 10.3.0.0/16

Invert a dictionary with list values in ansible

How do I convert a dictionary like below
{
"host1": ["tag1", "tag2"],
"host2": ["tag1"]
}
to below in ansible:
{
"tag1": ["host1","host2"],
"tag2": ["host1"]
}
I have been trying to do this but got stuck due to the value being a list.
The playbook
- hosts: localhost
vars:
dict1:
host1: [tag1, tag2]
host2: [tag1]
tasks:
- set_fact:
dict2: "{{ dict2|default({})|combine({item: list_of_hosts}) }}"
loop: "{{ dict1.values()|flatten|unique }}"
vars:
list_of_hosts: "{{ dict1|
dict2items|
selectattr('value', 'contains', item)|
map(attribute='key')|
list }}"
- debug:
var: dict2
gives
dict2:
tag1: [host1, host2]
tag2: [host1]
Optionally, use json_query
vars:
list_of_hosts: "{{ dict1|dict2items|json_query(_query) }}"
_query: '[?value.contains(#, `{{ item }}`)].key'

Ansible: Remove item from dict

I have a situation where i am trying to remove the item from a list. but i am not getting expected result. Please help me what I am doing wrong here?
here is the list :
"get_ec2_id.instances[0].tags": {
"Name": "test-db-system-2",
"aws:cloudformation:logical-id": "DBInstance",
"aws:cloudformation:stack-id": "arn:aws:cloudformation:us-east-1:123456789012:stack/test-db-system-2/0115v0a0-5d44-17e8-a024-503ama4a5qd1",
"aws:cloudformation:stack-name": "test-db-system-2",
"dbsystem:stack": "test-db-system-2",
"dbsystem:type": "db"
}
}
I am trying to remove the all "aws:cloudformation" tags from a list using below filter:
"{{ get_ec2_id.instances[0].tags | reject('search','aws:') | list }}"
I am getting the below result:
ok: [10.52.8.101] => {
"instances_tags": [
"dbsystem:type",
"dbsystem:stack",
"Name"
]
}
but I am expected below result :
"instances_tags": [
"dbsystem:stack": "test-db-system-2",
"dbsystem:type": "db"
"Name" : "test-db-system-2",
]
}
Help me to solve the issue.
More generic solution where input is a dict and blacklist is a list:
---
- set_fact:
blacklist:
- bad1
- bad2
- set_fact:
output: {}
- name: remove blacklisted items from input
set_fact:
output: "{{ output | combine({item.key: item.value}) }}"
when: item.key not in blacklist
loop: "{{ input | dict2items }}"
Given the data
get_ec2_id:
instances:
- tags:
Name: test-db-system-2
aws:cloudformation:logical-id: DBInstance
aws:cloudformation:stack-id: arn:aws:cloudformation:us-east-1:123456789012:stack/test-db-system-2/0115v0a0-5d44-17e8-a024-503ama4a5qd1
aws:cloudformation:stack-name: test-db-system-2
dbsystem:stack: test-db-system-2
dbsystem:type: db
Use rejectattr. For example
dict2: "{{ get_ec2_id.instances.0.tags|
dict2items|
rejectattr('key', 'search', 'aws:')|
items2dict }}"
gives
dict2:
Name: test-db-system-2
dbsystem:stack: test-db-system-2
dbsystem:type: db
Then, convert the dictionary into a list of dictionaries
instances_tags: "{{ dict2|
dict2items|
json_query('[].[[key, value]]')|
map('community.general.dict')|
list }}"
gives
instances_tags:
- Name: test-db-system-2
- dbsystem:stack: test-db-system-2
- dbsystem:type: db
Use this:
---
- name: dictionary
hosts: localhost
gather_facts: False
connection: local
vars:
get_ec2_id:
instances:
tags:
Name: "test-db-system-2"
"aws:cloudformation:logical-id": "DBInstance"
"aws:cloudformation:stack-id": "arn:aws:cloudformation:us-east-1:123456789012:stack/test-db-system-2/0115v0a0-5d44-17e8-a024-503ama4a5qd1"
"aws:cloudformation:stack-name": "test-db-system-2"
"dbsystem:stack": "test-db-system-2"
"dbsystem:type": "db"
dict2: {}
tasks:
- name: Fact1
set_fact:
dict: "{{ get_ec2_id.instances.tags }}"
- name: Debug1
debug:
var: dict
- name: Fact2
set_fact:
dict2: "{{ dict2 | combine({item.key: item.value}) }}"
when: "{{ item.key.find('aws:') }}"
with_dict: "{{ dict }}"
- name: Debug2
debug:
var: dict2
Output:
TASK [Debug2] ******************************************************************
ok: [localhost] => {
"dict2": {
"Name": "test-db-system-2",
"dbsystem:stack": "test-db-system-2",
"dbsystem:type": "db"
}
}

Resources