Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add os_openstacksdk role #4

Merged
merged 10 commits into from
Nov 23, 2023
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Ansible Collection - stackhpc.openstack

This collection includes content for interacting with OpenStack clouds.

## Included content

### Roles

- [os_openstacksdk](roles/os_openstacksdk): Install `openstacksdk` Python package
1 change: 1 addition & 0 deletions changelogs/.plugin-cache.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
objects:
role: {}
plugins:
Expand Down
3 changes: 2 additions & 1 deletion changelogs/changelog.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ancestor: null
---
ancestor:
releases: {}
1 change: 1 addition & 0 deletions changelogs/config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
changelog_filename_template: ../CHANGELOG.rst
changelog_filename_version_depth: 0
changes_file: changelog.yaml
Expand Down
1 change: 1 addition & 0 deletions galaxy.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
namespace: stackhpc
name: openstack
version: 1.0.0
Expand Down
45 changes: 45 additions & 0 deletions roles/os_openstacksdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
OpenStack openstacksdk
======================

This role can be used to install the python package `openstacksdk`.

Requirements
------------

Linux - none, macOS - brew.sh

Role Variables
--------------

`os_openstacksdk_venv` is a path to a directory in which to create a
virtualenv.

`os_openstacksdk_install_epel`: Whether to install EPEL repository package.

`os_openstacksdk_install_package_dependencies`: Whether to install package
dependencies.

`os_openstacksdk_version`: Version of `openstacksdk` to install, or latest if
empty.

Dependencies
------------

None

Example Playbook
----------------

The following playbook installs openstacksdk and its dependencies in a virtualenv.

---
- name: Ensure openstacksdk is installed
hosts: localhost
roles:
- role: stackhpc.openstack.os_openstacksdk
os_openstacksdk_venv: "~/os-openstacksdk-venv"

Author Information
------------------

- Mark Goddard (<[email protected]>)
21 changes: 21 additions & 0 deletions roles/os_openstacksdk/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
# 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 }}"

# Whether to install EPEL repository package.
os_openstacksdk_install_epel: true

# Whether to install package dependencies.
os_openstacksdk_install_package_dependencies: true

# State of the openstacksdk package.
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:
74 changes: 74 additions & 0 deletions roles/os_openstacksdk/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
- name: Gather OS specific variables
ansible.builtin.include_vars: "{{ item }}"
with_first_found:
- "{{ ansible_facts.distribution }}-{{ ansible_facts.distribution_major_version }}.yml"
- "{{ ansible_facts.distribution }}.yml"
- "{{ ansible_facts.os_family }}.yml"
tags: vars

- name: Ensure EPEL repo is installed
ansible.builtin.yum:
name: epel-release
state: present
become: true
when:
- os_openstacksdk_install_epel | bool
- ansible_facts.os_family == "RedHat"
- ansible_facts.distribution_major_version | int == 7

- name: Ensure apt cache is up to date (Debian)
ansible.builtin.apt:
update_cache: true
become: true
when: ansible_facts.os_family == "Debian"

- name: Ensure required packages are installed
ansible.builtin.package:
name: "{{ os_openstacksdk_package_dependencies }}"
state: present
become: "{{ ansible_facts.system != 'Darwin' }}"
when: os_openstacksdk_install_package_dependencies | bool

- name: Ensure the virtualenv directory exists
when: os_openstacksdk_venv is not none
block:
- name: Check whether the virtualenv directory exists
ansible.builtin.stat:
path: "{{ os_openstacksdk_venv | dirname }}"
get_md5: false
get_checksum: false
mime: false
register: os_openstacksdk_venv_stat

- name: Ensure the virtualenv directory exists
ansible.builtin.file:
path: "{{ os_openstacksdk_venv | dirname }}"
state: directory
owner: "{{ ansible_facts.user_uid }}"
group: "{{ ansible_facts.user_gid }}"
mode: "0700"
become: true
when:
- not os_openstacksdk_venv_stat.stat.exists or not os_openstacksdk_venv_stat.stat.writeable

- name: Ensure the latest versions of pip and setuptools are installed # noqa package-latest
ansible.builtin.pip:
name: "{{ item.name }}"
state: latest
virtualenv: "{{ os_openstacksdk_venv or omit }}"
virtualenv_command: "{{ os_openstacksdk_virtualenv_command }}"
with_items:
- { name: pip }
- { name: setuptools }

- name: Ensure required Python packages are installed
ansible.builtin.pip:
name: "{{ item.name }}"
version: "{{ item.version or omit }}"
state: "{{ os_openstacksdk_state }}"
virtualenv: "{{ os_openstacksdk_venv or omit }}"
extra_args: "{% if os_openstacksdk_upper_constraints_file %}-c {{ os_openstacksdk_upper_constraints_file }}{% endif %}"
with_items:
- name: openstacksdk
version: "{{ os_openstacksdk_version }}"
1 change: 1 addition & 0 deletions roles/os_openstacksdk/tests/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
localhost ansible_connection='local' ansible_python_interpreter='/usr/bin/env python'
6 changes: 6 additions & 0 deletions roles/os_openstacksdk/tests/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- name: Test os_openstacksdk
hosts: all
connection: local
roles:
- stackhpc.openstack.os_openstacksdk
4 changes: 4 additions & 0 deletions roles/os_openstacksdk/vars/Darwin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
os_openstacksdk_package_dependencies:
- python3
- virtualenv
8 changes: 8 additions & 0 deletions roles/os_openstacksdk/vars/Debian.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# List of package dependencies.
os_openstacksdk_package_dependencies:
- gcc
- libffi-dev
- libssl-dev
- python3-dev
- python3-pip
8 changes: 8 additions & 0 deletions roles/os_openstacksdk/vars/RedHat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# List of package dependencies.
os_openstacksdk_package_dependencies:
- gcc
- libffi-devel
- openssl-devel
- python3-devel
- python3-pip