Convert a text file with list of strings into yml help

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.

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.

Thank you Kai. That helped.