summaryrefslogtreecommitdiffstats
path: root/ansible/roles/resource-data/tasks/unarchive-ssh-resource.yml
blob: 1385ba55392eca9356e85f0fb2b99785ec626b95 (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
---
#
# Expected variables
#   resources_source_host
#   resources_dir
#   resource_source_filename
#   resource_destination_directory
# Output is upload_failed true/false
#
- name: "Upload resource {{ resources_dir }}/{{ resource_source_filename }} 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

    - name: Detect if archive is compressed
      shell: >
        file "{{ resources_dir }}/{{ resource_source_filename }}"
        | grep "compressed"
      register: compressed
      failed_when: compressed.rc > 1
      delegate_to: "{{ resources_source_host }}"

    - name: Set tar extract options
      set_fact:
        tar_extract_options: "{{ '-xzf' if compressed.rc == 0 else '-xf' }}"

    - name: "Unarchive resource {{ resources_dir }}/{{ resource_source_filename }} to {{ resource_destination_directory }} dir on infrastructure servers over ssh"
      shell: >
        ssh -o StrictHostKeyChecking=no -o BatchMode=yes
        -i /root/.ssh/infra_to_resource.privkey
        {{ resources_source_host }}
        'cat "{{ resources_dir }}/{{ resource_source_filename }}"'
        | tar -C "{{ resource_destination_directory }}" "{{ tar_extract_options }}" -
      args:
        warn: false
  always:
    - name: Remove the ssh private key
      file:
        path: /root/.ssh/infra_to_resource.privkey
        state: absent