Resolve id inventory and organization (#14)

This commit is contained in:
Seth Foster
2023-02-28 21:59:05 -05:00
committed by Elijah DeLee
parent f622d3a1e6
commit 4b9ca3deee
5 changed files with 33 additions and 20 deletions

View File

@@ -48,9 +48,9 @@ options:
type: str type: str
inventory: inventory:
description: description:
- Inventory ID the hosts should be made a member of. - Inventory name or ID the hosts should be made a member of.
required: True required: True
type: int type: str
extends_documentation_fragment: awx.awx.auth extends_documentation_fragment: awx.awx.auth
''' '''
@@ -71,21 +71,24 @@ def main():
# Any additional arguments that are not fields of the item can be added here # Any additional arguments that are not fields of the item can be added here
argument_spec = dict( argument_spec = dict(
hosts=dict(required=True, type='list', elements='dict'), hosts=dict(required=True, type='list', elements='dict'),
inventory=dict(required=True, type='int'), inventory=dict(required=True, type='str'),
) )
# Create a module for ourselves # Create a module for ourselves
module = ControllerAPIModule(argument_spec=argument_spec) module = ControllerAPIModule(argument_spec=argument_spec)
# Extract our parameters # Extract our parameters
inventory = module.params.get('inventory') inv_name = module.params.get('inventory')
hosts = module.params.get('hosts') hosts = module.params.get('hosts')
for h in hosts: for h in hosts:
if 'variables' in h: if 'variables' in h:
h['variables'] = json.dumps(h['variables']) h['variables'] = json.dumps(h['variables'])
inv_id = module.resolve_name_to_id('inventories', inv_name)
# Launch the jobs # Launch the jobs
result = module.post_endpoint("bulk/host_create", data={"inventory": inventory, "hosts": hosts}) result = module.post_endpoint("bulk/host_create", data={"inventory": inv_id, "hosts": hosts})
if result['status_code'] != 201: if result['status_code'] != 201:
module.fail_json(msg="Failed to create hosts, see response for details", response=result) module.fail_json(msg="Failed to create hosts, see response for details", response=result)

View File

@@ -125,11 +125,11 @@ options:
- If not provided, will use the organization the user is in. - If not provided, will use the organization the user is in.
- Required if the user belongs to more than one organization. - Required if the user belongs to more than one organization.
- Affects who can see the resulting bulk job. - Affects who can see the resulting bulk job.
type: int type: str
inventory: inventory:
description: description:
- Inventory ID to use for the jobs ran within the bulk job, only used if prompt for inventory is set. - Inventory name or ID to use for the jobs ran within the bulk job, only used if prompt for inventory is set.
type: int type: str
scm_branch: scm_branch:
description: description:
- A specific branch of the SCM project to run the template on. - A specific branch of the SCM project to run the template on.
@@ -189,14 +189,15 @@ EXAMPLES = '''
extra_vars: # these override / extend extra_data at the job level extra_vars: # these override / extend extra_data at the job level
food: grape food: grape
animal: owl animal: owl
inventory: 1 organization: Default
inventory: Demo Inventory
- name: Launch bulk jobs with lookup plugin - name: Launch bulk jobs with lookup plugin
bulk_job_launch: bulk_job_launch:
name: My Bulk Job Launch name: My Bulk Job Launch
jobs: jobs:
- unified_job_template: 7 - unified_job_template: 7
- unified_job_template: "{{ lookup('awx.awx.controller_api', 'job_templates', query_params={'name': 'Demo Job Template'}, return_ids=True) }}" - unified_job_template: "{{ lookup('awx.awx.controller_api', 'job_templates', query_params={'name': 'Demo Job Template'}, return_ids=True, expect_one=True) }}"
''' '''
from ..module_utils.controller_api import ControllerAPIModule from ..module_utils.controller_api import ControllerAPIModule
@@ -208,8 +209,8 @@ def main():
jobs=dict(required=True, type='list', elements='dict'), jobs=dict(required=True, type='list', elements='dict'),
name=dict(), name=dict(),
description=dict(), description=dict(),
organization=dict(type='int'), organization=dict(type='str'),
inventory=dict(type='int'), inventory=dict(type='str'),
limit=dict(), limit=dict(),
scm_branch=dict(), scm_branch=dict(),
extra_vars=dict(type='dict'), extra_vars=dict(type='dict'),
@@ -226,8 +227,6 @@ def main():
'jobs', 'jobs',
'name', 'name',
'description', 'description',
'organization',
'inventory',
'limit', 'limit',
'scm_branch', 'scm_branch',
'extra_vars', 'extra_vars',
@@ -240,6 +239,16 @@ def main():
if val: if val:
post_data[p] = val post_data[p] = val
# Resolve name to ID for related resources
# Do not resolve name for "jobs" suboptions, for optimization
org_name = module.params.get('organization')
if org_name:
post_data['organization'] = module.resolve_name_to_id('organizations', org_name)
inv_name = module.params.get('inventory')
if inv_name:
post_data['inventory'] = module.resolve_name_to_id('inventories', inv_name)
# Extract our parameters # Extract our parameters
wait = module.params.get('wait') wait = module.params.get('wait')
timeout = module.params.get('timeout') timeout = module.params.get('timeout')

View File

@@ -33,7 +33,7 @@ def test_bulk_host_create(run_module, admin_user, inventory):
run_module( run_module(
'bulk_host_create', 'bulk_host_create',
{ {
'inventory': inventory.id, 'inventory': inventory.name,
'hosts': hosts, 'hosts': hosts,
}, },
admin_user, admin_user,

View File

@@ -6,7 +6,7 @@
- name: Generate a unique name - name: Generate a unique name
set_fact: set_fact:
bulk_host_name: "AWX-Collection-tests-bulk_host_create-{{ test_id }}" bulk_inv_name: "AWX-Collection-tests-bulk_host_create-{{ test_id }}"
- name: Get our collection package - name: Get our collection package
controller_meta: controller_meta:
@@ -19,7 +19,7 @@
- name: Create an inventory - name: Create an inventory
inventory: inventory:
name: "{{ bulk_host_name }}" name: "{{ bulk_inv_name }}"
organization: Default organization: Default
state: present state: present
register: inventory_result register: inventory_result
@@ -36,7 +36,7 @@
- name: example.dns.gg - name: example.dns.gg
description: "myhost2" description: "myhost2"
enabled: false enabled: false
inventory: "{{ inventory_result.id }}" inventory: "{{ bulk_inv_name }}"
register: result register: result
- assert: - assert:
@@ -46,6 +46,6 @@
# cleanup # cleanup
- name: Delete inventory - name: Delete inventory
inventory: inventory:
name: "{{ bulk_host_name }}" name: "{{ bulk_inv_name }}"
organization: Default organization: Default
state: absent state: absent

View File

@@ -49,7 +49,8 @@
job_tags: "Hello World" job_tags: "Hello World"
limit: "localhost" limit: "localhost"
wait: False wait: False
inventory: "{{ inventory_id }}" inventory: Demo Inventory
organization: Default
register: result register: result
- assert: - assert: