Skip to content

Commit

Permalink
Adding support for a new variable named "galaxy_sources" (#172)
Browse files Browse the repository at this point in the history
* Adding support for galaxy_sources

* Adding support for galaxy_sources

* Adding support for galaxy_sources

* Adding support for galaxy_sources

* Updated README to include 'galaxy_sources'

* Updated test for pre/post
  • Loading branch information
oybed authored Jun 20, 2022
1 parent c63c4ed commit 5fee5e3
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
7 changes: 6 additions & 1 deletion roles/openshift-applier/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ The variable definitions come in the form of an object, `openshift_cluster_conte
openshift_cluster_content:
- galaxy_requirements: # Optional: only needed if pre/post steps are specified below
- "path/to/galaxy/requirements.yml" # Has to be a local file - e.g: with the inventory
- galaxy_sources: # Optional: only needed if pre/post steps are specified below
- src: https://github.com/....
version: v1.0.0
- src: https://github.com/....
version: main
- object: <object_type>
pre_steps: # Optional: pre-steps at object level can be added if desired
- role: <path to an ansible role>
Expand Down Expand Up @@ -346,7 +351,7 @@ The `openshift-applier` supports the use of pre and post steps to allow for task

The pre/post steps can be added at both the `object` level as well as the `content level`. See example at the top for more details.

In essence, the pre/post steps are ansible roles that gets executed in the order they are found in the inventory. These roles are sourced from the `galaxy_requirements` file part of the inventory. See the official [Ansible Galaxy docs for more details on the requirements yaml file](http://docs.ansible.com/ansible/latest/galaxy.html#installing-multiple-roles-from-a-file).
In essence, the pre/post steps are ansible roles that gets executed in the order they are found in the inventory. These roles are sourced from the `galaxy_sources` list, or `galaxy_requirements` file, part of the inventory. See the official [Ansible Galaxy docs for more details on the requirements yaml file](http://docs.ansible.com/ansible/latest/galaxy.html#installing-multiple-roles-from-a-file).

**_NOTE:_** it is important that the repos used for pre/post roles have the `meta/main.yml` file setup correctly. See the [Ansible Galaxy docs](http://docs.ansible.com/ansible/latest/galaxy.html) for more details.

Expand Down
31 changes: 30 additions & 1 deletion roles/openshift-applier/tasks/install-dependencies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,37 @@
when:
- tmp_dep_dir is undefined

- set_fact:
galaxy_requirements: "{{ dependencies.galaxy_requirements | default([]) }}"

- name: "Compose galaxy_requirements file if galaxy_sources is set"
block:
- tempfile:
state: file
suffix: .yml
register: tmp_galaxy_file
- template:
src: yaml_file.j2
dest: "{{ tmp_galaxy_file.path }}"
vars:
yaml_content: "{{ dependencies.galaxy_sources }}"
- set_fact:
galaxy_requirements: "{{ galaxy_requirements + [ tmp_galaxy_file.path ] }}"
when:
- dependencies.galaxy_sources is defined
- dependencies.galaxy_sources|length > 0

- name: "Run ansible-galaxy to pull in dependency roles"
command: >
ansible-galaxy install -r "{{ item }}" -p "{{ tmp_dep_dir }}"
with_items:
- "{{ dependencies.galaxy_requirements }}"
- "{{ galaxy_requirements }}"

- name: "Clean-up temporary file from above - if needed"
file:
path: "{{ tmp_galaxy_file.path }}"
state: absent
ignore_errors: True
when:
- tmp_galaxy_file is defined
- tmp_galaxy_file.path|trim != ''
2 changes: 1 addition & 1 deletion roles/openshift-applier/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
loop_control:
loop_var: dependencies
when:
- dependencies.galaxy_requirements is defined
- (dependencies.galaxy_requirements is defined or dependencies.galaxy_sources is defined)

- name: "Prepare to copy content to remote host(s) if not running 'locally'"
import_tasks: prep-copy-inventory-to-remote.yml
Expand Down
4 changes: 4 additions & 0 deletions roles/openshift-applier/templates/yaml_file.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---

{{ yaml_content | to_nice_yaml }}

3 changes: 3 additions & 0 deletions tests/inventories/pre-post-steps/group_vars/seed-hosts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ molecule_test_inventory_skip: true

openshift_cluster_content:
- galaxy_requirements: "{{ inventory_dir }}/../../files/dependency/galaxy_requirements.yml"
- galaxy_sources: # Duplicate of above, just to show an alternative approach
- src: https://github.com/redhat-cop/casl-ansible
version: master
- object: projectrequest
content:
- name: "label-test-project"
Expand Down

0 comments on commit 5fee5e3

Please sign in to comment.