summaryrefslogtreecommitdiff
path: root/tasks/copy-rendered-templates-recursive.yml
blob: 0733493ffd67dcf52879d5ef76706f31df5e2d3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
---

- 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