4

I would like to create a playbook that reads a local file to a var, Then be able to loop through this var line by line and use the lines in a task.

To get the file content i used:

file_contents: "{{lookup('file', './myfile.txt')}}" 

I tried using:

  • with_file
  • with_item
  • with_lines

But I did not get the result i wanted.

any help would be appreciated.

Komar Patel
  • 63
  • 1
  • 1
  • 3

1 Answers1

7

You can use Python built-ins for some types, like strings, for example.

So this will do the trick for you:

file_contents_lines: "{{ lookup('file', './aaa.txt').splitlines() }}"

and

with_items: "{{ file_contents_lines }}"
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193