Samuli Silvius | 29886f0 | 2019-02-19 16:00:37 +0200 | [diff] [blame^] | 1 | --- |
| 2 | # |
| 3 | # Expected variables |
| 4 | # resources_source_host |
| 5 | # resources_dir |
| 6 | # resource_source_filename |
| 7 | # resource_destination_directory |
| 8 | # Output is upload_failed true/false |
| 9 | # |
| 10 | - name: "Upload resource {{ resources_dir }}/{{ resource_source_filename }} to infrastructure servers over ssh" |
| 11 | block: |
| 12 | - name: Upload ssh private key |
| 13 | copy: |
| 14 | src: "{{ ansible_ssh_private_key_file }}" |
| 15 | dest: /root/.ssh/infra_to_resource.privkey |
| 16 | mode: 0600 |
| 17 | owner: root |
| 18 | group: root |
| 19 | remote_src: no |
| 20 | |
| 21 | - name: Detect if archive is compressed |
| 22 | shell: > |
| 23 | ssh -o StrictHostKeyChecking=no |
| 24 | -i /root/.ssh/infra_to_resource.privkey |
| 25 | {{ resources_source_host }} |
| 26 | 'file "{{ resources_dir }}/{{ resource_source_filename }}"' |
| 27 | | grep "compressed" |
| 28 | register: compressed |
| 29 | |
| 30 | - name: Set tar extract options |
| 31 | set_fact: |
| 32 | tar_extract_options: "{{ '-xzf' if compressed.rc == 0 else '-xf' }}" |
| 33 | |
| 34 | - name: "Unarchive resource {{ resources_dir }}/{{ resource_source_filename }} to {{ resource_destination_directory }} dir on infrastructure servers over ssh" |
| 35 | shell: > |
| 36 | ssh -o StrictHostKeyChecking=no -o BatchMode=yes |
| 37 | -i /root/.ssh/infra_to_resource.privkey |
| 38 | {{ resources_source_host }} |
| 39 | 'cat "{{ resources_dir }}/{{ resource_source_filename }}"' |
| 40 | | tar -C "{{ resource_destination_directory }}" "{{ tar_extract_options }}" - |
| 41 | args: |
| 42 | warn: false |
| 43 | rescue: |
| 44 | - name: Upload failed |
| 45 | set_fact: |
| 46 | upload_failed: true |
| 47 | always: |
| 48 | - name: Remove the ssh private key |
| 49 | file: |
| 50 | path: /root/.ssh/infra_to_resource.privkey |
| 51 | state: absent |