I have a list of ports
ports:
- 123
- 456
- 789
that I like to turn into containerPort for usage in a Kubernetes deployment:
"container_ports": [
{
"containerPort": 123
},
{
"containerPort": 456
},
{
"containerPort": 789
}
]
The example above is created when I define ports as
ports:
- containerPort: 123
- containerPort: 456
- containerPort: 789
However, I'd like to save the user some typing and add containerPort automatically. I am able to prepend it using
- name: Creating custom ports
set_fact:
container_ports: "{{ ports | map('regex_replace', '(^)', '- containerPort: \\1') | list }}"
when: ports is defined
This does only create
ok: [localhost] => {
"container_ports": [
"- containerPort: 123",
"- containerPort: 456",
"- containerPort: 789"
]
}
though, which is close, but not quite right.