make lookup plugin compatible with ansible.tower namespace (#9)

This commit is contained in:
Alan Rominger
2020-07-02 12:11:16 -04:00
committed by John Westcott IV
parent 8239232d4d
commit 34c23caed3
5 changed files with 131 additions and 47 deletions

View File

@@ -6,36 +6,35 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
DOCUMENTATION = ''' DOCUMENTATION = '''
name: tower name: tower
plugin_type: inventory plugin_type: inventory
author: author:
- Matthew Jones (@matburt) - Matthew Jones (@matburt)
- Yunfan Zhang (@YunfanZhang42) - Yunfan Zhang (@YunfanZhang42)
short_description: Ansible dynamic inventory plugin for Ansible Tower. short_description: Ansible dynamic inventory plugin for Ansible Tower.
description: description:
- Reads inventories from Ansible Tower. - Reads inventories from Ansible Tower.
- Supports reading configuration from both YAML config file and environment variables. - Supports reading configuration from both YAML config file and environment variables.
- If reading from the YAML file, the file name must end with tower.(yml|yaml) or tower_inventory.(yml|yaml), - If reading from the YAML file, the file name must end with tower.(yml|yaml) or tower_inventory.(yml|yaml),
the path in the command would be /path/to/tower_inventory.(yml|yaml). If some arguments in the config file the path in the command would be /path/to/tower_inventory.(yml|yaml). If some arguments in the config file
are missing, this plugin will try to fill in missing arguments by reading from environment variables. are missing, this plugin will try to fill in missing arguments by reading from environment variables.
- If reading configurations from environment variables, the path in the command must be @tower_inventory. - If reading configurations from environment variables, the path in the command must be @tower_inventory.
extends_documentation_fragment: extends_documentation_fragment: awx.awx.auth_plugin
- awx.awx.auth_plugin options:
options: inventory_id:
inventory_id: description:
description: - The ID of the Ansible Tower inventory that you wish to import.
- The ID of the Ansible Tower inventory that you wish to import. - This is allowed to be either the inventory primary key or its named URL slug.
- This is allowed to be either the inventory primary key or its named URL slug. - Primary key values will be accepted as strings or integers, and URL slugs must be strings.
- Primary key values will be accepted as strings or integers, and URL slugs must be strings. - Named URL slugs follow the syntax of "inventory_name++organization_name".
- Named URL slugs follow the syntax of "inventory_name++organization_name". type: raw
type: raw env:
env: - name: TOWER_INVENTORY
- name: TOWER_INVENTORY required: True
required: True include_metadata:
include_metadata: description: Make extra requests to provide all group vars with metadata about the source Ansible Tower host.
description: Make extra requests to provide all group vars with metadata about the source Ansible Tower host. type: bool
type: bool default: False
default: False
''' '''
EXAMPLES = ''' EXAMPLES = '''

View File

@@ -12,8 +12,9 @@ requirements:
description: description:
- Returns GET requests from the Ansible Tower API. See - Returns GET requests from the Ansible Tower API. See
U(https://docs.ansible.com/ansible-tower/latest/html/towerapi/index.html) for API usage. U(https://docs.ansible.com/ansible-tower/latest/html/towerapi/index.html) for API usage.
extends_documentation_fragment: - For use that is cross-compatible between the awx.awx and ansible.tower collection
- awx.awx.auth_plugin see the tower_meta module
extends_documentation_fragment: awx.awx.auth_plugin
options: options:
_terms: _terms:
description: description:

View File

@@ -0,0 +1,78 @@
#!/usr/bin/python
# coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}
DOCUMENTATION = '''
---
module: tower_meta
author: "Alan Rominger (@alancoding)"
short_description: Returns metadata about the collection this module lives in.
description:
- Allows a user to find out what collection this module exists in.
- This takes common module parameters, but does nothing with them.
options: {}
extends_documentation_fragment: awx.awx.auth
'''
RETURN = '''
prefix:
description: Collection namespace and name in the namespace.name format
returned: success
sample: awx.awx
name:
description: Collection name
returned: success
sample: awx
namespace:
description: Collection namespace
returned: success
sample: awx
version:
description: Version of the collection
returned: success
sample: 0.0.1-devel
'''
EXAMPLES = '''
- tower_meta:
register: result
- name: Show details about the collection
debug: var=result
- name: Load the UI setting without hard-coding the collection name
debug:
msg: "{{ lookup(result.prefix + '.tower_api', 'settings/ui') }}"
'''
from ..module_utils.tower_api import TowerModule
def main():
module = TowerModule(argument_spec={})
namespace = {
'awx': 'awx',
'tower': 'ansible'
}.get(module._COLLECTION_TYPE, 'unknown')
namespace_name = '{0}.{1}'.format(namespace, module._COLLECTION_TYPE)
module.exit_json(
prefix=namespace_name,
name=module._COLLECTION_TYPE,
namespace=namespace,
version=module._COLLECTION_VERSION
)
if __name__ == '__main__':
main()

View File

@@ -19,10 +19,13 @@
loop: "{{ usernames }}" loop: "{{ usernames }}"
register: user_creation_results register: user_creation_results
- tower_meta:
register: tower_meta
- block: - block:
- name: Test too many params (failure from validation of terms) - name: Test too many params (failure from validation of terms)
set_fact: set_fact:
junk: "{{ query('awx.awx.tower_api', 'users', 'teams', query_params={}, ) }}" junk: "{{ query(tower_meta.prefix + '.tower_api', 'users', 'teams', query_params={}, ) }}"
ignore_errors: true ignore_errors: true
register: result register: result
@@ -33,7 +36,7 @@
- name: Try to load invalid endpoint - name: Try to load invalid endpoint
set_fact: set_fact:
junk: "{{ query('awx.awx.tower_api', 'john', query_params={}, ) }}" junk: "{{ query(tower_meta.prefix + '.tower_api', 'john', query_params={}, ) }}"
ignore_errors: true ignore_errors: true
register: result register: result
@@ -44,7 +47,7 @@
- name: Load user of a specific name without promoting objects - name: Load user of a specific name without promoting objects
set_fact: set_fact:
users: "{{ lookup('awx.awx.tower_api', 'users', query_params={ 'username' : user_creation_results['results'][0]['item'] }, return_objects=False) }}" users: "{{ lookup(tower_meta.prefix + '.tower_api', 'users', query_params={ 'username' : user_creation_results['results'][0]['item'] }, return_objects=False) }}"
- assert: - assert:
that: that:
@@ -56,13 +59,13 @@
assert: assert:
that: that:
- item['id'] == user_creation_results['results'][0]['id'] - item['id'] == user_creation_results['results'][0]['id']
loop: "{{ query('awx.awx.tower_api', 'users', query_params={ 'username' : user_creation_results['results'][0]['item'] }) }}" loop: "{{ query(tower_meta.prefix + '.tower_api', 'users', query_params={ 'username' : user_creation_results['results'][0]['item'] }) }}"
loop_control: loop_control:
label: "{{ item['id'] }}" label: "{{ item['id'] }}"
- name: Get a page of users as just ids - name: Get a page of users as just ids
set_fact: set_fact:
users: "{{ query('awx.awx.tower_api', 'users', query_params={ 'username__endswith': test_id, 'page_size': 2 }, return_ids=True ) }}" users: "{{ query(tower_meta.prefix + '.tower_api', 'users', query_params={ 'username__endswith': test_id, 'page_size': 2 }, return_ids=True ) }}"
- name: Assert that user list has 2 integer ids only - name: Assert that user list has 2 integer ids only
assert: assert:
@@ -72,7 +75,7 @@
- name: Get all users of a system through next attribute - name: Get all users of a system through next attribute
set_fact: set_fact:
users: "{{ query('awx.awx.tower_api', 'users', query_params={ 'username__endswith': test_id, 'page_size': 1 }, return_all=true ) }}" users: "{{ query(tower_meta.prefix + '.tower_api', 'users', query_params={ 'username__endswith': test_id, 'page_size': 1 }, return_all=true ) }}"
- assert: - assert:
that: that:
@@ -80,11 +83,11 @@
- name: Get the ID of the first user created and verify that it is correct - name: Get the ID of the first user created and verify that it is correct
assert: assert:
that: "{{ query('awx.awx.tower_api', 'users', query_params={ 'username' : user_creation_results['results'][0]['item'] }, return_ids=True) }}[0] == {{ user_creation_results['results'][0]['id'] }}" that: "{{ query(tower_meta.prefix + '.tower_api', 'users', query_params={ 'username' : user_creation_results['results'][0]['item'] }, return_ids=True) }}[0] == {{ user_creation_results['results'][0]['id'] }}"
- name: Try to get an ID of someone who does not exist - name: Try to get an ID of someone who does not exist
set_fact: set_fact:
failed_user_id: "{{ query('awx.awx.tower_api', 'users', query_params={ 'username': 'john jacob jingleheimer schmidt' }, expect_one=True) }}" failed_user_id: "{{ query(tower_meta.prefix + '.tower_api', 'users', query_params={ 'username': 'john jacob jingleheimer schmidt' }, expect_one=True) }}"
register: result register: result
ignore_errors: true ignore_errors: true
@@ -95,7 +98,7 @@
- name: Lookup too many users - name: Lookup too many users
set_fact: set_fact:
too_many_user_ids: " {{ query('awx.awx.tower_api', 'users', query_params={ 'username__endswith': test_id }, expect_one=True) }}" too_many_user_ids: " {{ query(tower_meta.prefix + '.tower_api', 'users', query_params={ 'username__endswith': test_id }, expect_one=True) }}"
register: results register: results
ignore_errors: true ignore_errors: true
@@ -107,11 +110,11 @@
- name: Get the settings page - name: Get the settings page
assert: assert:
that: that:
- "'CUSTOM_LOGO' in {{ lookup('awx.awx.tower_api', 'settings/ui' ) }}" - "'CUSTOM_LOGO' in {{ lookup(tower_meta.prefix + '.tower_api', 'settings/ui' ) }}"
- name: Get the ping page - name: Get the ping page
set_fact: set_fact:
ping_data: "{{ lookup('awx.awx.tower_api', 'ping' ) }}" ping_data: "{{ lookup(tower_meta.prefix + '.tower_api', 'ping' ) }}"
register: results register: results
- assert: - assert:

View File

@@ -11,7 +11,7 @@
replace: replace:
path: "{{ collection_path }}/plugins/module_utils/tower_api.py" path: "{{ collection_path }}/plugins/module_utils/tower_api.py"
regexp: '^ _COLLECTION_TYPE = "awx"' regexp: '^ _COLLECTION_TYPE = "awx"'
replace: ' _COLLECTION_TYPE = "{{ collection_namespace }}"' replace: ' _COLLECTION_TYPE = "{{ collection_package }}"'
- name: Do file content replacements for non-default namespace or package name - name: Do file content replacements for non-default namespace or package name
block: block:
@@ -19,9 +19,12 @@
- name: Change module doc_fragments to support desired namespace and package names - name: Change module doc_fragments to support desired namespace and package names
replace: replace:
path: "{{ item }}" path: "{{ item }}"
regexp: '^extends_documentation_fragment: awx.awx.auth' regexp: '^extends_documentation_fragment: awx.awx.auth([a-zA-Z0-9_]*)$'
replace: 'extends_documentation_fragment: {{ collection_namespace }}.{{ collection_package }}.auth' replace: 'extends_documentation_fragment: {{ collection_namespace }}.{{ collection_package }}.auth\1'
with_fileglob: "{{ collection_path }}/plugins/modules/tower_*.py" with_fileglob:
- "{{ collection_path }}/plugins/inventory/*.py"
- "{{ collection_path }}/plugins/lookup/*.py"
- "{{ collection_path }}/plugins/modules/tower_*.py"
loop_control: loop_control:
label: "{{ item | basename }}" label: "{{ item | basename }}"