D
D
DenZel2020-02-24 21:01:32
linux
DenZel, 2020-02-24 21:01:32

How to run a loop inside a loop using Ansible?

Hello!
There was a need to create several files of the same type, for example test1 test2 test3
To create one file, I use the following:
- name: Create file
become: yes
shell: "{{ item }}"
with_items:
- echo -e '123' >> /tmp/{{ item }}
-echo -e '456' >> /tmp/{{ item }}
-echo -e '123' >> /tmp/{{ item }}
This will create one test file with 123456123 in it.
Task use the with_sequence module to create n-files test1,test2,....testn
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
chupasaurus, 2020-02-24
@gen4_gdm

- name: create files
  become: yes
  shell: echo '{{ item }}' > /tmp/test{{ item }}
  with_sequence: start=1 count=n

If you want to use a loop index, then you need to use loop. If you are using query/lookup, the loop_control.index_var parameter is available ; if you generate data using Jinja, then it is logical to display data in lists with an index.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question