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?
Nested loops in many ways are similar in nature to a set of arrays that would be iterated over using the with_nested operator. Nested loops provide us with a succinct way of iterating over multiple lists within a single task. Now we can create nested loops using with_nested.
Looping over Filetrees. with_filetree recursively matches all files in a directory tree, enabling you to template a complete tree of files on a target system while retaining permissions and ownership.
Ansible loop is used to repeat any task or a part of code multiple times in an Ansible-playbook. It includes the creation of multiple users using the user module, installing multiple packages using apt or yum module or changing permissions on several files or folders using the file module.
The loop keyword is usually used to create simple and standard loops that iterate through several items. The with_* keyword is used with a number of lookup plugins when iterating through values. Lookup plugins enable Ansible to access information from external sources such as external data stores, filesystems etc.
I managed to find an easy alternative:
- debug: msg="{{item}}" with_lines: cat files/branches.txt
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With