escape square bracket in ansible

741 views
Skip to first unread message

Sakshi Rathore

unread,
Mar 1, 2021, 2:52:38 AM3/1/21
to Ansible Project
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.

Vladimir Botka

unread,
Mar 1, 2021, 3:49:54 AM3/1/21
to Sakshi Rathore, ansible...@googlegroups.com
On Sun, 28 Feb 2021 23:52:38 -0800 (PST)
Sakshi Rathore <sakshira...@gmail.com> wrote:

> 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"]

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

--
Vladimir Botka

Sakshi Rathore

unread,
Mar 1, 2021, 4:03:24 AM3/1/21
to Vladimir Botka, ansible...@googlegroups.com
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?

Vladimir Botka

unread,
Mar 1, 2021, 4:29:33 AM3/1/21
to Sakshi Rathore, ansible...@googlegroups.com
On Mon, 1 Mar 2021 10:03:02 +0100
Sakshi Rathore <sakshira...@gmail.com> wrote:

> instead of specific ip when i want all ip addresses without bracket

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.

--
Vladimir Botka

Sakshi Rathore

unread,
Mar 1, 2021, 4:42:50 AM3/1/21
to Vladimir Botka, ansible...@googlegroups.com
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:
namecreate ccp inventory
  vars:
    ccp_nodes"{{ lookup('file', '../ip_address.txt'  , wantlist=True ) }}"
  template
    srcinventory-ccp.ini.j2
    destinventory-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.
Reply all
Reply to author
Forward
0 new messages