Skip to content

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.

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