From 61ef59ff2cb64bede6da55347d9c2fa6f0870492 Mon Sep 17 00:00:00 2001 From: Sagi Shnaidman Date: Thu, 26 Jan 2017 02:26:49 +0200 Subject: [PATCH] Support uploading files in modify image role Support for uploading files to image. Change-Id: I5cf1959437097f410cc36e1a1536141bfc9da325 --- roles/modify-image/README.md | 27 +++++++++++++++++++++++++++ roles/modify-image/defaults/main.yml | 3 +++ roles/modify-image/tasks/main.yml | 27 ++++++++++++++++++--------- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/roles/modify-image/README.md b/roles/modify-image/README.md index 6e6a2013f..722613268 100644 --- a/roles/modify-image/README.md +++ b/roles/modify-image/README.md @@ -19,6 +19,8 @@ Role Variables * `image_to_modify` -- the image that virt-customize will operate on * `modify_script` -- the script that will be run inside the image +* `modify_image_upload_files` -- list of src/dest of files to upload to image + (files are uploaded before running the script) * `modify_image_extract_list` -- list of artifacts to extract after the image is modified * `modify_image_working_dir` -- directory containing image and script. This is @@ -71,6 +73,31 @@ Example Usage roles: - modify-image +- name: Upload files to image + hosts: virthost + vars: + image_to_modify: "{{ working_dir }}/undercloud.qcow2" + modify_image_upload_files: + - src: /tmp/file_to_upload.tar.gz + dest: /home/user/renamed_file.tar.gz + - src: /home/local_user/file + dest: /home/image_user/ + roles: + - modify-image + +- name: Upload files to image and run script + hosts: virthost + vars: + image_to_modify: "{{ working_dir }}/undercloud.qcow2" + modify_image_upload_files: + - src: /tmp/file_to_upload.tar.gz + dest: /home/user/renamed_file.tar.gz + - src: /home/local_user/file + dest: /tmp/ + modify_script: "{{ working_dir }}/undercloud_convert.sh" + roles: + - modify-image + ``` License diff --git a/roles/modify-image/defaults/main.yml b/roles/modify-image/defaults/main.yml index 1a593fe66..573bc43c9 100644 --- a/roles/modify-image/defaults/main.yml +++ b/roles/modify-image/defaults/main.yml @@ -6,6 +6,9 @@ # image_to_modify: # modify_script: +# The upload list of files +modify_image_upload_files: [] + # The extract list defaults to empty list modify_image_extract_list: [] diff --git a/roles/modify-image/tasks/main.yml b/roles/modify-image/tasks/main.yml index b37539186..4085261bc 100644 --- a/roles/modify-image/tasks/main.yml +++ b/roles/modify-image/tasks/main.yml @@ -5,7 +5,7 @@ msg: "In order to use this role image_to_modify and modify_script must be provided." - when: image_to_modify is not defined or modify_script is not defined + when: image_to_modify is not defined or (modify_script is not defined and modify_image_upload_files == []) - name: ensure libguestfs is installed yum: name=libguestfs-tools-c state=latest @@ -27,21 +27,30 @@ set_fact: vc_args="{{ vc_args }} -x" when: modify_image_vc_trace|bool +- name: virt-customize args --> upload files + set_fact: vc_args="{{ vc_args }} --upload {{ item.src }}:{{ item.dest }}" + with_items: "{{ modify_image_upload_files }}" + +- name: virt-customize args --> modify script + set_fact: vc_args="{{ vc_args }} --run {{ modify_script }}" + when: modify_script is defined + - name: Run virt-customize on the provided image - shell: virt-customize {{ vc_args }} - -a {{ image_to_modify }} - --run {{ modify_script }} - > {{ modify_script }}.log 2>&1 + shell: > + virt-customize {{ vc_args }} + -a {{ image_to_modify }} + > {{ modify_script|default('modify_image') }}.log 2>&1 environment: LIBGUESTFS_BACKEND: direct args: chdir: "{{ modify_image_working_dir }}" - name: Extract artifacts from the image - shell: virt-copy-out - -a {{ image_to_modify }} - {{ item }} - {{ modify_image_working_dir }} + shell: > + virt-copy-out + -a {{ image_to_modify }} + {{ item }} + {{ modify_image_working_dir }} environment: LIBGUESTFS_BACKEND: direct args: