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

create content_view_promotion role #1216

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions roles/content_view_promote/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
theforeman.foreman.content_view_promote
=========

A role for automating the promotion of Foreman Content-Views through various LifeCycle Environemnts.

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

This role requires the theforeman.foreman module collection.

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

This role requires most of the common foreman variable more noteably:

`foreman_organization`: The Organization that the Content-View belongs to.

`foreman_username`: A foreman user that has access rights to publish new Content-View versions in the aforementioned Organization.

`foreman_password`: The password for the user. foreman_server_url: The URL used to access foreman.

As well as two additional variables:

`foreman_content_view`: The name of the Content-View which should have Lifecycle Environments promoted.

`foreman_lifecycle_envrionments`: A list of Lifecycle Environments that should be promtoed.

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

You need a Foreman user with admin access to the Organization, Lifecycle_Environment, and Content_View you wish to interact with.

By default, the role will require a valid SSL certificate installed on your Foreman server that the ansible client can trace trust to. To disable that update the 'FOREMAN_VALIDATE_CERTS' variable in defaults/main.yml.

For example, to disable certificate checking you would update the variable as such:
```
FOREMAN_VALIDATE_CERTS: false
```

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

The role can be instantiated quite simply, all of the decision making is handled by the variables previously set:

```
---
- name: "Run the content_view_promotion Role"
hosts: all
tasks:
- name: "Run the content_view_promotion Role"
include_role:
name: theforeman.foreman.content_view_promotion
```


Notes
----------------

Not all Lifecycle Environments in a Content-View must be promoted. If you only want to promote a subset (or single), LifeCycle Environment, you just need to update the `foreman_lifecycle_envrionments` variable with the Lifecycle Environments you wish to be affected. This may be helpful in an environment where you only want to promote one environment at a time to help minimize potential risk.

When there isn't a new version of the Content-View to promote to, a new version of the Content-View will be published automatically. Be aware that publishing new versions on a large Content-View can take a long time- you may need to adjust your Ansible job timeouts accordingly.
5 changes: 5 additions & 0 deletions roles/content_view_promote/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
# defaults file for content_view_promote
publish_only: false
rollback: false
FOREMAN_VALIDATE_CERTS: true
21 changes: 21 additions & 0 deletions roles/content_view_promote/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
galaxy_info:
author: Brandon Marlow
description: A role for promoting content views
company: Red Hat
license: BSD-3-Clause

min_ansible_version: "2.9"

galaxy_tags:
- satellite
- katello
- foreman
- theforeman
- update
- content-view
- lifecycle
- patch
- patching

collections:
- theforeman.foreman
103 changes: 103 additions & 0 deletions roles/content_view_promote/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---
# get data on the current content-view
- name: "Gather Data For Current Content-View From Foreman"
theforeman.foreman.resource_info:
username: "{{ foreman_user }}"
password: "{{ foreman_password }}"
server_url: "{{ foreman_server_url }}"
organization: "{{ foreman_organization }}"
validate_certs: "{{ foreman_validate_certs }}"
resource: content_views
search: name = "{{ foreman_content_view }}"
register: content_view_data

# get data on the current content-view version
- name: "Gather Data For Current Content-View Versions From Foreman"
theforeman.foreman.resource_info:
username: "{{ foreman_user }}"
password: "{{ foreman_password }}"
server_url: "{{ foreman_server_url }}"
organization: "{{ foreman_organization }}"
validate_certs: "{{ foreman_validate_certs }}"
resource: content_view_versions
params:
content_view_id: "{{ content_view_data.resources[0].id }}"
register: version_information

# creates a dictionary with data formatted as such {'Prod':'11.0'}
- name: "Build Dictionary With Lifecycle Envrionment And Version Number"
set_fact:
environments: "{{ environments | default({}) | combine ({item[1].name : item[0].name.split()[-1]}) }}"
with_subelements:
- "{{ version_information.resources }}"
- environments

# create list of content-view versions
- name: "Build List of All Versions of Content-View"
set_fact:
cv_versions: "{{ cv_versions | default([]) + [item.major] }}"
with_items: "{{ version_information.resources }}"

# set the highest version to zero so that we don't use previous Content-View settings
- name: "Set Highest Version to 0"
set_fact:
highest_version: 0

# set highest number
- name: "Set the Highest Version of the Content-View Currently Available"
set_fact:
highest_version: "{{ cv_versions | max }}"

# add one to each of the version numbers
- name: "Update Facts With Incremented Content-View Version Numbers"
set_fact:
new_environments: "{{ new_environments | default({}) | combine ({item.key: item.value|int + 1.0 }) }} "
with_dict: "{{ environments }}"

- name: "Check if publishing new version of Content-View is necessary"
set_fact:
publish_true: true
with_dict: "{{ new_environments }}"
when: item.value | int > highest_version | int

- name: Run Content-View Publish
block:
# only publish new view if necessary (when the current view is at or above library)
- name: "Publish new version of Content-View"
theforeman.foreman.content_view_version:
username: "{{ foreman_user }}"
password: "{{ foreman_password }}"
server_url: "{{ foreman_server_url }}"
organization: "{{ foreman_organization }}"
validate_certs: "{{ foreman_validate_certs }}"
content_view: "{{ foreman_content_view }}"
async: 3600 #allow async run for up to 1 hour for large content-views
poll: 0
register: cv_publish

- name: 'Check on status of Content-View Publish'
async_status:
jid: "{{ cv_publish.ansible_job_id }}"
register: job_result
until: job_result.finished
retries: 360
delay: 10
when: publish_true | default(false)

# only promote environments defined in the vars
- name: "Promote Environments to Version N+1"
theforeman.foreman.content_view_version:
username: "{{ foreman_user }}"
password: "{{ foreman_password }}"
server_url: "{{ foreman_server_url }}"
organization: "{{ foreman_organization }}"
validate_certs: "{{ foreman_validate_certs }}"
content_view: "{{ foreman_content_view }}"
# dictionaries aren't ordered and Foreman doesn't want you promoting things out of order
# but we're promoting them all so we just override that behavior
force_promote: true
lifecycle_environments: "{{ item.key }}"
version: "{{ item.value }}"
with_dict: "{{ new_environments }}"
when: item.key in foreman_lifecycle_environments