How to loop variable with index

Hi,

If I have this config, no problem I can see the output however I have a long list inside the variable and i don’t want to manually add new lines and modify the index from “0” to “100” it will be long! How can I loop it?

 - name: Build out CSV file
    ansible.builtin.lineinfile:
      dest: "{{ csv_path }}/{{ csv_filename}}"
      line: "{{ forward_lookup[0] }}, {{ fwd_lookup.results[0].msg }}, {{ forward_lookup_expected[0] }}, {{ reverse_lookup[0] }}, {{ rev_lookup.results[0].msg }}, {{ reverse_lookup_expected[0] }}"
      create: true
      state: present
    delegate_to: localhost

Next time format your ansible in a code block </>

You can use a loop in your task:
Loops — Ansible Documentation

Thank you Al. I checked the documentation and I’m sorry I can’t make it work with lineinfile to loop it. I have some plays using with_items but this one for index I just can’t…

So I am using vars_files and the file look like this.

forward_lookup:
  - test1.domain.com
  - test2.domain.com
  - test3.domain.com

And the task that I am struggling is this. Index 0 I can see it or to any 1,2,3 but I want to loop it. Is this really possible using lineinfile module?

  - name: Build out CSV file
    ansible.builtin.lineinfile:
      dest: "{{ csv_path }}/{{ csv_filename}}"
      line: "{{ forward_lookup [0] }}"
      create: true
      state: present
    delegate_to: localhost

I can loop to it using debug but I just can’t figure it out in lineinfile

vars_files:
    - ../../vars/dns/file 

  tasks:
  - name: debug list1
    ansible.builtin.debug:
      var: list1

  - name: loop through standard list
    ansible.builtin.debug:
      msg: "{{ item }}"
    loop: "{{ list1 }}"

sorry it’s actually doing loop … got confused!

using the vars_file

forward_lookup:
  - cnn.com
  - facebook.com
  - youtube.com

  - name: Build out CSV file
    ansible.builtin.lineinfile:
      dest: "{{ csv_path }}/{{ csv_filename}}"
      line: "{{ forward_lookup }}"
      create: true
      state: present
    delegate_to: localhost

Do you have an idea how it can be move to the next line? seems \n doesn’t work. I want those three domains or more to be in the same columnn FWD_lookup. Thanks

Your playbook should look something like this:

---
- hosts: localhost
  gather_facts: false
  vars:
    forward_lookup:
      - test1.domain.com
      - test2.domain.com
      - test3.domain.com
  tasks:
    - name: Build CSV file
      ansible.builtin.lineinfile:
        path: output.csv
        line: "{{ item }},second column,third column"
        create: true
        state: present
      loop: "{{ forward_lookup }}"

Note we loop over the forward_lookup.

1 Like

Thank you Al! I put the loop statement in the wrong place that’s why I keep getting error and thought about lineinfile can’t support it. Thank you so much!

Before

- name: Build out CSV file
    ansible.builtin.lineinfile:
      dest: "{{ csv_path }}/{{ csv_filename}}"
      line:  "{{ item }}"
    loop: "{{ forward_lookup }}" >>>>>>>
      create: true
      state: present
    delegate_to: localhost

After

- name: Build out CSV file
    ansible.builtin.lineinfile:
      dest: "{{ csv_path }}/{{ csv_filename}}"
      line:  "{{ item }}"
      create: true
      state: present
    loop: "{{ forward_lookup }}"
    delegate_to: localhost