Initial commit for new inventory sync module

This commit is contained in:
beeankha 2020-08-27 17:28:31 -04:00
parent cce66e366f
commit 72dbd10c2a
3 changed files with 183 additions and 3 deletions

View File

@ -0,0 +1,100 @@
#!/usr/bin/python
# coding: utf-8 -*-
# (c) 2020, Bianca Henderson <bianca@redhat.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}
DOCUMENTATION = '''
---
module: tower_inventory_source_update
author: "Bianca Henderson (@beeankha)"
short_description: Update inventory source(s).
description:
- Update Ansible Tower inventory source(s). See
U(https://www.ansible.com/tower) for an overview.
options:
inventory:
description:
- Name of the inventory that contains the inventory source(s) to update.
required: True
type: str
inventory_source:
description:
- The name of the inventory source to update.
required: True
type: str
extends_documentation_fragment: awx.awx.auth
'''
EXAMPLES = '''
- name: Update a single inventory source
tower_inventory_source_update:
inventory: "My Inventory"
inventory_source: "Example Inventory Source"
- name: Update all inventory sources
tower_inventory_source_update:
inventory: "My Other Inventory"
inventory_source: "{{ item }}"
loop: "{{ query('awx.awx.tower_api', 'inventory_sources', query_params={ 'inventory': 30 }, return_ids=True ) }}"
'''
RETURN = '''
id:
description: id of the inventory update
returned: success
type: int
sample: 86
status:
description: status of the inventory update
returned: success
type: str
sample: pending
'''
from ..module_utils.tower_api import TowerAPIModule
def main():
# Any additional arguments that are not fields of the item can be added here
argument_spec = dict(
inventory=dict(required=True),
inventory_source=dict(required=True),
)
# Create a module for ourselves
module = TowerAPIModule(argument_spec=argument_spec)
# Extract our parameters
inventory = module.params.get('inventory')
inventory_source = module.params.get('inventory_source')
# Attempt to look up the inventory the user specified (these will fail the module if not found)
inventory_object = module.get_one_by_name_or_id('inventories', inventory)
# Return all inventory sources related to the specified inventory
inventory_source_object = module.get_one_by_name_or_id(inventory_object['related']['inventory_sources'], inventory_source)
# Sync the inventory source(s)
inventory_source_update_results = module.post_endpoint(inventory_source_object['related']['update'], **{'data': {}})
if inventory_source_update_results['status_code'] != 202:
module.fail_json(msg="Failed to update inventory source, see response for details", **{'response': inventory_source_update_results})
module.exit_json(**{
'changed': True,
'id': inventory_source_update_results['json']['id'],
'status': inventory_source_update_results['json']['status'],
})
if __name__ == '__main__':
main()

View File

@ -23,9 +23,9 @@ no_module_for_endpoint = []
# Some modules work on the related fields of an endpoint. These modules will not have an auto-associated endpoint
no_endpoint_for_module = [
'tower_import', 'tower_meta', 'tower_export', 'tower_job_launch', 'tower_job_wait', 'tower_job_list',
'tower_license', 'tower_ping', 'tower_receive', 'tower_send', 'tower_workflow_launch', 'tower_job_cancel',
'tower_workflow_template',
'tower_import', 'tower_meta', 'tower_export', 'tower_inventory_source_update', 'tower_job_launch', 'tower_job_wait',
'tower_job_list', 'tower_license', 'tower_ping', 'tower_receive', 'tower_send', 'tower_workflow_launch',
'tower_job_cancel', 'tower_workflow_template',
]
# Global module parameters we can ignore

View File

@ -0,0 +1,80 @@
---
- name: Generate a test ID
set_fact:
test_id: "{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}"
- name: Generate names
set_fact:
project_name: "AWX-Collection-tests-tower_inventory_source_update-project-{{ test_id }}"
inv_name: "AWX-Collection-tests-tower_inventory_source_update-inv-{{ test_id }}"
inv_source1: "AWX-Collection-tests-tower_inventory_source_update-source1-{{ test_id }}"
inv_source2: "AWX-Collection-tests-tower_inventory_source_update-source2-{{ test_id }}"
- block:
- name: Create a git project without credentials
tower_project:
name: "{{ project_name }}"
organization: Default
scm_type: git
scm_url: https://github.com/ansible/test-playbooks
wait: true
- name: Create an Inventory
tower_inventory:
name: "{{ inv_name }}"
organization: Default
state: present
register: created_inventory
- name: Create an Inventory Source
tower_inventory_source:
name: "{{ inv_source1 }}"
source: scm
source_project: "{{ project_name }}"
source_path: inventories/inventory.ini
description: Source for Test inventory
inventory: "{{ inv_name }}"
- name: Create Another Inventory Source
tower_inventory_source:
name: "{{ inv_source2 }}"
source: scm
source_project: "{{ project_name }}"
source_path: inventories/create_10_hosts.ini
description: Source for Test inventory
inventory: "{{ inv_name }}"
- name: Test Inventory Source Update
tower_inventory_source_update:
inventory: "{{ inv_name }}"
inventory_source: "{{ inv_source1 }}"
register: result
- assert:
that:
- "result is changed"
- name: Test Inventory Source Update for All Sources
tower_inventory_source_update:
inventory: "{{ inv_name }}"
inventory_source: "{{ item }}"
loop: "{{ query('awx.awx.tower_api', 'inventory_sources', query_params={ 'inventory': created_inventory.id }, return_ids=True ) }}"
register: result
- assert:
that:
- "result is changed"
always:
- name: Delete Inventory
tower_inventory:
name: "{{ inv_name }}"
organization: Default
state: absent
- name: Delete Project
tower_project:
name: "{{ project_name }}"
organization: Default
state: absent