Convert a text file with list of strings into yml help

161 views
Skip to first unread message

Saranya N

unread,
Mar 19, 2019, 3:09:03 PM3/19/19
to Ansible Project
I'm having a text file with lots of strings like below.

Cat test.txt
String1
String2
.
.
.
Etc

I literally have 2k of strings on that file.

I want to convert to yank like below.

_ _ _

Listofstrings:
- string1
- string2

...etc


I can achieve this via shell script but is there an ansible way of doing it using lineinfile or anything else?

I know I can achieve via jinja2​ tempalte as well but I don't know how to loop over the strings in the text file in j2 template.

Kindly assist.

Kai Stian Olstad

unread,
Mar 19, 2019, 4:34:44 PM3/19/19
to ansible...@googlegroups.com
You can create a variable with the list like so

- set_fact:
listofstrings: '{{ listofstrings | default([]) + [item] }}'
with_lines: cat test.txt

and then you can do all sort of thing with the variable listofstrings in Ansible.


--
Kai Stian Olstad

Saranya Nagaraj

unread,
Mar 21, 2019, 5:28:01 AM3/21/19
to ansible...@googlegroups.com
Thank you Kai. That helped. 

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/il1LDL5H4xQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/ae70bfbe-7a63-28e0-53e9-fb7fa7a74470%40olstad.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages