tripleo-ansible/tripleo_ansible/roles/tripleo_transfer/tasks/main.yml

104 lines
3.8 KiB
YAML

---
# Copyright 2019 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
- name: make sure we don't have a trailing forward slash in the src
set_fact:
tripleo_transfer_src_dir_safe: "{{ tripleo_transfer_src_dir | regex_replace('\\/$', '') }}"
cacheable: false
- name: make sure we don't have a trailing forward slash in the dst
set_fact:
tripleo_transfer_dest_dir_safe: "{{ tripleo_transfer_dest_dir | regex_replace('\\/$', '') }}"
cacheable: false
- name: ensure local storage directory exists and has correct permissions
file:
path: "{{ tripleo_transfer_storage_root_dir }}"
# Attempting to set an owner fails with "chown failed: failed to
# look up user" so we at least ensure the permissions.
mode: 0700
state: directory
delegate_to: localhost
become: "{{ tripleo_transfer_storage_root_become }}"
- name: create tempfile for the archive
tempfile:
prefix: ansible.tripleo-transfer.
register: tripleo_transfer_tempfile
become: "{{ tripleo_transfer_src_become }}"
delegate_to: "{{ tripleo_transfer_src_host }}"
# Using the "archive" module lists lists all tarred files in module
# output, if there's too many files, it can crash ansible even with
# "no_log: "{{ not tripleo_transfer_debug | bool }}"".
- name: create the archive
shell: |-
set -euo pipefail
tar --transform "s|^{{ tripleo_transfer_src_dir_safe | basename }}|{{ tripleo_transfer_dest_dir_safe | basename }}|" \
-czf "{{ tripleo_transfer_tempfile.path }}" \
-C "{{ tripleo_transfer_src_dir_safe | dirname }}" \
"{{ tripleo_transfer_src_dir_safe | basename }}"
become: "{{ tripleo_transfer_src_become }}"
delegate_to: "{{ tripleo_transfer_src_host }}"
- name: fetch the archive
fetch:
src: "{{ tripleo_transfer_tempfile.path }}"
dest: "{{ tripleo_transfer_storage_root_dir }}/{{ tripleo_transfer_dest_host }}{{ tripleo_transfer_dest_dir_safe }}.tar.gz"
flat: true
become: "{{ tripleo_transfer_src_become }}"
delegate_to: "{{ tripleo_transfer_src_host }}"
- name: remove tempfile
file:
name: "{{ tripleo_transfer_tempfile.path }}"
state: absent
become: "{{ tripleo_transfer_src_become }}"
delegate_to: "{{ tripleo_transfer_src_host }}"
- include_tasks: flag.yml
when:
- tripleo_transfer_flag_file != None
- tripleo_transfer_flag_file|length > 0
- name: wipe the destination directory
file:
path: "{{ tripleo_transfer_dest_dir_safe }}"
state: absent
become: "{{ tripleo_transfer_dest_become }}"
delegate_to: "{{ tripleo_transfer_dest_host }}"
when: tripleo_transfer_dest_wipe|bool
- name: make sure the destination parent directory is present
file:
path: "{{ tripleo_transfer_dest_dir_safe|dirname }}"
state: directory
become: "{{ tripleo_transfer_dest_become }}"
delegate_to: "{{ tripleo_transfer_dest_host }}"
- name: push and extract the archive
unarchive:
src: "{{ tripleo_transfer_storage_root_dir }}/{{ tripleo_transfer_dest_host }}{{ tripleo_transfer_dest_dir_safe }}.tar.gz"
dest: "{{ tripleo_transfer_dest_dir_safe|dirname }}"
become: "{{ tripleo_transfer_dest_become }}"
delegate_to: "{{ tripleo_transfer_dest_host }}"
- name: remove the local archive
file:
path: "{{ tripleo_transfer_storage_root_dir }}/{{ tripleo_transfer_dest_host }}{{ tripleo_transfer_dest_dir_safe }}.tar.gz"
state: absent