# .format() in Jinja2 filter

**URL:** <https://forum.ansible.com/t/format-in-jinja2-filter/32579>\
**Category:** Ansible Project\
**Created:** [April 1, 2020, 9:31am UTC](https://forum.ansible.com/t/format-in-jinja2-filter/32579 "2020-04-01T09:31:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Stroder](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Michael\_Stroder](https://forum.ansible.com/u/Michael_Stroder)\
**Post date:** [April 1, 2020, 9:31am UTC](https://forum.ansible.com/t/format-in-jinja2-filter/32579/1 "2020-04-01T09:31:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [April 1, 2020, 10:17am UTC](https://forum.ansible.com/t/format-in-jinja2-filter/32579/2 "2020-04-01T10:17:09Z")

</div>

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  
&nbsp;&nbsp;connection: local  
&nbsp;&nbsp;become: false  
&nbsp;&nbsp;gather\_facts: false

&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;ips:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 1.2.3.4  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 12.32.45.11  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 192.168.1.1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 8.8.8.8  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 224.224.11.211

&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filtered: "{{ ips | map('regex\_replace', '^(.\*)$',  
'peername.ip=\\1%255.255.255.255') | list }}"

&nbsp;&nbsp;&nbsp;&nbsp;- debug: var=filtered

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\>  
&nbsp;&nbsp;filtered:  
&nbsp;&nbsp;- peername.ip=1.2.3.4%255.255.255.255  
&nbsp;&nbsp;- peername.ip=12.32.45.11%255.255.255.255  
&nbsp;&nbsp;- peername.ip=192.168.1.1%255.255.255.255  
&nbsp;&nbsp;- peername.ip=8.8.8.8%255.255.255.255  
&nbsp;&nbsp;- peername.ip=224.224.11.211%255.255.255.255

---

<div class="post-metadata">

**Author:** ![Michael\_Stroder](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Michael\_Stroder](https://forum.ansible.com/u/Michael_Stroder)\
**Post date:** [April 1, 2020, 10:30am UTC](https://forum.ansible.com/t/format-in-jinja2-filter/32579/3 "2020-04-01T10:30:05Z")

</div>

Dirk,
