--- # Reusable task to deploy files and templates from role paths # Parameters: # - subdir: subdirectory under role// (e.g., 'home', 'root') (optional, default: '') # - target_root: target root path (e.g., ansible_facts.env.HOME, '/etc') # - use_symlinks: bool, whether to use symlinks for local deployments (default: true) # - become_root: bool, whether to become root (default: false) - name: "Build file lists for {{ var_prefix }}" ansible.builtin.set_fact: "{{ var_prefix }}_files": "{{ lookup('community.general.filetree', role_path ~ '/' ~ (subdir | default('')), wantlist=True) | selectattr('state', 'equalto', 'file') | rejectattr('path', 'match', '.*\\.j2$') | list }}" "{{ var_prefix }}_templates": "{{ lookup('community.general.filetree', role_path ~ '/' ~ (subdir | default('')), wantlist=True) | selectattr('state', 'equalto', 'file') | selectattr('path', 'match', '.*\\.j2$') | list }}" - name: "Ensure directories exist for {{ var_prefix }}" ansible.builtin.file: path: "{{ target_root }}/{{ item.path | dirname }}" state: directory mode: "0755" loop: "{{ lookup('vars', var_prefix ~ '_files') + lookup('vars', var_prefix ~ '_templates') }}" when: item.path | dirname != '' become: "{{ become_root | default(false) }}" - name: "Deploy files (local with symlinks) for {{ var_prefix }}" ansible.builtin.file: src: "{{ item.src }}" dest: "{{ target_root }}/{{ item.path }}" state: link force: true loop: "{{ lookup('vars', var_prefix ~ '_files') }}" when: - ansible_connection in ['local', 'localhost'] - use_symlinks | default(true) become: "{{ become_root | default(false) }}" - name: "Deploy files (local/remote copy) for {{ var_prefix }}" ansible.builtin.copy: src: "{{ item.src }}" dest: "{{ target_root }}/{{ item.path }}" mode: preserve loop: "{{ lookup('vars', var_prefix ~ '_files') }}" when: | (ansible_connection not in ['local', 'localhost']) or (not (use_symlinks | default(true))) become: "{{ become_root | default(false) }}" - name: "Render templates for {{ var_prefix }}" ansible.builtin.template: src: "{{ item.src }}" dest: "{{ target_root }}/{{ item.path | replace('.j2', '') }}" mode: preserve loop: "{{ lookup('vars', var_prefix ~ '_templates') }}" become: "{{ become_root | default(false) }}"