# Convert a text file with list of strings into yml help

**URL:** <https://forum.ansible.com/t/convert-a-text-file-with-list-of-strings-into-yml-help/29913>\
**Category:** Ansible Project\
**Created:** [March 19, 2019, 7:09pm UTC](https://forum.ansible.com/t/convert-a-text-file-with-list-of-strings-into-yml-help/29913 "2019-03-19T19:09:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saranya\_N](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@Saranya\_N](https://forum.ansible.com/u/Saranya_N)\
**Post date:** [March 19, 2019, 7:09pm UTC](https://forum.ansible.com/t/convert-a-text-file-with-list-of-strings-into-yml-help/29913/1 "2019-03-19T19:09:03Z")

</div>

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:  
&nbsp;&nbsp;- string1  
&nbsp;&nbsp;- 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.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 19, 2019, 8:34pm UTC](https://forum.ansible.com/t/convert-a-text-file-with-list-of-strings-into-yml-help/29913/2 "2019-03-19T20:34:33Z")

</div>

You can create a variable with the list like so

&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;listofstrings: '{{ listofstrings | default() + [item] }}'  
&nbsp;&nbsp;&nbsp;&nbsp;with\_lines: cat test.txt

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

---

<div class="post-metadata">

**Author:** ![Saranya\_N](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@Saranya\_N](https://forum.ansible.com/u/Saranya_N)\
**Post date:** [March 21, 2019, 9:27am UTC](https://forum.ansible.com/t/convert-a-text-file-with-list-of-strings-into-yml-help/29913/3 "2019-03-21T09:27:49Z")

</div>

Thank you Kai. That helped.
