# escape square bracket in ansible

**URL:** <https://forum.ansible.com/t/escape-square-bracket-in-ansible/34698>\
**Category:** Ansible Project\
**Created:** [March 1, 2021, 7:52am UTC](https://forum.ansible.com/t/escape-square-bracket-in-ansible/34698 "2021-03-01T07:52:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sakshi\_Rathore](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@Sakshi\_Rathore](https://forum.ansible.com/u/Sakshi_Rathore)\
**Post date:** [March 1, 2021, 7:52am UTC](https://forum.ansible.com/t/escape-square-bracket-in-ansible/34698/1 "2021-03-01T07:52:38Z")

</div>

Hello everyone,

Can anyone please advise how to escape square brackets from output in ansible?

i need to insert some ip addresses from a file into dynamic inventory file. while doing this i am getting square bracket also .

how can i remove or escape square bracket into inventory file?

sample input file is:

[“ipadd1” , “ipadd2” , “ipadd3” , “ipadd4”]  
[“ipadd5” , “ipadd6” , “ipadd7” , “ipadd8”]

please advise.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [March 1, 2021, 8:49am UTC](https://forum.ansible.com/t/escape-square-bracket-in-ansible/34698/2 "2021-03-01T08:49:31Z")

</div>

Given the file ip\_list.txt is on the controller, try

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ip\_list: "{{ lookup('file', 'ip\_list.txt').split('\n')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('from\_yaml')|list }}"

should give

&nbsp;&nbsp;ip\_list:  
&nbsp;&nbsp;&nbsp;&nbsp;- [ipadd1, ipadd2, ipadd3, ipadd4]  
&nbsp;&nbsp;&nbsp;&nbsp;- [ipadd5, ipadd6, ipadd7, ipadd8]

Select the items, e.g. the third item from the second list

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: ip\_list[1][2]

should give

&nbsp;&nbsp;ip\_list[1][2]: ipadd7

---

<div class="post-metadata">

**Author:** ![Sakshi\_Rathore](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@Sakshi\_Rathore](https://forum.ansible.com/u/Sakshi_Rathore)\
**Post date:** [March 1, 2021, 9:03am UTC](https://forum.ansible.com/t/escape-square-bracket-in-ansible/34698/3 "2021-03-01T09:03:02Z")

</div>

thanks Vladimir, thanks for the solution , can you also please advise instead of specific ip when i want all ip addresses without bracket constraint from ip\_ist.txt what approach i should follow?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [March 1, 2021, 9:29am UTC](https://forum.ansible.com/t/escape-square-bracket-in-ansible/34698/4 "2021-03-01T09:29:19Z")

</div>

It's not clear what type you're looking for. Should it be a list?

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: ip\_list|flatten

gives

&nbsp;&nbsp;ip\_list|flatten:  
&nbsp;&nbsp;- ipadd1  
&nbsp;&nbsp;- ipadd2  
&nbsp;&nbsp;- ipadd3  
&nbsp;&nbsp;- ipadd4  
&nbsp;&nbsp;- ipadd5  
&nbsp;&nbsp;- ipadd6  
&nbsp;&nbsp;- ipadd7  
&nbsp;&nbsp;- ipadd8

Or, should it be a string perhaps?

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: ip\_list|flatten|join(',')

gives

&nbsp;&nbsp;ip\_list|flatten|join(','):  
&nbsp;&nbsp;ipadd1,ipadd2,ipadd3,ipadd4,ipadd5,ipadd6,ipadd7,ipadd8

Post expected result if this not what you want.

---

<div class="post-metadata">

**Author:** ![Sakshi\_Rathore](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@Sakshi\_Rathore](https://forum.ansible.com/u/Sakshi_Rathore)\
**Post date:** [March 1, 2021, 9:42am UTC](https://forum.ansible.com/t/escape-square-bracket-in-ansible/34698/5 "2021-03-01T09:42:27Z")

</div>

yes exactly i want the ip addresses either in this form:

ipadd1,ipadd2,ipadd3,ipadd4,ipadd5,ipadd6,ipadd7,ipadd8

or

ipadd1,  
ipadd2,  
ipadd3,  
ipadd4,  
ipadd5,  
ipadd6,  
ipadd7  
ipadd8

but currently i am getting output in below form in inventory-ccp.ini :

[ccpclusternodes]  
[“ipadd1”, “ipadd2”, “ipadd3”, “ipadd4”]  
[‘ipadd5’, ‘ipadd6’, ‘ipadd7’, ‘ipadd8’]

in main.yaml:

name: create ccp inventory  
vars:  
ccp\_nodes: “{{ lookup(‘file’, ‘…/ip\_address.txt’ , wantlist=True ) }}”  
template:  
src: inventory-ccp.ini.j2  
dest: inventory-ccp.ini

ip\_address.txt:

[“ipadd1”, “ipadd2”, “ipadd3”, “ipadd4”]  
[‘ipadd5’, ‘ipadd6’, ‘ipadd7’, ‘ipadd8’]

The motive is, these ip are getting fetched dynamically from a url and need to deploy certificates in all the ip addresses using ansible.
