Skip to content

Instantly share code, notes, and snippets.

@peterjenkins1
Created September 22, 2017 07:46
Show Gist options
  • Save peterjenkins1/045c7a7ebd720c11a3cf4d7baf76dd1f to your computer and use it in GitHub Desktop.
Save peterjenkins1/045c7a7ebd720c11a3cf4d7baf76dd1f to your computer and use it in GitHub Desktop.
Quick and dirty OpenStack image creation
---
# 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
@peterjenkins1
Copy link
Author

peterjenkins1 commented Sep 22, 2017

@junousi I saw your pull request for DIB (I should really unfollow all the old CSC repos!). Since you are looking at images, I thought I'd just share the very simple hack we have for getting images. We have this one playbook (which needs some refactoring love and should be a role), and a config file with a list of images:

glance_images:
  cirros-0.3.4:
    url_prefix: "http://download.cirros-cloud.net/0.3.4/"
    filename: cirros-0.3.4-x86_64-disk.img
    user: cirros
  ubuntu-14.04.4:
    url_prefix: "http://cloud-images.ubuntu.com/trusty/current/"
    filename: trusty-server-cloudimg-amd64-disk1.img
    user: ubuntu
  ubuntu-16.04:
    url_prefix: "http://cloud-images.ubuntu.com/xenial/current/"
    filename: xenial-server-cloudimg-amd64-disk1.img
    user: ubuntu
    modify_hook: ubu16-image-customize.yml

@peterjenkins1
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment