-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into stackhpc.os-volumes
- Loading branch information
Showing
8 changed files
with
348 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright (c) 2023 StackHPC Ltd. | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
# This module has been proposed to the OpenStack collection: | ||
# https://review.opendev.org/c/openstack/ansible-collections-openstack/+/874755 | ||
# Keep a copy here until it merges. | ||
# It requires OpenStack SDK 0.53.0. | ||
|
||
from __future__ import (absolute_import, division, print_function) | ||
__metaclass__ = type | ||
|
||
DOCUMENTATION = r''' | ||
module: baremetal_deploy_template | ||
short_description: Create/Delete Bare Metal deploy template Resources from OpenStack | ||
author: OpenStack Ansible SIG | ||
description: | ||
- Create, Update and Remove ironic deploy templates from OpenStack. | ||
options: | ||
extra: | ||
description: | ||
- A set of one or more arbitrary metadata key and value pairs. | ||
type: dict | ||
id: | ||
description: | ||
- ID of the deploy template. | ||
- Will be auto-generated if not specified. | ||
type: str | ||
aliases: ['uuid'] | ||
name: | ||
description: | ||
- Name of the deploy template. | ||
- Must be formatted as a trait name (see API reference). | ||
- Required when the deploy template is created, after which the | ||
name or ID may be used. | ||
type: str | ||
steps: | ||
description: | ||
- List of deploy steps to apply. | ||
- Required when the deploy template is created. | ||
type: list | ||
elements: dict | ||
state: | ||
description: | ||
- Indicates desired state of the resource | ||
choices: ['present', 'absent'] | ||
default: present | ||
type: str | ||
extends_documentation_fragment: | ||
- openstack.cloud.openstack | ||
''' | ||
|
||
EXAMPLES = r''' | ||
- name: Create Bare Metal deploy template | ||
openstack.cloud.baremetal_deploy_template: | ||
cloud: devstack | ||
state: present | ||
name: CUSTOM_FOO | ||
steps: | ||
- interface: bios | ||
step: apply_configuration | ||
args: | ||
settings: | ||
- name: LogicalProc | ||
value: Enabled | ||
priority: 110 | ||
extra: | ||
something: extra | ||
register: result | ||
- name: Delete Bare Metal deploy template | ||
openstack.cloud.baremetal_deploy_template: | ||
cloud: devstack | ||
state: absent | ||
id: 1a85ebca-22bf-42eb-ad9e-f640789b8098 | ||
register: result | ||
- name: Update Bare Metal deploy template | ||
openstack.cloud.baremetal_deploy_template: | ||
cloud: devstack | ||
state: present | ||
id: 1a85ebca-22bf-42eb-ad9e-f640789b8098 | ||
extra: | ||
something: new | ||
''' | ||
|
||
RETURN = r''' | ||
template: | ||
description: A deploy template dictionary, subset of the dictionary keys | ||
listed below may be returned, depending on your cloud | ||
provider. | ||
returned: success | ||
type: dict | ||
contains: | ||
created_at: | ||
description: Bare Metal deploy template created at timestamp. | ||
returned: success | ||
type: str | ||
extra: | ||
description: A set of one or more arbitrary metadata key and value | ||
pairs. | ||
returned: success | ||
type: dict | ||
id: | ||
description: The UUID for the Baremetal Deploy Template resource. | ||
returned: success | ||
type: str | ||
links: | ||
description: A list of relative links, including the self and | ||
bookmark links. | ||
returned: success | ||
type: list | ||
location: | ||
description: Cloud location of this resource (cloud, project, | ||
region, zone) | ||
returned: success | ||
type: dict | ||
name: | ||
description: Bare Metal deploy template name. | ||
returned: success | ||
type: str | ||
steps: | ||
description: A list of deploy steps. | ||
returned: success | ||
type: list | ||
elements: dict | ||
updated_at: | ||
description: Bare Metal deploy template updated at timestamp. | ||
returned: success | ||
type: str | ||
''' | ||
|
||
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import ( | ||
OpenStackModule | ||
) | ||
|
||
|
||
class BaremetalDeployTemplateModule(OpenStackModule): | ||
argument_spec = dict( | ||
extra=dict(type='dict'), | ||
id=dict(aliases=['uuid']), | ||
name=dict(), | ||
steps=dict(type='list', elements='dict'), | ||
state=dict(default='present', choices=['present', 'absent']), | ||
) | ||
|
||
module_kwargs = dict( | ||
required_one_of=[ | ||
('id', 'name'), | ||
], | ||
) | ||
|
||
def run(self): | ||
template = self._find_deploy_template() | ||
state = self.params['state'] | ||
if state == 'present': | ||
# create or update deploy template | ||
|
||
kwargs = {} | ||
for k in ['extra', 'id', 'name', 'steps']: | ||
if self.params[k] is not None: | ||
kwargs[k] = self.params[k] | ||
|
||
changed = True | ||
if not template: | ||
# create deploy template | ||
template = self.conn.baremetal.create_deploy_template(**kwargs) | ||
else: | ||
# update deploy template | ||
updates = dict((k, v) | ||
for k, v in kwargs.items() | ||
if v != template[k]) | ||
|
||
if updates: | ||
template = \ | ||
self.conn.baremetal.update_deploy_template(template['id'], **updates) | ||
else: | ||
changed = False | ||
|
||
self.exit_json(changed=changed, template=template.to_dict(computed=False)) | ||
|
||
if state == 'absent': | ||
# remove deploy template | ||
if not template: | ||
self.exit_json(changed=False) | ||
|
||
template = self.conn.baremetal.delete_deploy_template(template['id']) | ||
self.exit_json(changed=True) | ||
|
||
def _find_deploy_template(self): | ||
id_or_name = self.params['id'] if self.params['id'] else self.params['name'] | ||
try: | ||
return self.conn.baremetal.get_deploy_template(id_or_name) | ||
except self.sdk.exceptions.ResourceNotFound: | ||
return None | ||
|
||
|
||
def main(): | ||
module = BaremetalDeployTemplateModule() | ||
module() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
OpenStack Ironic deploy templates | ||
================================= | ||
|
||
This role can be used to register deploy templates in OpenStack ironic. | ||
|
||
What are deploy templates? Read the | ||
[docs](https://docs.openstack.org/ironic/latest/admin/node-deployment.html) or | ||
watch the [video](https://www.youtube.com/watch?v=DrQcTljx_eM) to find out! | ||
|
||
Requirements | ||
------------ | ||
|
||
The OpenStack Ironic API should be accessible from the target host. | ||
|
||
Role Variables | ||
-------------- | ||
|
||
`os_deploy_templates_venv` is a path to a directory in which to create a | ||
virtualenv. | ||
|
||
`os_deploy_templates_upper_constraints_file` is a file or URL containing Python | ||
upper constraints. | ||
|
||
`os_deploy_templates_auth_type` is an authentication type compatible with | ||
the `auth_type` argument of `openstack.cloud` Ansible modules. | ||
|
||
`os_deploy_templates_auth` is a dict containing authentication information | ||
compatible with the `auth` argument of `openstack.cloud` Ansible modules. | ||
|
||
`os_deploy_templates_cacert` is an optional path to a CA certificate bundle. | ||
|
||
`os_deploy_templates_interface` is the endpoint URL type to fetch from the service | ||
catalog. Maybe be one of `public`, `admin`, or `internal`. | ||
|
||
`os_deploy_templates` is a list of Ironic deploy templates to register. Each | ||
item should be a dict containing following items: | ||
* `name`: Name of the deploy template. | ||
* `steps`: List of deploy steps. | ||
* `extra`: Dict of metadata, optional. | ||
* `uuid`: UUID, optional. | ||
* `state`: State, optional. | ||
|
||
Dependencies | ||
------------ | ||
|
||
This role depends on the `stackhpc.openstack.os_openstacksdk` role. | ||
|
||
Example Playbook | ||
---------------- | ||
|
||
The following playbook registers an Ironic deploy template. | ||
|
||
``` | ||
--- | ||
- name: Ensure Ironic deploy templates are registered | ||
hosts: os-clients | ||
tasks: | ||
- import_role: | ||
name: stackhpc.openstack.os_deploy_templates | ||
vars: | ||
os_deploy_templates_venv: "~/os-deploy_templates-venv" | ||
os_deploy_templates_auth_type: "password" | ||
os_deploy_templates_auth: | ||
project_name: <keystone project> | ||
username: <keystone user> | ||
password: <keystone password> | ||
auth_url: <keystone auth URL> | ||
os_deploy_templates: | ||
- name: CUSTOM_HYPERTHREADING_ENABLED | ||
steps: | ||
- interface: bios | ||
step: apply_configuration | ||
args: | ||
settings: | ||
- name: LogicalProc | ||
value: Enabled | ||
priority: 110 | ||
``` | ||
|
||
Author Information | ||
------------------ | ||
|
||
- Mark Goddard (<[email protected]>) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--- | ||
# Path to a directory in which to create a virtualenv. | ||
os_deploy_templates_venv: | ||
|
||
# Upper constraints file for installation of python dependencies. | ||
# | ||
# Use Yoga upper constraints, to avoid incompatibility with openstacksdk | ||
# 0.99.0 and include support for deploy templates (included since openstacksdk | ||
# 0.53). | ||
os_deploy_templates_upper_constraints_file: https://releases.openstack.org/constraints/upper/yoga | ||
|
||
# Authentication type. | ||
os_deploy_templates_auth_type: | ||
|
||
# Authentication information. | ||
os_deploy_templates_auth: {} | ||
|
||
# Endpoint URL type to fetch from the service catalog. Maybe be one of: | ||
# public, admin, or internal. | ||
os_deploy_templates_interface: | ||
|
||
# List of Ironic deploy templates to register. Each item should be a dict | ||
# containing the following items: | ||
# - 'name': Name of the deploy template. | ||
# - 'steps': List of deploy steps. | ||
# - 'extra': Dict of metadata, optional. | ||
# - 'uuid': UUID, optional. | ||
# - 'state': State, optional. | ||
os_deploy_templates: [] # noqa var-naming[no-role-prefix] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
dependencies: | ||
- role: stackhpc.openstack.os_openstacksdk | ||
os_openstacksdk_venv: "{{ os_deploy_templates_venv }}" | ||
os_openstacksdk_upper_constraints_file: "{{ os_deploy_templates_upper_constraints_file }}" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
- name: Ensure Ironic deploy templates exist | ||
stackhpc.openstack.baremetal_deploy_template: | ||
auth_type: "{{ os_deploy_templates_auth_type }}" | ||
auth: "{{ os_deploy_templates_auth }}" | ||
cacert: "{{ os_deploy_templates_cacert | default(omit) }}" | ||
interface: "{{ os_deploy_templates_interface | default(omit, true) }}" | ||
name: "{{ item.name }}" | ||
steps: "{{ item.steps }}" | ||
uuid: "{{ item.uuid | default(omit) }}" | ||
extra: "{{ item.extra | default(omit) }}" | ||
state: "{{ item.state | default(omit) }}" | ||
loop: "{{ os_deploy_templates }}" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 deploy_templates.yml | ||
ansible.builtin.import_tasks: deploy_templates.yml | ||
vars: | ||
ansible_python_interpreter: "{{ os_deploy_templates_venv ~ '/bin/python' if os_deploy_templates_venv != None else old_ansible_python_interpreter }}" |