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
inventory:
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
type: int
type: str
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
argument_spec = 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
module = ControllerAPIModule(argument_spec=argument_spec)
# Extract our parameters
inventory = module.params.get('inventory')
inv_name = module.params.get('inventory')
hosts = module.params.get('hosts')
for h in hosts:
if 'variables' in h:
h['variables'] = json.dumps(h['variables'])
inv_id = module.resolve_name_to_id('inventories', inv_name)
# 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:
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.
- Required if the user belongs to more than one organization.
- Affects who can see the resulting bulk job.
type: int
type: str
inventory:
description:
- Inventory ID to use for the jobs ran within the bulk job, only used if prompt for inventory is set.
type: int
- Inventory name or ID to use for the jobs ran within the bulk job, only used if prompt for inventory is set.
type: str
scm_branch:
description:
- 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
food: grape
animal: owl
inventory: 1
organization: Default
inventory: Demo Inventory
- name: Launch bulk jobs with lookup plugin
bulk_job_launch:
name: My Bulk Job Launch
jobs:
- 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
@@ -208,8 +209,8 @@ def main():
jobs=dict(required=True, type='list', elements='dict'),
name=dict(),
description=dict(),
organization=dict(type='int'),
inventory=dict(type='int'),
organization=dict(type='str'),
inventory=dict(type='str'),
limit=dict(),
scm_branch=dict(),
extra_vars=dict(type='dict'),
@@ -226,8 +227,6 @@ def main():
'jobs',
'name',
'description',
'organization',
'inventory',
'limit',
'scm_branch',
'extra_vars',
@@ -240,6 +239,16 @@ def main():
if 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
wait = module.params.get('wait')
timeout = module.params.get('timeout')