Karl
Karl

Reputation: 3113

How can i insert list as string with ansible in line in file

I have list in ansible as

mylist = ["test1", "test2", "test3"]

This is ansible variable.

Now in my some file on remote host i want something like

lineinfile: dest=~/.config line='myvar = ["test1", "test2", "test3"]'

How can i do that using mylist

Upvotes: 1

Views: 4003

Answers (2)

thankyour
thankyour

Reputation: 207

If you want to show the list's pythonic representation as a string, you'll need to write the string to represent the list representation (brackets), and join the list's values using a delimiter (' ,' in the example):

lineinfile: dest=~/.config line="myvar = [ {{' ,'.join(myvar) }} ]"

Upvotes: 3

stacksonstacks
stacksonstacks

Reputation: 9313

Ordinary files don't know about types etc., you want an ordinary string.

mylist = '["test1", "test2", "test3"]'

lineinfile: dest=~/.config line="myvar = {{ mylist }}"

Upvotes: 0

Related Questions