Skip to content

Commit

Permalink
add variable to set the plugin name to use everywhere
Browse files Browse the repository at this point in the history
updated README.md

Added checks to automatically detect what collection is installed
  • Loading branch information
ivarmu committed Sep 12, 2022
1 parent e8576d2 commit 61cd955
Show file tree
Hide file tree
Showing 42 changed files with 236 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
minor_changes:
- Add a way to detect which of `awx.awx` or `ansible.controller` collection is installed. Added to the playbooks and examples.
...
22 changes: 21 additions & 1 deletion examples/configure_controller.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@

pre_tasks:

- block:
- name: "Check if the collection ansible.controller is installed"
set_fact:
ansible_controller_collection_installed: "{{ lookup('pipe', 'ansible-galaxy collection list | grep -i ansible.controllersss') }}"
rescue:
- name: "Check if the collection awx.awx is installed"
set_fact:
awx_awx_collection_installed: "{{ lookup('pipe', 'ansible-galaxy collection list | grep -i awx.awx') }}"
always:
- name: "Set the collection providing the controller_api lookup plugin"
set_fact:
controller_api_plugin: "{{ ('ansible.controller.controller_api' if ansible_controller_collection_installed is defined) | default('awx.awx.controller_api' if awx_awx_collection_installed is defined) | default('NONE') }}"
- name: "Fail if no collection is detected"
fail:
msg: "One of the following collections is required to be installed: 'ansible.controller' or 'awx.awx'."
when: controller_api_plugin is match('NONE')
- name: "Show the plugin we are using"
debug:
msg: "Using the 'controller_api' plugin from: {{ controller_api_plugin }}"

- name: Wait for Controller to come up
uri:
url: "https://{{ controller_hostname }}/api/v2/ping"
Expand Down Expand Up @@ -95,7 +115,7 @@

- name: Get the organization ID
set_fact:
controller_organization_id: "{{ lookup('awx.awx.controller_api', 'organizations', query_params={ 'name': 'Default' } ,host=controller_hostname, username=controller_username, password=controller_password, verify_ssl=false) }}"
controller_organization_id: "{{ lookup(controller_api_plugin, 'organizations', query_params={ 'name': 'Default' } ,host=controller_hostname, username=controller_username, password=controller_password, verify_ssl=false) }}"

- name: "Set empty lists for testing"
set_fact:
Expand Down
20 changes: 20 additions & 0 deletions examples/configure_controller_export_model.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@
# controller_password: changeme
pre_tasks:

- block:
- name: "Check if the collection ansible.controller is installed"
set_fact:
ansible_controller_collection_installed: "{{ lookup('pipe', 'ansible-galaxy collection list | grep -i ansible.controllersss') }}"
rescue:
- name: "Check if the collection awx.awx is installed"
set_fact:
awx_awx_collection_installed: "{{ lookup('pipe', 'ansible-galaxy collection list | grep -i awx.awx') }}"
always:
- name: "Set the collection providing the controller_api lookup plugin"
set_fact:
controller_api_plugin: "{{ ('ansible.controller.controller_api' if ansible_controller_collection_installed is defined) | default('awx.awx.controller_api' if awx_awx_collection_installed is defined) | default('NONE') }}"
- name: "Fail if no collection is detected"
fail:
msg: "One of the following collections is required to be installed: 'ansible.controller' or 'awx.awx'."
when: controller_api_plugin is match('NONE')
- name: "Show the plugin we are using"
debug:
msg: "Using the 'controller_api' plugin from: {{ controller_api_plugin }}"

- name: Wait for Controller to come up
uri:
url: "{{ controller_hostname }}/api/v2/ping"
Expand Down
2 changes: 1 addition & 1 deletion examples/tasks/differential.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
- name: "Get the API list of all {{ differential_item.name }} in the Default Organization"
set_fact:
controller_api_results: "{{ lookup('awx.awx.controller_api', differential_item.name, query_params={ 'organization': controller_organization_id.id } ,host=controller_hostname, username=controller_username, password=controller_password, verify_ssl=false) }}"
controller_api_results: "{{ lookup(controller_api_plugin, differential_item.name, query_params={ 'organization': controller_organization_id.id } ,host=controller_hostname, username=controller_username, password=controller_password, verify_ssl=false) }}"

- name: "Find the difference of {{ differential_item.name }} between what is on the Controller versus curated list."
set_fact:
Expand Down
20 changes: 20 additions & 0 deletions playbooks/configure_awx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@
- redhat_cop.controller_configuration
pre_tasks:

- block:
- name: "Check if the collection ansible.controller is installed"
set_fact:
ansible_controller_collection_installed: "{{ lookup('pipe', 'ansible-galaxy collection list | grep -i ansible.controllersss') }}"
rescue:
- name: "Check if the collection awx.awx is installed"
set_fact:
awx_awx_collection_installed: "{{ lookup('pipe', 'ansible-galaxy collection list | grep -i awx.awx') }}"
always:
- name: "Set the collection providing the controller_api lookup plugin"
set_fact:
controller_api_plugin: "{{ ('ansible.controller.controller_api' if ansible_controller_collection_installed is defined) | default('awx.awx.controller_api' if awx_awx_collection_installed is defined) | default('NONE') }}"
- name: "Fail if no collection is detected"
fail:
msg: "One of the following collections is required to be installed: 'ansible.controller' or 'awx.awx'."
when: controller_api_plugin is match('NONE')
- name: "Show the plugin we are using"
debug:
msg: "Using the 'controller_api' plugin from: {{ controller_api_plugin }}"

- name: Include vars from configs directory
include_vars:
dir: "{{ controller_configs_dir | default((lookup('env','CONTROLLER_CONFIGS_DIR') == '') | ternary('./configs', lookup('env','CONTROLLER_CONFIGS_DIR'))) }}"
Expand Down
20 changes: 20 additions & 0 deletions playbooks/configure_controller.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@
- redhat_cop.controller_configuration
pre_tasks:

- block:
- name: "Check if the collection ansible.controller is installed"
set_fact:
ansible_controller_collection_installed: "{{ lookup('pipe', 'ansible-galaxy collection list | grep -i ansible.controllersss') }}"
rescue:
- name: "Check if the collection awx.awx is installed"
set_fact:
awx_awx_collection_installed: "{{ lookup('pipe', 'ansible-galaxy collection list | grep -i awx.awx') }}"
always:
- name: "Set the collection providing the controller_api lookup plugin"
set_fact:
controller_api_plugin: "{{ ('ansible.controller.controller_api' if ansible_controller_collection_installed is defined) | default('awx.awx.controller_api' if awx_awx_collection_installed is defined) | default('NONE') }}"
- name: "Fail if no collection is detected"
fail:
msg: "One of the following collections is required to be installed: 'ansible.controller' or 'awx.awx'."
when: controller_api_plugin is match('NONE')
- name: "Show the plugin we are using"
debug:
msg: "Using the 'controller_api' plugin from: {{ controller_api_plugin }}"

- name: Include vars from configs directory
include_vars:
dir: "{{ controller_configs_dir | default((lookup('env','CONTROLLER_CONFIGS_DIR') == '') | ternary('./configs', lookup('env','CONTROLLER_CONFIGS_DIR'))) }}"
Expand Down
33 changes: 30 additions & 3 deletions plugins/lookup/controller_object_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,47 @@
"""

EXAMPLES = """
# Set the controller_api_plugin manually:
- name: Set the name for the collection to provide the plugin controller_api
set_fact:
controller_api_plugin: "awx.awx.controller_api"
# Set the controller_api_plugin automatically:
- block:
- name: "Check if the collection ansible.controller is installed"
set_fact:
ansible_controller_collection_installed: "{{ lookup('pipe', 'ansible-galaxy collection list | grep -i ansible.controllersss') }}"
rescue:
- name: "Check if the collection awx.awx is installed"
set_fact:
awx_awx_collection_installed: "{{ lookup('pipe', 'ansible-galaxy collection list | grep -i awx.awx') }}"
always:
- name: "Set the collection providing the controller_api lookup plugin"
set_fact:
controller_api_plugin: "{{ ('ansible.controller.controller_api' if ansible_controller_collection_installed is defined) | default('awx.awx.controller_api' if awx_awx_collection_installed is defined) | default('NONE') }}"
- name: "Fail if no collection is detected"
fail:
msg: "One of the following collections is required to be installed: 'ansible.controller' or 'awx.awx'."
when: controller_api_plugin is match('NONE')
- name: "Show the plugin we are using"
debug:
msg: "Using the 'controller_api' plugin from: {{ controller_api_plugin }}"
# Examples for the lookup plugin
- name: Get the organization ID
set_fact:
controller_organization_id: "{{ lookup('awx.awx.controller_api', 'organizations', query_params={ 'name': 'Default' },
controller_organization_id: "{{ lookup(controller_api_plugin, 'organizations', query_params={ 'name': 'Default' },
host=controller_hostname, username=controller_username, password=controller_password, verify_ssl=false) }}"
- name: "Get the API list of all Projects in the Default Organization"
set_fact:
controller_api_results: "{{ lookup('awx.awx.controller_api', 'projects', query_params={ 'organization':
controller_api_results: "{{ lookup(controller_api_plugin, 'projects', query_params={ 'organization':
controller_organization_id.id } ,host=controller_hostname, username=controller_username,
password=controller_password, verify_ssl=false) }}"
- name: "Get the API in a list form. Useful for making sure the results of one item is set to a list.
set_fact:
controller_api_results: "{{ query('awx.awx.controller_api', 'inventories', query_params={ 'organization':
controller_api_results: "{{ query(controller_api_plugin, 'inventories', query_params={ 'organization':
controller_organization_id.id } ,host=controller_hostname, username=controller_username,
password=controller_password, verify_ssl=false) }}"
Expand Down
5 changes: 4 additions & 1 deletion roles/filetree_create/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ Role Variables

The following variables are required for that role to work properly:

- **`output_path`**: The path to the output directory where all the generated `yaml` files with the corresponding Objects as code will be written to. The default path is `/tmp/filetree_output`.
| Variable Name | Default Value | Required | Description |
| :------------ | :-----------: | :------: | :---------- |
| **`controller_api_plugin`**: | N/A | yes | Full path for the controller_api_plugin to be used. <br/> Can have two possible values: <br/>&nbsp;&nbsp;- awx.awx.controller_api # For the community Collection version <br/>&nbsp;&nbsp;- ansible.controller.controller_api # For the Red Hat Certified Collection version|
| **`output_path:`** | N/A | yes | The path to the output directory where all the generated `yaml` files with the corresponding Objects as code will be written to. The default path is `/tmp/filetree_output`. |

Dependencies
------------
Expand Down
2 changes: 1 addition & 1 deletion roles/filetree_create/tasks/all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

- name: "Check if the connection is to an Ansible Tower or to Automation Platform"
set_fact:
is_aap: "{{ lookup('ansible.controller.controller_api', 'ping', host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs).version is version('4.0.0', '>=') }}"
is_aap: "{{ lookup(controller_api_plugin, 'ping', host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs).version is version('4.0.0', '>=') }}"

- block:
- name: "Export Inventories and related Groups and Hosts"
Expand Down
4 changes: 2 additions & 2 deletions roles/filetree_create/tasks/credential_types.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
- name: "Get current Credential Types from the API when AAP"
set_fact:
credential_types_lookvar: "{{ query('ansible.controller.controller_api', 'api/v2/credential_types/', query_params={ 'managed': false }, host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs, return_all=true, max_objects=query_controller_api_max_objects) }}"
credential_types_lookvar: "{{ query(controller_api_plugin, 'api/v2/credential_types/', query_params={ 'managed': false }, host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs, return_all=true, max_objects=query_controller_api_max_objects) }}"
when: is_aap

- name: "Get current Credential Types from the API when Tower"
set_fact:
credential_types_lookvar: "{{ query('ansible.controller.controller_api', 'api/v2/credential_types/', query_params={ 'managed_by_tower': false }, host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs, return_all=true, max_objects=query_controller_api_max_objects) }}"
credential_types_lookvar: "{{ query(controller_api_plugin, 'api/v2/credential_types/', query_params={ 'managed_by_tower': false }, host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs, return_all=true, max_objects=query_controller_api_max_objects) }}"
when: not is_aap

- name: "Create the output directory for credential types: {{ output_path }}"
Expand Down
2 changes: 1 addition & 1 deletion roles/filetree_create/tasks/credentials.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
- name: "Get current Credentials from the API"
set_fact:
credentials_lookvar: "{{ query('ansible.controller.controller_api', 'api/v2/credentials/', query_params={'order_by': 'organization'}, host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs, return_all=true, max_objects=query_controller_api_max_objects) }}"
credentials_lookvar: "{{ query(controller_api_plugin, 'api/v2/credentials/', query_params={'order_by': 'organization'}, host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs, return_all=true, max_objects=query_controller_api_max_objects) }}"

- name: "Group the credentials by Organization"
set_fact:
Expand Down
2 changes: 1 addition & 1 deletion roles/filetree_create/tasks/execution_environments.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
- name: "Get current Execution Environments from the API"
set_fact:
execution_environments_lookvar: "{{ query('ansible.controller.controller_api', 'api/v2/execution_environments/', host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs, return_all=true, max_objects=query_controller_api_max_objects) }}"
execution_environments_lookvar: "{{ query(controller_api_plugin, 'api/v2/execution_environments/', host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs, return_all=true, max_objects=query_controller_api_max_objects) }}"

- name: "Create the output directory for execution environments: {{ output_path }}"
file:
Expand Down
10 changes: 5 additions & 5 deletions roles/filetree_create/tasks/inventory.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
- name: "Get the inventoryes from the API"
set_fact:
inventory_lookvar: "{{ query('ansible.controller.controller_api', 'api/v2/inventories/', host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs, return_all=true, max_objects=query_controller_api_max_objects, query_params={
inventory_lookvar: "{{ query(controller_api_plugin, 'api/v2/inventories/', host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs, return_all=true, max_objects=query_controller_api_max_objects, query_params={
'not__kind': 'smart',
'order_by': 'organization'
}
Expand All @@ -19,13 +19,13 @@
'host_filter': inventory_lookvar_item.host_filter,
'kind': inventory_lookvar_item.kind,
'variables': inventory_lookvar_item.variables,
'inventory_sources': query('ansible.controller.controller_api', inventory_lookvar_item.related.inventory_sources, host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs, return_all=true, max_objects=query_controller_api_max_objects) if inventory_lookvar_item.has_inventory_sources else [],
'hosts': query('ansible.controller.controller_api', inventory_lookvar_item.related.hosts, host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs, return_all=true, max_objects=query_controller_api_max_objects) if not inventory_lookvar_item.has_inventory_sources else [],
'groups': query('ansible.controller.controller_api', inventory_lookvar_item.related.groups, host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs, return_all=true, max_objects=query_controller_api_max_objects) if not inventory_lookvar_item.has_inventory_sources else []
'inventory_sources': query(controller_api_plugin, inventory_lookvar_item.related.inventory_sources, host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs, return_all=true, max_objects=query_controller_api_max_objects) if inventory_lookvar_item.has_inventory_sources else [],
'hosts': query(controller_api_plugin, inventory_lookvar_item.related.hosts, host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs, return_all=true, max_objects=query_controller_api_max_objects) if not inventory_lookvar_item.has_inventory_sources else [],
'groups': query(controller_api_plugin, inventory_lookvar_item.related.groups, host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs, return_all=true, max_objects=query_controller_api_max_objects) if not inventory_lookvar_item.has_inventory_sources else []
}]}) }}"
needed_paths: "{{ ((needed_paths | default([])) + [inventory_lookvar_item_organization]) | flatten | unique }}"
vars:
inventory_lookvar_item_organization: "{{ lookup('ansible.controller.controller_api', 'api/v2/organizations/'+(inventory_lookvar_item.organization|string), host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs, return_all=true)['name'] }}"
inventory_lookvar_item_organization: "{{ lookup(controller_api_plugin, 'api/v2/organizations/'+(inventory_lookvar_item.organization|string), host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs, return_all=true)['name'] }}"
loop: "{{ inventory_lookvar }}"
loop_control:
loop_var: inventory_lookvar_item
Expand Down
2 changes: 1 addition & 1 deletion roles/filetree_create/tasks/job_templates.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
- name: "Get current Job Templates from the API"
set_fact:
job_templates_lookvar: "{{ query('ansible.controller.controller_api', 'api/v2/job_templates/', query_params={'order_by': 'organization'}, host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs, return_all=true, max_objects=query_controller_api_max_objects) }}"
job_templates_lookvar: "{{ query(controller_api_plugin, 'api/v2/job_templates/', query_params={'order_by': 'organization'}, host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs, return_all=true, max_objects=query_controller_api_max_objects) }}"

- name: "Group the job_templates by Organization"
set_fact:
Expand Down
Loading

0 comments on commit 61cd955

Please sign in to comment.