refactor: copy all file/templates from dir
Ansible can list all the files in a directory. This allows us to make the code more "magical".
For example, send all templates from the templates/alloy
directory to the servers.
For example, for this loop
- name: Copy common alloy configuration files
ansible.builtin.template:
src: "templates/alloy/{{ item }}.j2"
dest: "/etc/alloy/{{ item }}"
mode: '0644'
owner: root
group: root
loop:
- "metrics.alloy"
- "logs.alloy"
register: copy_config_files
We can use the lookup with_fileglob
instead of listing the files in loop, at the risk of forgetting some if we add more.
- https://www.reddit.com/r/ansible/comments/sbtx95/how_do_you_copy_all_template_files_to_destination/
- https://docs.ansible.com/ansible/latest/collections/ansible/builtin/fileglob_lookup.html
To do
List all templates/files that take all files from a directory and use with_fileglob
instead of a list.
Translated with DeepL.com (free version)
Edited by Christophe Chaudier