summaryrefslogtreecommitdiffstats
path: root/ansible/roles/resource-data/tasks/ssh-upload.yml
blob: 8e04d5c0fb68bf14360f0c182b3ad93a00ddc09b (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
---
- name: Upload resources to infrastructure servers over ssh
  block:
    - name: Upload ssh private key
      copy:
        src: "{{ ansible_ssh_private_key_file }}"
        dest: /root/.ssh/infra_to_resource.privkey
        mode: 0600
        owner: root
        group: root
        remote_src: no

    - name: Unarchive resources
      shell: >
        ssh -o StrictHostKeyChecking=no -o BatchMode=yes
        -i /root/.ssh/infra_to_resource.privkey
        {{ hostvars[groups.resources.0].ansible_host }}
        'cat "{{ hostvars[groups.resources.0].resources_dir }}/{{ hostvars[groups.resources.0].resources_filename }}"'
        | tar -C "{{ app_data_path }}" -xf -
      args:
        warn: False
      when: not resources_data_check.stat.exists

    - name: Unarchive auxiliary resources
      shell: >
        ssh -i /root/.ssh/infra_to_resource.privkey
        {{ hostvars[groups.resources.0].ansible_host }}
        'cat "{{ hostvars[groups.resources.0].resources_dir }}/{{ hostvars[groups.resources.0].aux_resources_filename }}"'
        | tar -C "{{ aux_data_path }}" -xf -
      when: >
        hostvars[groups.resources.0].aux_resources_filename is defined
        and aux_data_path is defined and aux_data_path is not none
        and hostvars[groups.resources.0].aux_file_presence.stat.exists
        and not aux_resources_data_check.stat.exists
      args:
        warn: False

  rescue:
    - name: Removing the resources data due to an error - so the next run can try again
      command: /bin/false
      register: upload_failed

  always:
    - name: Remove the ssh private key
      file:
        path: /root/.ssh/infra_to_resource.privkey
        state: absent

    - name: Remove the resource data on error
      file:
        path: "{{ app_data_path }}"
        state: absent
      when: upload_failed is defined

    - name: Remove the auxilliary resource data on error
      file:
        path: "{{ aux_data_path }}"
        state: absent
      when: upload_failed is defined