--- - name: Create temporary directory on localhost delegate_to: localhost become: false ansible.builtin.tempfile: state: directory register: tempdir - name: Ensure parent directories exist for rendered templates delegate_to: localhost become: false ansible.builtin.file: path: "{{ tempdir.path }}/{{ item.path | dirname }}" state: directory mode: "{{ mode | default('0755') }}" with_filetree: "{{ render_dir }}" when: item.state == "file" - name: Recursively render templates delegate_to: localhost become: false ansible.builtin.template: src: "{{ item.src }}" dest: "{{ tempdir.path }}/{{ item.path }}" mode: "{{ mode | default('0755') }}" with_filetree: "{{ render_dir }}" when: item.state == "file" - name: Sync rendered templates to remote host delegate_to: localhost become: false ansible.builtin.synchronize: src: "{{ tempdir.path }}/" dest: "{{ tempdir.path }}/" - name: Remove local temporary directory delegate_to: localhost become: false ansible.builtin.file: path: "{{ tempdir.path }}" state: absent - name: Ensure destination exists ansible.builtin.file: path: "{{ destination_dir }}" state: directory - name: Update remote files ansible.builtin.command: cmd: bash -c 'cp -r {{ tempdir.path }}/* {{ destination_dir }}/' - name: Remove local temporary directory delegate_to: localhost become: false ansible.builtin.file: path: "{{ tempdir.path }}" state: absent - name: Remove remote temporary directory ansible.builtin.file: path: "{{ tempdir.path }}" state: absent