diff --git a/roles/os_openstackclient/defaults/main.yml b/roles/os_openstackclient/defaults/main.yml index c38f195..63ed4ee 100644 --- a/roles/os_openstackclient/defaults/main.yml +++ b/roles/os_openstackclient/defaults/main.yml @@ -1,7 +1,6 @@ --- # Path to a directory in which to create a virtualenv. os_openstackclient_venv: - # Whether to install package dependencies. os_openstackclient_install_package_dependencies: true @@ -10,6 +9,5 @@ os_openstackclient_state: present # Version of python-openstackclient to install, or unrestricted if empty. os_openstackclient_version: - # Upper constraints file for installation of openstackclient. os_openstackclient_upper_constraints_file: diff --git a/roles/os_openstacksdk/defaults/main.yml b/roles/os_openstacksdk/defaults/main.yml index 54b9d97..90535ba 100644 --- a/roles/os_openstacksdk/defaults/main.yml +++ b/roles/os_openstacksdk/defaults/main.yml @@ -1,7 +1,6 @@ --- # Path to a directory in which to create a virtualenv. os_openstacksdk_venv: - # Value to use for the pip module's `virtualenv_command` argument when creating the virtualenv os_openstacksdk_virtualenv_command: "{{ 'python3.' ~ ansible_facts.python.version.minor ~ ' -m venv' if os_openstacksdk_venv else omit }}" @@ -13,6 +12,5 @@ os_openstacksdk_state: present # Version of openstacksdk to install, or unrestricted if empty. os_openstacksdk_version: - # Upper constraints file for installation of openstacksdk. os_openstacksdk_upper_constraints_file: diff --git a/roles/os_volumes/README.md b/roles/os_volumes/README.md new file mode 100644 index 0000000..d45cf1e --- /dev/null +++ b/roles/os_volumes/README.md @@ -0,0 +1,77 @@ +OpenStack Volumes +================= + +This role can be used to register volumes and volume types in cinder using the +openstack.cloud.volume module. + +Requirements +------------ + +The OpenStack cinder API should be accessible from the target host. + +Role Variables +-------------- + +`os_volumes_venv` is a path to a directory in which to create a virtualenv. + +`os_volumes_auth_type` is an authentication type compatible with the +`auth_type` argument of `openstack.cloud` Ansible modules. + +`os_volumes_auth` is a dict containing authentication information +compatible with the `auth` argument of `openstack.cloud` Ansible modules. + +`os_volumes` is a list of volumes to register. Each item should be a dict +containing the following items: +- `display_description`: Optional description of the volume. +- `display_name`: Name of the volume. +- `image`: Optional image name or ID for boot from volume. +- `scheduler_hints`: Optional dict of scheduler hints pass to the volume API. +- `size`: Size of the volume in GB. +- `snapshot_id`: Optional ID of a volume snapshot from which to create the + volume. +- `state`: Optional state of the volume, default is `present`. +- `volume`: Optional name or ID of a volume from which to create the volume. +- `volume_type`: Optional type of the volume. + +`os_volumes_types` is a list of volume types to register. Each item should be a +dict containing the following items: +- `name`: Name of the volume type. +- `description`: Optional description of the volume type. +- `public`: Whether the volume type is public, default value is `True`. +- `extra_specs`: Optional dict of additional specifications for the volume + type. + +Dependencies +------------ + +This role depends on the `stackhpc.openstack.os_openstacksdk` role. + +Example Playbook +---------------- + +The following playbook registers a cinder volume and volume type. + + --- + - name: Ensure volumes and volume types are registered + hosts: cinder-api + roles: + - role: stackhpc.openstack.os_volumes + os_volumes_venv: "~/os-volumes-venv" + os_volumes_auth_type: "password" + os_volumes_auth: + project_name: + username: + password: + auth_url: + os_volumes: + - display_name: my-volume + size: 3 + os_volumes_types: + - name: type-1 + extra_specs: + volume_backend_name: my-backend + +Author Information +------------------ + +- Mark Goddard () diff --git a/roles/os_volumes/defaults/main.yml b/roles/os_volumes/defaults/main.yml new file mode 100644 index 0000000..f8d8db2 --- /dev/null +++ b/roles/os_volumes/defaults/main.yml @@ -0,0 +1,34 @@ +--- +# Path to virtualenv in which to install openstacksdk and its dependencies. +os_volumes_venv: + +# Authentication type compatible with the 'os_volume' Ansible module's +# auth_type argument. +os_volumes_auth_type: + +# Authentication parameters compatible with the 'os_volume' Ansible module's +# auth argument. +os_volumes_auth: {} + +# List of volumes to register. Each item should be a dict containing the +# following items: +# - 'display_description': Optional description of the volume. +# - 'display_name': Name of the volume. +# - 'image': Optional image name or ID for boot from volume. +# - 'scheduler_hints': Optional dict of scheduler hints pass to the volume API. +# - 'size': Size of the volume in GB. +# - 'snapshot_id': Optional ID of a volume snapshot from which to create the +# volume. +# - 'state': Optional state of the volume, default is 'present'. +# - 'volume': Optional name or ID of a volume from which to create the volume. +# - 'volume_type': Optional type of the volume. +os_volumes: [] # noqa: var-naming[no-role-prefix] + +# List of volume types to register. Each item should be a dict containing the +# following items: +# - 'name': Name of the volume type. +# - 'description': Optional description of the volume type. +# - 'public': Whether the volume type is public, default value is 'True'. +# - 'extra_specs': Optional dict of additional specifications for the volume +# type. +os_volumes_types: [] diff --git a/roles/os_volumes/meta/main.yml b/roles/os_volumes/meta/main.yml new file mode 100644 index 0000000..178dd67 --- /dev/null +++ b/roles/os_volumes/meta/main.yml @@ -0,0 +1,6 @@ +--- +dependencies: + - role: stackhpc.openstack.os_openstacksdk + os_openstacksdk_venv: "{{ os_volumes_venv }}" + - role: stackhpc.openstack.os_openstackclient + os_openstackclient_venv: "{{ os_volumes_venv }}" diff --git a/roles/os_volumes/tasks/main.yml b/roles/os_volumes/tasks/main.yml new file mode 100644 index 0000000..a16059f --- /dev/null +++ b/roles/os_volumes/tasks/main.yml @@ -0,0 +1,9 @@ +--- +- name: Set a fact about the Ansible python interpreter + ansible.builtin.set_fact: + old_ansible_python_interpreter: "{{ ansible_python_interpreter | default('/usr/bin/python3') }}" + +- name: Import task volumes.yml + ansible.builtin.import_tasks: volumes.yml + vars: + ansible_python_interpreter: "{{ os_volumes_venv ~ '/bin/python' if os_volumes_venv != None else old_ansible_python_interpreter }}" diff --git a/roles/os_volumes/tasks/volumes.yml b/roles/os_volumes/tasks/volumes.yml new file mode 100644 index 0000000..f07aeba --- /dev/null +++ b/roles/os_volumes/tasks/volumes.yml @@ -0,0 +1,62 @@ +--- +- name: Query volume types in cinder + ansible.builtin.shell: >- + source {{ os_volumes_venv }}/bin/activate && + openstack + {% for auth_name, auth_value in os_volumes_auth.items() %} + --os-{{ auth_name | replace('_', '-') }}='{{ auth_value }}' + {% endfor %} + --os-interface=public + volume type show {{ item.name }} + with_items: "{{ os_volumes_types }}" + changed_when: false + failed_when: false + environment: + OS_IDENTITY_API_VERSION: "3" + register: os_volumes_types_result + loop_control: + label: "{{ item.name }}" + +# NOTE: Currently updating volume types is not supported. +- name: Ensure volume type is registered with cinder + ansible.builtin.shell: >- + source {{ os_volumes_venv }}/bin/activate && + openstack + {% for auth_name, auth_value in os_volumes_auth.items() %} + --os-{{ auth_name | replace('_', '-') }}='{{ auth_value }}' + {% endfor %} + --os-interface=public + volume type create + {{ volume_type.name }} + {% if 'description' in volume_type %} --description '{{ volume_type.description }}'{% endif %} + {% if volume_type.get('public', True) %} --public{% else %} --private{% endif %} + {% for key, value in volume_type.get('extra_specs', {}).items() %} --property {{ key }}='{{ value }}'{% endfor %} + with_items: "{{ os_volumes_types_result.results }}" + changed_when: true + when: item.rc != 0 + environment: + OS_IDENTITY_API_VERSION: "3" + vars: + volume_type: "{{ item.item }}" + loop_control: + label: "{{ volume_type.name }}" + +# FIXME: Ansible's os_volume module cannot specify the project for a volume. +- name: Ensure volume is registered with cinder + openstack.cloud.volume: + auth_type: "{{ os_volumes_auth_type }}" + auth: "{{ os_volumes_auth }}" + display_name: "{{ item.display_name }}" + display_description: "{{ item.display_description | default(omit) }}" + image: "{{ item.image | default(omit) }}" + scheduler_hints: "{{ item.scheduler_hints | default(omit) }}" + size: "{{ item.size | default(omit) }}" + snapshot_id: "{{ item.snapshot_id | default(omit) }}" + state: "{{ item.state | default(omit) }}" + volume: "{{ item.volume | default(omit) }}" + volume_type: "{{ item.volume_type | default(omit) }}" + with_items: "{{ os_volumes }}" + environment: + OS_IDENTITY_API_VERSION: "3" + loop_control: + label: "{{ item.display_name }}" diff --git a/roles/os_volumes/tests/inventory b/roles/os_volumes/tests/inventory new file mode 100644 index 0000000..2fbb50c --- /dev/null +++ b/roles/os_volumes/tests/inventory @@ -0,0 +1 @@ +localhost diff --git a/roles/os_volumes/tests/test.yml b/roles/os_volumes/tests/test.yml new file mode 100644 index 0000000..61a0fbb --- /dev/null +++ b/roles/os_volumes/tests/test.yml @@ -0,0 +1,5 @@ +--- +- name: Test volumes + hosts: localhost + roles: + - os_volumes