HI!
I'd like to reformat a list of IPv4 addresses with a Python formatting
string.
'12.34.56.78' should be transformed into
'peername.ip=12.34.56.78%255.255.255.255'.
In Python I'd just use
'peername.ip={0}%255.255.255.255'.format( '12.34.56.78')
How to express that in a Jinja2 filter?
I've tried this which fails with "not all arguments converted during
string formatting":
"{{ aedir_hosts.provider | map('extract', hostvars,
['ansible_default_ipv4', 'address']) |
format('peername.ip={0}%255.255.255.255') | list }}"
Many thanks in advance.
Ciao, Michael.
It looks like you just want to have a string prepended and appended to
it - I would use regex_replace for that.
This playbook works:
- hosts: localhost
connection: local
become: false
gather_facts: false
vars:
ips:
- 1.2.3.4
- 12.32.45.11
- 192.168.1.1
- 8.8.8.8
- 224.224.11.211
tasks:
- set_fact:
filtered: "{{ ips | map('regex_replace', '^(.*)$',
'peername.ip=\\1%255.255.255.255') | list }}"
- debug: var=filtered
TASK [debug] *******************************************************************
ok: [localhost] =>
filtered:
- peername.ip=1.2.3.4%255.255.255.255
- peername.ip=12.32.45.11%255.255.255.255
- peername.ip=192.168.1.1%255.255.255.255
- peername.ip=8.8.8.8%255.255.255.255
- peername.ip=224.224.11.211%255.255.255.255