escape square bracket in ansible

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.

Given the file ip_list.txt is on the controller, try

    - set_fact:
        ip_list: "{{ lookup('file', 'ip_list.txt').split('\n')|
                     map('from_yaml')|list }}"

should give

  ip_list:
    - [ipadd1, ipadd2, ipadd3, ipadd4]
    - [ipadd5, ipadd6, ipadd7, ipadd8]

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

    - debug:
        var: ip_list[1][2]

should give

  ip_list[1][2]: ipadd7

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?

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

    - debug:
        var: ip_list|flatten

gives

  ip_list|flatten:
  - ipadd1
  - ipadd2
  - ipadd3
  - ipadd4
  - ipadd5
  - ipadd6
  - ipadd7
  - ipadd8

Or, should it be a string perhaps?

    - debug:
        var: ip_list|flatten|join(',')

gives

  ip_list|flatten|join(','):
  ipadd1,ipadd2,ipadd3,ipadd4,ipadd5,ipadd6,ipadd7,ipadd8

Post expected result if this not what you want.

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.