Created
September 22, 2017 07:46
-
-
Save peterjenkins1/045c7a7ebd720c11a3cf4d7baf76dd1f to your computer and use it in GitHub Desktop.
Quick and dirty OpenStack image creation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
# Images are defined in group_vars/all/glance-images.yml. | |
# imagespec is used for individual images, e.g. "-e imagespec=centos-6,centos-7". | |
- name: Add images to OpenStack | |
hosts: localhost | |
# This environment block shouldn't be needed, but it doesn't work otherwise! | |
environment: | |
LIBGUESTFS_BACKEND: direct | |
vars: | |
workdir: /mnt/scratch # Directory where the images are temporarily stored | |
is_public: true # If you want to upload a private image, add "-e is_public=false" | |
image_owner: cloudy-admins | |
image_name_postfix: "" | |
hyper: "{{ kvm_server }}" # Infranode where images are customized. | |
tasks: | |
- name: Download new images | |
get_url: | |
url: "{{ item.value.url_prefix }}{{ item.value.filename }}" | |
dest: "{{ workdir }}/{{ item.value.filename }}" | |
mode: 0660 | |
with_dict: "{{glance_images}}" | |
when: imagespec is undefined or "{{item.key}}" in imagespec | |
- name: Customize images if needed | |
include: hooks/glance-image-customize.yml | |
with_dict: "{{ glance_images }}" | |
when: | |
- item.value.modify_hook is defined | |
- imagespec is undefined or "{{item.key}}" in imagespec | |
- name: Convert qcow2 images to raw format | |
command: "qemu-img convert -f qcow2 -O raw {{ workdir }}/{{ item.value.filename }} {{ workdir }}/{{ item.value.filename }}.raw" | |
with_dict: "{{ glance_images }}" | |
when: imagespec is undefined or "{{ item.key }}" in imagespec | |
- name: Upload images to Glance | |
os_image: | |
name: "{{ item.key }}{{ image_name_postfix }}" | |
filename: "{{ workdir }}/{{ item.value.filename }}.raw" | |
is_public: "{{ is_public }}" | |
disk_format: raw | |
owner: "{{ image_owner }}" | |
properties: | |
hw_scsi_model: virtio-scsi | |
hw_disk_bus: scsi | |
with_dict: "{{ glance_images }}" | |
when: imagespec is undefined or "{{ item.key }}" in imagespec | |
- name: Delete local qcow2 images | |
file: | |
path: "{{ workdir }}/{{ item.value.filename }}" | |
state: absent | |
with_dict: "{{ glance_images }}" | |
when: imagespec is undefined or "{{ item.key }}" in imagespec | |
- name: Delete local raw images | |
file: | |
path: "{{ workdir }}/{{ item.value.filename }}.raw" | |
state: absent | |
with_dict: "{{ glance_images }}" | |
when: imagespec is undefined or "{{ item.key }}" in imagespec |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Obviously you guys need to build images in some cases, and our approach is totally dependant on the upstream images (and they are far from perfect), but never the less it's turned out to be a pretty good solution and saved me a lot of time!
I take the philosophy that we can fix bad/broken images as needed with ansible, virt-customize or guestfish. See the related hacks in a-r-provision-vm (our fork) https://github.com/SoneraCloud/ansible-role-provision-vm/blob/master/tasks/image-guest.yml#L66-L87