mirror of
https://github.com/ansible/awx.git
synced 2026-03-02 01:08:48 -03:30
Converted tower_job_launch.py
This commit is contained in:
committed by
beeankha
parent
9955ee6548
commit
b4014ebabf
@@ -23,11 +23,12 @@ description:
|
|||||||
- Launch an Ansible Tower jobs. See
|
- Launch an Ansible Tower jobs. See
|
||||||
U(https://www.ansible.com/tower) for an overview.
|
U(https://www.ansible.com/tower) for an overview.
|
||||||
options:
|
options:
|
||||||
job_template:
|
name:
|
||||||
description:
|
description:
|
||||||
- Name of the job template to use.
|
- Name of the job template to use.
|
||||||
required: True
|
required: True
|
||||||
type: str
|
type: str
|
||||||
|
aliases: ['job_template']
|
||||||
job_type:
|
job_type:
|
||||||
description:
|
description:
|
||||||
- Job_type to use for the job, only used if prompt for job_type is set.
|
- Job_type to use for the job, only used if prompt for job_type is set.
|
||||||
@@ -37,10 +38,11 @@ options:
|
|||||||
description:
|
description:
|
||||||
- Inventory to use for the job, only used if prompt for inventory is set.
|
- Inventory to use for the job, only used if prompt for inventory is set.
|
||||||
type: str
|
type: str
|
||||||
credential:
|
credentials:
|
||||||
description:
|
description:
|
||||||
- Credential to use for job, only used if prompt for credential is set.
|
- Credential to use for job, only used if prompt for credential is set.
|
||||||
type: str
|
type: list
|
||||||
|
aliases: ['credential']
|
||||||
extra_vars:
|
extra_vars:
|
||||||
description:
|
description:
|
||||||
- extra_vars to use for the Job Template. Prepend C(@) if a file.
|
- extra_vars to use for the Job Template. Prepend C(@) if a file.
|
||||||
@@ -55,6 +57,27 @@ options:
|
|||||||
description:
|
description:
|
||||||
- Specific tags to use for from playbook.
|
- Specific tags to use for from playbook.
|
||||||
type: list
|
type: list
|
||||||
|
scm_branch:
|
||||||
|
description:
|
||||||
|
- A specific of the SCM project to run the template on.
|
||||||
|
- This is only applicable if your project allows for branch override
|
||||||
|
type: str
|
||||||
|
skip_tags:
|
||||||
|
decription:
|
||||||
|
- Specific tags to skip from the playbook.
|
||||||
|
type: list
|
||||||
|
verbosity:
|
||||||
|
description:
|
||||||
|
- Verbosity level for this job run
|
||||||
|
tpye: int
|
||||||
|
choices: [ 0, 1, 2, 3, 4, 5 ]
|
||||||
|
diff_mode:
|
||||||
|
description:
|
||||||
|
- Show the changes made by Ansible tasks where supported
|
||||||
|
type: bool
|
||||||
|
credential_passwords:
|
||||||
|
description:
|
||||||
|
- Passwords for credentials which are set to prompt on launch
|
||||||
extends_documentation_fragment: awx.awx.auth
|
extends_documentation_fragment: awx.awx.auth
|
||||||
'''
|
'''
|
||||||
|
|
||||||
@@ -105,92 +128,102 @@ status:
|
|||||||
sample: pending
|
sample: pending
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import json
|
from ..module_utils.tower_api import TowerModule
|
||||||
|
|
||||||
from ..module_utils.ansible_tower import TowerModule, tower_auth_config, tower_check_mode
|
|
||||||
|
|
||||||
try:
|
|
||||||
import tower_cli
|
|
||||||
import tower_cli.exceptions as exc
|
|
||||||
|
|
||||||
from tower_cli.conf import settings
|
|
||||||
except ImportError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def update_fields(module, p):
|
|
||||||
params = p.copy()
|
|
||||||
|
|
||||||
params_update = {}
|
|
||||||
job_template = params.get('job_template')
|
|
||||||
extra_vars = params.get('extra_vars')
|
|
||||||
try:
|
|
||||||
job_template_to_launch = tower_cli.get_resource('job_template').get(name=job_template)
|
|
||||||
except (exc.NotFound) as excinfo:
|
|
||||||
module.fail_json(msg='Unable to launch job, job_template/{0} was not found: {1}'.format(job_template, excinfo), changed=False)
|
|
||||||
|
|
||||||
ask_extra_vars = job_template_to_launch['ask_variables_on_launch']
|
|
||||||
survey_enabled = job_template_to_launch['survey_enabled']
|
|
||||||
|
|
||||||
if extra_vars and (ask_extra_vars or survey_enabled):
|
|
||||||
params_update['extra_vars'] = [json.dumps(extra_vars)]
|
|
||||||
|
|
||||||
elif extra_vars:
|
|
||||||
module.fail_json(msg="extra_vars is set on launch but the Job Template does not have ask_extra_vars or survey_enabled set to True.")
|
|
||||||
|
|
||||||
params.update(params_update)
|
|
||||||
return params
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
# Any additional arguments that are not fields of the item can be added here
|
||||||
argument_spec = dict(
|
argument_spec = dict(
|
||||||
job_template=dict(required=True, type='str'),
|
name=dict(type=str, required=True, aliases=['job_template']),
|
||||||
job_type=dict(choices=['run', 'check']),
|
job_type=dict(type=str, choices=['run', 'check']),
|
||||||
inventory=dict(type='str', default=None),
|
inventory=dict(type=str, default=None),
|
||||||
credential=dict(type='str', default=None),
|
# Credentials will be a str instead of a list for backwards compatability
|
||||||
|
credentials=dict(type='list', default=None, aliases=['credential']),
|
||||||
limit=dict(),
|
limit=dict(),
|
||||||
tags=dict(type='list'),
|
tags=dict(type='list'),
|
||||||
extra_vars=dict(type='dict', required=False),
|
extra_vars=dict(type=dict, required=False),
|
||||||
|
scm_branch=dict(type=str, required=False),
|
||||||
|
skip_tags=dict(type=list, required=False),
|
||||||
|
verbosity=dict(type=int, required=False, choices=[0,1,2,3,4,5]),
|
||||||
|
diff_mode=dict(type=bool, required=False),
|
||||||
|
credential_passwords=dict(type=dict, required=False),
|
||||||
)
|
)
|
||||||
|
|
||||||
module = TowerModule(
|
# Create a module for ourselves
|
||||||
argument_spec=argument_spec,
|
module = TowerModule(argument_spec=argument_spec, supports_check_mode=True)
|
||||||
supports_check_mode=True
|
|
||||||
)
|
|
||||||
|
|
||||||
json_output = {}
|
optional_args = {}
|
||||||
tags = module.params.get('tags')
|
# Extract our parameters
|
||||||
|
name = module.params.get('name')
|
||||||
|
optional_args['job_type'] = module.params.get('job_type')
|
||||||
|
inventory = module.params.get('inventory')
|
||||||
|
credentials = module.params.get('credentials')
|
||||||
|
optional_args['limit'] = module.params.get('limit')
|
||||||
|
optional_args['tags'] = module.params.get('tags')
|
||||||
|
optional_args['extra_vars'] = module.params.get('extra_vars')
|
||||||
|
optional_args['scm_branch'] = module.params.get('scm_branch')
|
||||||
|
optional_args['skip_tags'] = module.params.get('skip_tags')
|
||||||
|
optional_args['verbosity'] = module.params.get('verbosity')
|
||||||
|
optional_args['diff_mode'] = module.params.get('diff_mode')
|
||||||
|
optional_args['credential_passwords'] = module.params.get('credential_passwords')
|
||||||
|
|
||||||
tower_auth = tower_auth_config(module)
|
# Create a datastructure to pass into our job launch
|
||||||
with settings.runtime_values(**tower_auth):
|
post_data = {}
|
||||||
tower_check_mode(module)
|
for key in optional_args.keys():
|
||||||
try:
|
if optional_args[key]:
|
||||||
params = module.params.copy()
|
post_data[key] = optional_args[key]
|
||||||
if isinstance(tags, list):
|
|
||||||
params['tags'] = ','.join(tags)
|
|
||||||
job = tower_cli.get_resource('job')
|
|
||||||
|
|
||||||
params = update_fields(module, params)
|
# Attempt to look up the related items the user specified (these will fail the module if not found)
|
||||||
|
if inventory:
|
||||||
|
post_data['inventory'] = module.resolve_name_to_id('inventories', inventory)
|
||||||
|
|
||||||
lookup_fields = ('job_template', 'inventory', 'credential')
|
if credentials:
|
||||||
for field in lookup_fields:
|
post_data['credentials'] = []
|
||||||
try:
|
for credential in credentials:
|
||||||
name = params.pop(field)
|
post_data['credentials'].append( module.resolve_name_to_id('credentials', credential) )
|
||||||
if name:
|
|
||||||
result = tower_cli.get_resource(field).get(name=name)
|
|
||||||
params[field] = result['id']
|
|
||||||
except exc.NotFound as excinfo:
|
|
||||||
module.fail_json(msg='Unable to launch job, {0}/{1} was not found: {2}'.format(field, name, excinfo), changed=False)
|
|
||||||
|
|
||||||
result = job.launch(no_input=True, **params)
|
# Attempt to look up job_template based on the provided name
|
||||||
json_output['id'] = result['id']
|
job_template = module.get_one('job_templates', **{
|
||||||
json_output['status'] = result['status']
|
'data': {
|
||||||
except (exc.ConnectionError, exc.BadRequest, exc.AuthError) as excinfo:
|
'name': name,
|
||||||
module.fail_json(msg='Unable to launch job: {0}'.format(excinfo), changed=False)
|
}
|
||||||
|
})
|
||||||
|
|
||||||
json_output['changed'] = result['changed']
|
if job_template == None:
|
||||||
module.exit_json(**json_output)
|
module.fail_json(msg="Unable to find job template by name {0}".format(name))
|
||||||
|
|
||||||
|
# The API will allow you to submit values to a jb launch that are not prompt on launch.
|
||||||
|
# Therefore, we will test to see if anything is set which is not prompt on launch and fail.
|
||||||
|
check_vars_to_prompts = {
|
||||||
|
'scm_branch': 'ask_scm_branch_on_launch',
|
||||||
|
'diff_mode': 'ask_diff_mode_on_launch',
|
||||||
|
'extra_vars': 'ask_variables_on_launch',
|
||||||
|
'limit': 'ask_limit_on_launch',
|
||||||
|
'tags': 'ask_tags_on_launch',
|
||||||
|
'skip_tags': 'ask_skip_tags_on_launch',
|
||||||
|
'job_type': 'ask_job_type_on_launch',
|
||||||
|
'verbosity': 'ask_verbosity_on_launch',
|
||||||
|
'inventory': 'ask_inventory_on_launch',
|
||||||
|
'credentials': 'ask_credential_on_launch',
|
||||||
|
}
|
||||||
|
|
||||||
|
param_errors = []
|
||||||
|
for variable_name in check_vars_to_prompts:
|
||||||
|
if module.params.get(variable_name) and not job_template[check_vars_to_prompts[variable_name]]:
|
||||||
|
param_errors.append("The field {0} was specified but the job template does not allow for it to be overridden".format(variable_name))
|
||||||
|
if len(param_errors) > 0:
|
||||||
|
module.fail_json(msg="Parameters specified which can not be passed into job template, see errors for details", **{ 'errors': param_errors })
|
||||||
|
|
||||||
|
# Launch the job
|
||||||
|
results = module.post_endpoint(job_template['related']['launch'], **{ 'data': post_data })
|
||||||
|
|
||||||
|
if results['status_code'] != 201:
|
||||||
|
module.fail_json(msg="Failed to launch job, see response for details", **{'response': results })
|
||||||
|
|
||||||
|
module.exit_json(**{
|
||||||
|
'changed': True,
|
||||||
|
'id': results['json']['id'],
|
||||||
|
'status': results['json']['status'],
|
||||||
|
})
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user