The tasks below
- set_fact:
ep2: "{{ ep2|
default([]) + [
{'write': item.write,
'address': item.address,
'port': item.name.split(':').1,
'name': item.name.split(':').0} ]
}}"
loop: "{{ endpoints }}"
- debug:
var: ep2
give
ep2:
- address: 10.10.10.1
name: hostname1
port: '867'
write: true
- address: 10.10.10.2
name: hostname2
port: '867'
write: true
- address: 10.10.10.3
name: hostname3
port: '867'
write: true
Task below is more flexible and adds items write and address only if defined
- set_fact:
ep: "{{ ep|
default([]) + [ {}|
combine((item.write is defined)|
ternary({'write': item.write}, {}))|
combine((item.address is defined)|
ternary({'address': item.address}, {}))|
combine({'port': item.name.split(':').1})|
combine({'name': item.name.split(':').0}) ]
}}"
loop: "{{ endpoints }}"
If the complete item can be used then the task can be simplified
- set_fact:
ep: "{{ ep|
default([]) + [
item|
combine({'port': item.name.split(':').1})|
combine({'name': item.name.split(':').0}) ]
}}"
loop: "{{ endpoints }}"
If you want to try filter_plugins use dict_utils. Below is the simplified dict_add_hash filter.
$ cat filter_plugins/dict_utils.py
def dict_add_hash(d, h):
for k, v in h.iteritems():
d[k] = v
return d
class FilterModule(object):
''' Ansible filters. Interface to Python dictionary methods.'''
def filters(self):
return {
'dict_add_hash' : dict_add_hash
}
the tasks
- set_fact:
ep2: "{{ ep2|
default([]) + [
item|
dict_add_hash({'port': item.name.split(':').1})|
dict_add_hash({'name': item.name.split(':').0}) ]
}}"
loop: "{{ endpoints }}"
- debug:
var: ep2
give the same result
ep2:
- address: 10.10.10.1
name: hostname1
port: '867'
write: true
- address: 10.10.10.2
name: hostname2
port: '867'
write: true
- address: 10.10.10.3
name: hostname3
port: '867'
write: true
See all available plugins.