Add a volume to an Openstack Instance

Summary

As a follow on to a similar post regarding vmware disks , this post will focus on a playbook to attach extra volumes to an Openstack instance.

Requirements

  • An account with appropriate access to add volumes in OSP.
  • A valid instance.
  • Enough resources to handle the new volumes.

Get ‘er done!

The playbook requires an instance name and a dictionary containing pairs of disks and sizes to be added. I have made this flexible enough to allow a user to add 1 or more volumes to an instance. What is doing on with the dictsort in the loop? I wanted a way to order the keys in the dictionary so that the disks would be added in the order they are listed.

main.yml


---
 
- name: create new volume(s) on openstack instance
  hosts: localhost
  connection: local
  vars:
    username: osp_user
    password: XXXX
    auth_url: https://openstack.example.com/v3.0/
    project: osp_project_name
    instance_name: instance_name
    volume_list:
      vol_1: 10
      vol_2: 20
  tasks:
  - name: iterate over volume_list dict, run create_volumes.yml on each volume
    include_tasks: create_volume.yml
    loop: "{{ volume_list|dictsort }}"
    loop_control:
      loop_var: volume

create_volume.yml


---
 
- name: check if host exists
  os_server_facts:
    auth:
      auth_url: "{{ os_server }}"
      username: "{{ username }}"
      password: "{{ password }}"
      project_name: "{{ project }}"
      project_domain_name: "{{ domain }}"
      user_domain_name: "{{ domain }}"
    server: "{{ instance_name }}"
    verify: no
- name: fail if no instance found
  fail:
    msg: "{{ instance_name }} doesn't exist in Openstack project {{ project }}."
  when: openstack_servers|length == 0
- debug:
    var: volume.1
- name: create new volume
  os_volume:
    auth:
      auth_url: "{{ os_server }}"
      username: "{{ username }}"
      password: "{{ password }}"
      project_name: "{{ project }}"
      project_domain_name: "{{ domain }}"
      user_domain_name: "{{ domain }}"
    state: present
    size: "{{ volume.1 | int }}"
    display_name: "{{ volume.0 }}"
    verify: no
- name: attach volume to instance
  os_server_volume:
    auth:
      auth_url: "{{ os_server }}"
      username: "{{ username }}"
      password: "{{ password }}"
      project_name: "{{ project }}"
      project_domain_name: "{{ domain }}"
      user_domain_name: "{{ domain }}"
    state: present
    server: "{{ instance_name }}"
    volume: "{{ volume.0 }}"
    verify: no

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s