How do I loop over each line inside a file with ansible?

ansible loop multiple tasks
ansible loop: (( range))
ansible loop over files in directory
ansible include_tasks loop
ansible with_items deprecated
ansible with_items files in directory
ansible lookup
ansible register with loop

I am looking for something that would be similar to with_items: but that would get the list of items from a file instead of having to include it in the playbook file.

How can I do this in ansible?

I managed to find an easy alternative:

- debug: msg="{{item}}"
  with_lines: cat files/branches.txt

ansible read local file to var and then loop read line by line, 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', '. This adds the ability to loop over the set of tasks in one shot. Ansible by default sets the loop variable item for each loop, which causes these nested loops to overwrite the value of item from the “outer” loops. As of Ansible 2.1, the loop_control option can be used to specify the name of the variable to be used for the loop:

Lets say you have a file like

item 1
item 2
item 3

And you want to install these items.Simply get the file contents to a variable using register.And use this variable for with_items. Make sure your file has one item per line.

- hosts: your-host
  remote_user: your-remote_user
  - name: get the file contents
    command: cat /path/to/your/file
    register: my_items
  - name: install these items
    pip: name:{{item}}
    with_items: my_items.stdout_lines

Loops, Iterating over nested lists; Retrying a task until a condition is met; Looping over You can define the list in a variables file, or in the 'vars' section of your play, then for each item instead of the entire contents of the multi-line {{ item }} variable. With the release of Ansible 2.5, the recommended way to perform loops is the use the new loop keyword instead of with_X style loops. In many cases, loop syntax is better expressed using filters instead of more complex use of query or lookup .

Latest Ansible recommends loop instead of with_something. It can be used in combination with lookup and splitlines(), as Ikar Pohorský pointed out:

- debug: msg="{{item}}"
  loop: "{{ lookup('file', 'files/branches.txt').splitlines() }}"

files/branches.txt should be relative to the playbook

Loop Control, Note that the types of items you iterate over with 'with_items' do not have You can work around this by wrapping your nested list inside a list: with_file iterates over the content of a list of files, item will be set to the content of each file on the output of that program, loop over the results of that line by line. - name: Ansible loop with index modification example debug: msg: "echo loop index at {{ item.0 + 1}} and value at {{item.1}}" Ansible loop with conditional. You can also use the “when” conditional statement along with the loop structure. Thus you can control the looping based on a variable or system facts.

I am surprised that nobody mentioned the ansible Lookups, I think that is exactly what you want.

It reads contents that you want to use in your playbook but do not want to include inside the playbook from files, pipe, csv, redis etc from your local control machine(not from remote machine, that is important, since in most cases, these contents are alongside your playbook on your local machine), and it works with ansible loops.

- hosts: localhost
  gather_facts: no
    - name: Loop over lines in a file
        var: item
      with_lines: cat "./files/lines"

with_lines here is actually loop with lines lookup, to see how the lines lookup works, see the code here, it just runs any commands you give it(so you can give it any thing like echo, cat etc), then split the output into lines and return them.

There are many powerful lookups, to get the comprehensive list, check out the lookup plugins folder.

[SOLVED] How do I loop over each line inside a file with ansible , I managed to find an easy alternative: - debug: msg="{{item}}" with_lines: cat files/​branches.txt. Loops in Ansible are one-dimensional. There is a trick which used to work in previous versions and will again work in Ansible 2.0: You can have one loop together with an include statement and in that included yml you have the 2nd loop. So something along these lines: main.yml: - include: nested_loop.yml obj={{ item }} with_items: objs nested_loop.yml:

ansible, include changing ownership on several files and/or directories with the file module, creating multiple users with the user module, and repeating a polling step until a certain result is reached. template – Template a file out to a remote server The official documentation on the template module. win_lineinfile – Ensure a particular line is in a file, or replace an existing line using a back-referenced regular expression The official documentation on the win_lineinfile module.

More control over SSH pipelining in Ansible 2, How can you reduce the number of SSH connections required in Ansible? Looking for help with a problem I've been struggling with for a few hours. I want to iterate over a list, run a command, register the output for each command and then iterate with debug over each unique registers {{ someregister }}.stdout. For example, the following code will spit out "msg": "1" and "msg": "2"

Please explain usage of "item" in Ansible, I find Ansible docs *really* frustrating. to iterate over every single row and print the value of the 3 columns? --- "msg": "Iterating over CSV lines = # CSV File sample.csv" Look inside the .py for extra info on how to use it. Now, let us see how to use ansible variables like list or dictionary in the templates files. Using list variables in Ansible templates. In the below task, I am looping over the list1 variable in the template, using the for loop structure. Note that, after each iteration, a new line is also added. So the three list items will be in three lines.