mirror of
https://github.com/ansible/awx.git
synced 2026-03-05 18:51:06 -03:30
Second attempt at converting tower_job_template
This commit is contained in:
@@ -475,6 +475,10 @@ class TowerModule(AnsibleModule):
|
|||||||
self.exit_json(**self.json_output)
|
self.exit_json(**self.json_output)
|
||||||
|
|
||||||
def modify_associations(self, association_endpoint, new_association_list):
|
def modify_associations(self, association_endpoint, new_association_list):
|
||||||
|
# if we got None instead of [] we are not modifying the association_list
|
||||||
|
if new_association_list is None:
|
||||||
|
return
|
||||||
|
|
||||||
# First get the existing associations
|
# First get the existing associations
|
||||||
response = self.get_all_endpoint(association_endpoint)
|
response = self.get_all_endpoint(association_endpoint)
|
||||||
existing_associated_ids = [association['id'] for association in response['json']['results']]
|
existing_associated_ids = [association['id'] for association in response['json']['results']]
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ DOCUMENTATION = '''
|
|||||||
module: tower_job_template
|
module: tower_job_template
|
||||||
author: "Wayne Witzel III (@wwitzel3)"
|
author: "Wayne Witzel III (@wwitzel3)"
|
||||||
version_added: "2.3"
|
version_added: "2.3"
|
||||||
short_description: create, update, or destroy Ansible Tower job template.
|
short_description: create, update, or destroy Ansible Tower job templates.
|
||||||
description:
|
description:
|
||||||
- Create, update, or destroy Ansible Tower job templates. See
|
- Create, update, or destroy Ansible Tower job templates. See
|
||||||
U(https://www.ansible.com/tower) for an overview.
|
U(https://www.ansible.com/tower) for an overview.
|
||||||
@@ -28,6 +28,10 @@ options:
|
|||||||
- Name to use for the job template.
|
- Name to use for the job template.
|
||||||
required: True
|
required: True
|
||||||
type: str
|
type: str
|
||||||
|
new_name:
|
||||||
|
description:
|
||||||
|
- Setting this option will change the existing name (looed up via the name field.
|
||||||
|
type: str
|
||||||
description:
|
description:
|
||||||
description:
|
description:
|
||||||
- Description to use for the job template.
|
- Description to use for the job template.
|
||||||
@@ -45,31 +49,28 @@ options:
|
|||||||
project:
|
project:
|
||||||
description:
|
description:
|
||||||
- Name of the project to use for the job template.
|
- Name of the project to use for the job template.
|
||||||
required: True
|
|
||||||
type: str
|
type: str
|
||||||
playbook:
|
playbook:
|
||||||
description:
|
description:
|
||||||
- Path to the playbook to use for the job template within the project provided.
|
- Path to the playbook to use for the job template within the project provided.
|
||||||
required: True
|
|
||||||
type: str
|
type: str
|
||||||
credential:
|
credential:
|
||||||
description:
|
description:
|
||||||
- Name of the credential to use for the job template.
|
- Name of the credential to use for the job template.
|
||||||
- Deprecated, mutually exclusive with 'credentials'.
|
- Deprecated, use 'credentials'.
|
||||||
version_added: 2.7
|
version_added: 2.7
|
||||||
type: str
|
type: str
|
||||||
credentials:
|
credentials:
|
||||||
description:
|
description:
|
||||||
- List of credentials to use for the job template.
|
- List of credentials to use for the job template.
|
||||||
- Will not remove any existing credentials. This may change in the future.
|
|
||||||
version_added: 2.8
|
|
||||||
type: list
|
type: list
|
||||||
default: []
|
|
||||||
elements: str
|
elements: str
|
||||||
|
version_added: 2.8
|
||||||
|
default: []
|
||||||
vault_credential:
|
vault_credential:
|
||||||
description:
|
description:
|
||||||
- Name of the vault credential to use for the job template.
|
- Name of the vault credential to use for the job template.
|
||||||
- Deprecated, mutually exclusive with 'credentials'.
|
- Deprecated, use 'credentials'.
|
||||||
version_added: 2.7
|
version_added: 2.7
|
||||||
type: str
|
type: str
|
||||||
forks:
|
forks:
|
||||||
@@ -91,21 +92,18 @@ options:
|
|||||||
- Specify C(extra_vars) for the template.
|
- Specify C(extra_vars) for the template.
|
||||||
type: dict
|
type: dict
|
||||||
version_added: 3.7
|
version_added: 3.7
|
||||||
extra_vars_path:
|
|
||||||
description:
|
|
||||||
- This parameter has been deprecated, please use 'extra_vars' instead.
|
|
||||||
- Path to the C(extra_vars) YAML file.
|
|
||||||
type: path
|
|
||||||
job_tags:
|
job_tags:
|
||||||
description:
|
description:
|
||||||
- Comma separated list of the tags to use for the job template.
|
- Comma separated list of the tags to use for the job template.
|
||||||
type: str
|
type: str
|
||||||
force_handlers_enabled:
|
force_handlers:
|
||||||
description:
|
description:
|
||||||
- Enable forcing playbook handlers to run even if a task fails.
|
- Enable forcing playbook handlers to run even if a task fails.
|
||||||
version_added: 2.7
|
version_added: 2.7
|
||||||
type: bool
|
type: bool
|
||||||
default: 'no'
|
default: 'no'
|
||||||
|
aliases:
|
||||||
|
- force_handlers_enabled
|
||||||
skip_tags:
|
skip_tags:
|
||||||
description:
|
description:
|
||||||
- Comma separated list of the tags to skip for the job template.
|
- Comma separated list of the tags to skip for the job template.
|
||||||
@@ -115,71 +113,98 @@ options:
|
|||||||
- Start the playbook at the task matching this name.
|
- Start the playbook at the task matching this name.
|
||||||
version_added: 2.7
|
version_added: 2.7
|
||||||
type: str
|
type: str
|
||||||
diff_mode_enabled:
|
diff_mode:
|
||||||
description:
|
description:
|
||||||
- Enable diff mode for the job template.
|
- Enable diff mode for the job template.
|
||||||
version_added: 2.7
|
version_added: 2.7
|
||||||
type: bool
|
type: bool
|
||||||
|
aliases:
|
||||||
|
- diff_mode_enabled
|
||||||
default: 'no'
|
default: 'no'
|
||||||
fact_caching_enabled:
|
use_fact_cache:
|
||||||
description:
|
description:
|
||||||
- Enable use of fact caching for the job template.
|
- Enable use of fact caching for the job template.
|
||||||
version_added: 2.7
|
version_added: 2.7
|
||||||
type: bool
|
type: bool
|
||||||
default: 'no'
|
default: 'no'
|
||||||
|
aliases:
|
||||||
|
- fact_caching_enabled
|
||||||
host_config_key:
|
host_config_key:
|
||||||
description:
|
description:
|
||||||
- Allow provisioning callbacks using this host config key.
|
- Allow provisioning callbacks using this host config key.
|
||||||
type: str
|
type: str
|
||||||
ask_diff_mode:
|
ask_scm_branch_on_launch:
|
||||||
|
description:
|
||||||
|
- Prompt user for (scm branch) on launch.
|
||||||
|
type: bool
|
||||||
|
default: 'False'
|
||||||
|
ask_diff_mode_on_launch:
|
||||||
description:
|
description:
|
||||||
- Prompt user to enable diff mode (show changes) to files when supported by modules.
|
- Prompt user to enable diff mode (show changes) to files when supported by modules.
|
||||||
version_added: 2.7
|
version_added: 2.7
|
||||||
type: bool
|
type: bool
|
||||||
default: 'no'
|
default: 'False'
|
||||||
ask_extra_vars:
|
aliases:
|
||||||
|
- ask_diff_mode
|
||||||
|
ask_variables_on_launch:
|
||||||
description:
|
description:
|
||||||
- Prompt user for (extra_vars) on launch.
|
- Prompt user for (extra_vars) on launch.
|
||||||
type: bool
|
type: bool
|
||||||
default: 'no'
|
default: 'False'
|
||||||
ask_limit:
|
aliases:
|
||||||
|
- ask_extra_vars
|
||||||
|
ask_limit_on_launch:
|
||||||
description:
|
description:
|
||||||
- Prompt user for a limit on launch.
|
- Prompt user for a limit on launch.
|
||||||
version_added: 2.7
|
version_added: 2.7
|
||||||
type: bool
|
type: bool
|
||||||
default: 'no'
|
default: 'False'
|
||||||
ask_tags:
|
aliases:
|
||||||
|
- ask_limit
|
||||||
|
ask_tags_on_launch:
|
||||||
description:
|
description:
|
||||||
- Prompt user for job tags on launch.
|
- Prompt user for job tags on launch.
|
||||||
type: bool
|
type: bool
|
||||||
default: 'no'
|
default: 'False'
|
||||||
ask_skip_tags:
|
aliases:
|
||||||
|
- ask_tags
|
||||||
|
ask_skip_tags_on_launch:
|
||||||
description:
|
description:
|
||||||
- Prompt user for job tags to skip on launch.
|
- Prompt user for job tags to skip on launch.
|
||||||
version_added: 2.7
|
version_added: 2.7
|
||||||
type: bool
|
type: bool
|
||||||
default: 'no'
|
default: 'False'
|
||||||
ask_job_type:
|
aliases:
|
||||||
|
- ask_skip_tags
|
||||||
|
ask_job_type_on_launch:
|
||||||
description:
|
description:
|
||||||
- Prompt user for job type on launch.
|
- Prompt user for job type on launch.
|
||||||
type: bool
|
type: bool
|
||||||
default: 'no'
|
default: 'False'
|
||||||
ask_verbosity:
|
aliases:
|
||||||
|
- ask_job_type
|
||||||
|
ask_verbosity_on_launch:
|
||||||
description:
|
description:
|
||||||
- Prompt user to choose a verbosity level on launch.
|
- Prompt user to choose a verbosity level on launch.
|
||||||
version_added: 2.7
|
version_added: 2.7
|
||||||
type: bool
|
type: bool
|
||||||
default: 'no'
|
default: 'False'
|
||||||
ask_inventory:
|
aliases:
|
||||||
|
- ask_verbosity
|
||||||
|
ask_inventory_on_launch:
|
||||||
description:
|
description:
|
||||||
- Prompt user for inventory on launch.
|
- Prompt user for inventory on launch.
|
||||||
type: bool
|
type: bool
|
||||||
default: 'no'
|
default: 'False'
|
||||||
ask_credential:
|
aliases:
|
||||||
|
- ask_inventory
|
||||||
|
ask_credential_on_launch:
|
||||||
description:
|
description:
|
||||||
- Prompt user for credential on launch.
|
- Prompt user for credential on launch.
|
||||||
type: bool
|
type: bool
|
||||||
default: 'no'
|
default: 'False'
|
||||||
|
aliases:
|
||||||
|
- ask_credential
|
||||||
survey_enabled:
|
survey_enabled:
|
||||||
description:
|
description:
|
||||||
- Enable a survey on the job template.
|
- Enable a survey on the job template.
|
||||||
@@ -191,18 +216,19 @@ options:
|
|||||||
- JSON/YAML dict formatted survey definition.
|
- JSON/YAML dict formatted survey definition.
|
||||||
version_added: 2.8
|
version_added: 2.8
|
||||||
type: dict
|
type: dict
|
||||||
required: False
|
|
||||||
become_enabled:
|
become_enabled:
|
||||||
description:
|
description:
|
||||||
- Activate privilege escalation.
|
- Activate privilege escalation.
|
||||||
type: bool
|
type: bool
|
||||||
default: 'no'
|
default: 'no'
|
||||||
concurrent_jobs_enabled:
|
allow_simultaneous:
|
||||||
description:
|
description:
|
||||||
- Allow simultaneous runs of the job template.
|
- Allow simultaneous runs of the job template.
|
||||||
version_added: 2.7
|
version_added: 2.7
|
||||||
type: bool
|
type: bool
|
||||||
default: 'no'
|
default: 'no'
|
||||||
|
aliases:
|
||||||
|
- concurrent_jobs_enabled
|
||||||
timeout:
|
timeout:
|
||||||
description:
|
description:
|
||||||
- Maximum time in seconds to wait for a job to finish (server-side).
|
- Maximum time in seconds to wait for a job to finish (server-side).
|
||||||
@@ -212,17 +238,43 @@ options:
|
|||||||
description:
|
description:
|
||||||
- Local absolute file path containing a custom Python virtualenv to use.
|
- Local absolute file path containing a custom Python virtualenv to use.
|
||||||
type: str
|
type: str
|
||||||
required: False
|
job_slice_count:
|
||||||
|
description:
|
||||||
|
- The number of jobs to slice into at runtime. Will cause the Job Template to launch a workflow if value is greater than 1.
|
||||||
|
type: int
|
||||||
|
default: '1'
|
||||||
|
webhook_service:
|
||||||
|
description:
|
||||||
|
- Service that webhook requests will be accepted from
|
||||||
|
type: str
|
||||||
|
choices:
|
||||||
|
- 'github'
|
||||||
|
- 'gitlab'
|
||||||
|
webhook_credential:
|
||||||
|
description:
|
||||||
|
- Personal Access Token for posting back the status to the service API
|
||||||
|
type: str
|
||||||
|
scm_branch:
|
||||||
|
description:
|
||||||
|
- Branch to use in job run. Project default used if blank. Only allowed if project allow_override field is set to true.
|
||||||
|
type: str
|
||||||
default: ''
|
default: ''
|
||||||
|
labels:
|
||||||
|
description:
|
||||||
|
- The labels applied to this job template
|
||||||
|
type: list
|
||||||
|
elements: str
|
||||||
state:
|
state:
|
||||||
description:
|
description:
|
||||||
- Desired state of the resource.
|
- Desired state of the resource.
|
||||||
default: "present"
|
default: "present"
|
||||||
choices: ["present", "absent"]
|
choices: ["present", "absent"]
|
||||||
type: str
|
type: str
|
||||||
|
tower_oauthtoken:
|
||||||
requirements:
|
description:
|
||||||
- ansible-tower-cli >= 3.0.2
|
- The Tower OAuth token to use.
|
||||||
|
type: str
|
||||||
|
version_added: "3.7"
|
||||||
|
|
||||||
extends_documentation_fragment: awx.awx.auth
|
extends_documentation_fragment: awx.awx.auth
|
||||||
|
|
||||||
@@ -241,7 +293,8 @@ EXAMPLES = '''
|
|||||||
inventory: "Local"
|
inventory: "Local"
|
||||||
project: "Demo"
|
project: "Demo"
|
||||||
playbook: "ping.yml"
|
playbook: "ping.yml"
|
||||||
credential: "Local"
|
credentials:
|
||||||
|
- "Local"
|
||||||
state: "present"
|
state: "present"
|
||||||
tower_config_file: "~/tower_cli.cfg"
|
tower_config_file: "~/tower_cli.cfg"
|
||||||
survey_enabled: yes
|
survey_enabled: yes
|
||||||
@@ -249,172 +302,164 @@ EXAMPLES = '''
|
|||||||
custom_virtualenv: "/var/lib/awx/venv/custom-venv/"
|
custom_virtualenv: "/var/lib/awx/venv/custom-venv/"
|
||||||
'''
|
'''
|
||||||
|
|
||||||
from ..module_utils.ansible_tower import TowerModule, tower_auth_config, tower_check_mode
|
from ..module_utils.tower_api import TowerModule
|
||||||
import json
|
|
||||||
|
|
||||||
|
|
||||||
try:
|
def update_survey(module, last_request):
|
||||||
import tower_cli
|
spec_endpoint = last_request.get('related', {}).get('survey_spec')
|
||||||
import tower_cli.exceptions as exc
|
if module.params.get('survey_spec') == {}:
|
||||||
|
response = module.delete_endpoint(spec_endpoint)
|
||||||
from tower_cli.conf import settings
|
if response['status_code'] != 200:
|
||||||
except ImportError:
|
# Not sure how to make this actually return a non 200 to test what to dump in the respinse
|
||||||
pass
|
module.fail_json(msg="Failed to delete survey: {0}".format(response['json']))
|
||||||
|
else:
|
||||||
|
response = module.post_endpoint(spec_endpoint, **{'data': module.params.get('survey_spec')})
|
||||||
def update_fields(module, p):
|
if response['status_code'] != 200:
|
||||||
'''This updates the module field names
|
module.fail_json(msg="Failed to update survey: {0}".format(response['json']['error']))
|
||||||
to match the field names tower-cli expects to make
|
module.exit_json(**module.json_output)
|
||||||
calling of the modify/delete methods easier.
|
|
||||||
'''
|
|
||||||
params = p.copy()
|
|
||||||
field_map = {
|
|
||||||
'fact_caching_enabled': 'use_fact_cache',
|
|
||||||
'ask_diff_mode': 'ask_diff_mode_on_launch',
|
|
||||||
'ask_extra_vars': 'ask_variables_on_launch',
|
|
||||||
'ask_limit': 'ask_limit_on_launch',
|
|
||||||
'ask_tags': 'ask_tags_on_launch',
|
|
||||||
'ask_skip_tags': 'ask_skip_tags_on_launch',
|
|
||||||
'ask_verbosity': 'ask_verbosity_on_launch',
|
|
||||||
'ask_inventory': 'ask_inventory_on_launch',
|
|
||||||
'ask_credential': 'ask_credential_on_launch',
|
|
||||||
'ask_job_type': 'ask_job_type_on_launch',
|
|
||||||
'diff_mode_enabled': 'diff_mode',
|
|
||||||
'concurrent_jobs_enabled': 'allow_simultaneous',
|
|
||||||
'force_handlers_enabled': 'force_handlers',
|
|
||||||
}
|
|
||||||
|
|
||||||
params_update = {}
|
|
||||||
for old_k, new_k in field_map.items():
|
|
||||||
v = params.pop(old_k)
|
|
||||||
params_update[new_k] = v
|
|
||||||
|
|
||||||
extra_vars = params.get('extra_vars')
|
|
||||||
extra_vars_path = params.get('extra_vars_path')
|
|
||||||
|
|
||||||
if extra_vars:
|
|
||||||
params_update['extra_vars'] = [json.dumps(extra_vars)]
|
|
||||||
|
|
||||||
elif extra_vars_path is not None:
|
|
||||||
params_update['extra_vars'] = ['@' + extra_vars_path]
|
|
||||||
module.deprecate(
|
|
||||||
msg='extra_vars_path should not be used anymore. Use \'extra_vars: "{{ lookup(\'file\', \'/path/to/file\') | from_yaml }}"\' instead',
|
|
||||||
version="3.8"
|
|
||||||
)
|
|
||||||
|
|
||||||
params.update(params_update)
|
|
||||||
return params
|
|
||||||
|
|
||||||
|
|
||||||
def update_resources(module, p):
|
|
||||||
params = p.copy()
|
|
||||||
identity_map = {
|
|
||||||
'project': 'name',
|
|
||||||
'inventory': 'name',
|
|
||||||
'credential': 'name',
|
|
||||||
'vault_credential': 'name',
|
|
||||||
}
|
|
||||||
for k, v in identity_map.items():
|
|
||||||
try:
|
|
||||||
if params[k]:
|
|
||||||
key = 'credential' if '_credential' in k else k
|
|
||||||
result = tower_cli.get_resource(key).get(**{v: params[k]})
|
|
||||||
params[k] = result['id']
|
|
||||||
elif k in params:
|
|
||||||
# unset empty parameters to avoid ValueError: invalid literal for int() with base 10: ''
|
|
||||||
del(params[k])
|
|
||||||
except (exc.NotFound) as excinfo:
|
|
||||||
module.fail_json(msg='Failed to update job template: {0}'.format(excinfo), changed=False)
|
|
||||||
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(
|
||||||
name=dict(required=True),
|
name=dict(required=True),
|
||||||
|
new_name=dict(),
|
||||||
description=dict(default=''),
|
description=dict(default=''),
|
||||||
job_type=dict(choices=['run', 'check']),
|
job_type=dict(choices=['run', 'check']),
|
||||||
inventory=dict(default=''),
|
inventory=dict(),
|
||||||
project=dict(required=True),
|
project=dict(),
|
||||||
playbook=dict(required=True),
|
playbook=dict(),
|
||||||
credential=dict(default=''),
|
credential=dict(default=''),
|
||||||
vault_credential=dict(default=''),
|
vault_credential=dict(default=''),
|
||||||
custom_virtualenv=dict(type='str', required=False),
|
custom_virtualenv=dict(required=False),
|
||||||
credentials=dict(type='list', default=[], elements='str'),
|
credentials=dict(type='list', default=[], elements='str'),
|
||||||
forks=dict(type='int'),
|
forks=dict(type='int'),
|
||||||
limit=dict(default=''),
|
limit=dict(default=''),
|
||||||
verbosity=dict(type='int', choices=[0, 1, 2, 3, 4], default=0),
|
verbosity=dict(type='int', choices=[0, 1, 2, 3, 4], default=0),
|
||||||
extra_vars=dict(type='dict', required=False),
|
extra_vars=dict(type='dict', required=False),
|
||||||
extra_vars_path=dict(type='path', required=False),
|
|
||||||
job_tags=dict(default=''),
|
job_tags=dict(default=''),
|
||||||
force_handlers_enabled=dict(type='bool', default=False),
|
force_handlers=dict(type='bool', default=False, aliases=['force_handlers_enabled']),
|
||||||
skip_tags=dict(default=''),
|
skip_tags=dict(default=''),
|
||||||
start_at_task=dict(default=''),
|
start_at_task=dict(default=''),
|
||||||
timeout=dict(type='int', default=0),
|
timeout=dict(type='int', default=0),
|
||||||
fact_caching_enabled=dict(type='bool', default=False),
|
use_fact_cache=dict(type='bool', aliases=['fact_caching_enabled']),
|
||||||
host_config_key=dict(default=''),
|
host_config_key=dict(),
|
||||||
ask_diff_mode=dict(type='bool', default=False),
|
ask_diff_mode_on_launch=dict(type='bool', aliases=['ask_diff_mode']),
|
||||||
ask_extra_vars=dict(type='bool', default=False),
|
ask_variables_on_launch=dict(type='bool', aliases=['ask_extra_vars']),
|
||||||
ask_limit=dict(type='bool', default=False),
|
ask_limit_on_launch=dict(type='bool', aliases=['ask_limit']),
|
||||||
ask_tags=dict(type='bool', default=False),
|
ask_tags_on_launch=dict(type='bool', aliases=['ask_tags']),
|
||||||
ask_skip_tags=dict(type='bool', default=False),
|
ask_skip_tags_on_launch=dict(type='bool', aliases=['ask_skip_tags']),
|
||||||
ask_job_type=dict(type='bool', default=False),
|
ask_job_type_on_launch=dict(type='bool', aliases=['ask_job_type']),
|
||||||
ask_verbosity=dict(type='bool', default=False),
|
ask_verbosity_on_launch=dict(type='bool', aliases=['ask_verbosity']),
|
||||||
ask_inventory=dict(type='bool', default=False),
|
ask_inventory_on_launch=dict(type='bool', aliases=['ask_inventory']),
|
||||||
ask_credential=dict(type='bool', default=False),
|
ask_credential_on_launch=dict(type='bool', aliases=['ask_credential']),
|
||||||
survey_enabled=dict(type='bool', default=False),
|
survey_enabled=dict(type='bool'),
|
||||||
survey_spec=dict(type='dict', required=False),
|
survey_spec=dict(type="dict"),
|
||||||
become_enabled=dict(type='bool', default=False),
|
become_enabled=dict(type='bool'),
|
||||||
diff_mode_enabled=dict(type='bool', default=False),
|
diff_mode=dict(type='bool', aliases=['diff_mode_enabled']),
|
||||||
concurrent_jobs_enabled=dict(type='bool', default=False),
|
allow_simultaneous=dict(type='bool', aliases=['concurrent_jobs_enabled']),
|
||||||
|
scm_branch=dict(),
|
||||||
|
ask_scm_branch_on_launch=dict(type='bool'),
|
||||||
|
job_slice_count=dict(type='int', default='1'),
|
||||||
|
webhook_service=dict(choices=['github', 'gitlab']),
|
||||||
|
webhook_credential=dict(),
|
||||||
|
labels=dict(type="list", elements='str'),
|
||||||
state=dict(choices=['present', 'absent'], default='present'),
|
state=dict(choices=['present', 'absent'], default='present'),
|
||||||
)
|
)
|
||||||
|
|
||||||
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,
|
|
||||||
mutually_exclusive=[
|
|
||||||
('credential', 'credentials'),
|
|
||||||
('vault_credential', 'credentials'),
|
|
||||||
('extra_vars_path', 'extra_vars'),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
# Extract our parameters
|
||||||
name = module.params.get('name')
|
name = module.params.get('name')
|
||||||
state = module.params.pop('state')
|
new_name = module.params.get("new_name")
|
||||||
json_output = {'job_template': name, 'state': state}
|
state = module.params.get('state')
|
||||||
|
|
||||||
tower_auth = tower_auth_config(module)
|
# Deal with legacy credential and vault_credential
|
||||||
with settings.runtime_values(**tower_auth):
|
credential = module.params.get('credential')
|
||||||
tower_check_mode(module)
|
vault_credential = module.params.get('vault_credential')
|
||||||
jt = tower_cli.get_resource('job_template')
|
credentials = module.params.get('credentials')
|
||||||
|
if vault_credential:
|
||||||
|
if credentials is None:
|
||||||
|
credentials = []
|
||||||
|
credentials.append(vault_credential)
|
||||||
|
if credential:
|
||||||
|
if credentials is None:
|
||||||
|
credentials = []
|
||||||
|
credentials.append(credential)
|
||||||
|
|
||||||
params = update_resources(module, module.params)
|
# Attempt to look up an existing item based on the provided data
|
||||||
params = update_fields(module, params)
|
existing_item = module.get_one('job_templates', **{
|
||||||
params['create_on_missing'] = True
|
'data': {
|
||||||
|
'name': name,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
try:
|
# Create the data that gets sent for create and update
|
||||||
if state == 'present':
|
new_fields = {}
|
||||||
result = jt.modify(**params)
|
new_fields['name'] = new_name if new_name else name
|
||||||
json_output['id'] = result['id']
|
for field_name in (
|
||||||
elif state == 'absent':
|
'description', 'job_type', 'playbook', 'scm_branch', 'forks', 'limit', 'verbosity', 'extra_vars',
|
||||||
result = jt.delete(**params)
|
'job_tags', 'force_handlers', 'skip_tags', 'start_at_task', 'timeout', 'use_fact_cache',
|
||||||
except (exc.ConnectionError, exc.BadRequest, exc.NotFound, exc.AuthError) as excinfo:
|
'host_config_key', 'ask_scm_branch_on_launch', 'ask_diff_mode_on_launch', 'ask_variables_on_launch',
|
||||||
module.fail_json(msg='Failed to update job template: {0}'.format(excinfo), changed=False)
|
'ask_limit_on_launch', 'ask_tags_on_launch', 'ask_skip_tags_on_launch', 'ask_job_type_on_launch',
|
||||||
|
'ask_verbosity_on_launch', 'ask_inventory_on_launch', 'ask_credential_on_launch', 'survey_enabled',
|
||||||
|
'become_enabled', 'diff_mode', 'allow_simultaneous', 'custom_virtualenv', 'job_slice_count', 'webhook_service',
|
||||||
|
):
|
||||||
|
field_val = module.params.get(field_name)
|
||||||
|
if field_val:
|
||||||
|
new_fields[field_name] = field_val
|
||||||
|
|
||||||
cred_list = module.params.get('credentials')
|
# Attempt to look up the related items the user specified (these will fail the module if not found)
|
||||||
if cred_list:
|
inventory = module.params.get('inventory')
|
||||||
cred = tower_cli.get_resource('credential')
|
project = module.params.get('project')
|
||||||
for cred_name in cred_list:
|
webhook_credential = module.params.get('webhook_credential')
|
||||||
try:
|
|
||||||
cred_id = cred.get(name=cred_name)['id']
|
|
||||||
r = jt.associate_credential(result['id'], cred_id)
|
|
||||||
except (exc.ConnectionError, exc.BadRequest, exc.NotFound, exc.AuthError) as excinfo:
|
|
||||||
module.fail_json(msg='Failed to add credential to job template: {0}'.format(excinfo), changed=False)
|
|
||||||
if r.get('changed'):
|
|
||||||
result['changed'] = True
|
|
||||||
|
|
||||||
json_output['changed'] = result['changed']
|
if inventory is not None:
|
||||||
module.exit_json(**json_output)
|
new_fields['inventory'] = module.resolve_name_to_id('inventories', inventory)
|
||||||
|
if project is not None:
|
||||||
|
new_fields['project'] = module.resolve_name_to_id('projects', project)
|
||||||
|
if webhook_credential is not None:
|
||||||
|
new_fields['webhook_credential'] = module.resolve_name_to_id('credentials', webhook_credential)
|
||||||
|
|
||||||
|
credentials_ids = None
|
||||||
|
if credentials is not None:
|
||||||
|
credentials_ids = []
|
||||||
|
for item in credentials:
|
||||||
|
credentials_ids.append(module.resolve_name_to_id('credentials', item))
|
||||||
|
|
||||||
|
labels = module.params.get('labels')
|
||||||
|
labels_ids = None
|
||||||
|
if labels is not None:
|
||||||
|
labels_ids = []
|
||||||
|
for item in labels:
|
||||||
|
labels_ids.append(module.resolve_name_to_id('labels', item))
|
||||||
|
|
||||||
|
on_change = None
|
||||||
|
new_spec = module.params.get('survey_spec')
|
||||||
|
if new_spec is not None:
|
||||||
|
existing_spec = None
|
||||||
|
if existing_item:
|
||||||
|
spec_endpoint = existing_item.get('related', {}).get('survey_spec')
|
||||||
|
existing_spec = module.get_endpoint(spec_endpoint)['json']
|
||||||
|
if new_spec != existing_spec:
|
||||||
|
module.json_output['changed'] = True
|
||||||
|
on_change = update_survey
|
||||||
|
|
||||||
|
if state == 'absent':
|
||||||
|
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this
|
||||||
|
module.delete_if_needed(existing_item)
|
||||||
|
elif state == 'present':
|
||||||
|
# If the state was present and we can let the module build or update the existing item, this will return on its own
|
||||||
|
module.create_or_update_if_needed(
|
||||||
|
existing_item, new_fields,
|
||||||
|
endpoint='job_templates', item_type='job_template',
|
||||||
|
associations={
|
||||||
|
'credentials': credentials_ids,
|
||||||
|
'labels': labels_ids,
|
||||||
|
},
|
||||||
|
on_create=on_change, on_update=on_change,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
@@ -1,12 +1,27 @@
|
|||||||
---
|
---
|
||||||
|
- name: Generate a random string for test
|
||||||
|
set_fact:
|
||||||
|
test_id: "{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}"
|
||||||
|
|
||||||
- name: generate random string for project
|
- name: generate random string for project
|
||||||
set_fact:
|
set_fact:
|
||||||
cred1: "AWX-Collection-tests-tower_job_template-cred1-{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}"
|
cred1: "AWX-Collection-tests-tower_job_template-cred1-{{ test_id }}"
|
||||||
cred2: "AWX-Collection-tests-tower_job_template-cred2-{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}"
|
cred2: "AWX-Collection-tests-tower_job_template-cred2-{{ test_id }}"
|
||||||
cred3: "AWX-Collection-tests-tower_job_template-cred3-{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}"
|
cred3: "AWX-Collection-tests-tower_job_template-cred3-{{ test_id }}"
|
||||||
proj1: "AWX-Collection-tests-tower_job_template-proj-{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}"
|
proj1: "AWX-Collection-tests-tower_job_template-proj-{{ test_id }}"
|
||||||
jt1: "AWX-Collection-tests-tower_job_template-jt1-{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}"
|
jt1: "AWX-Collection-tests-tower_job_template-jt1-{{ test_id }}"
|
||||||
jt2: "AWX-Collection-tests-tower_job_template-jt2-{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}"
|
jt2: "AWX-Collection-tests-tower_job_template-jt2-{{ test_id }}"
|
||||||
|
lab1: "AWX-Collection-tests-tower_job_template-lab1-{{ test_id }}"
|
||||||
|
|
||||||
|
- name: Create a Demo Project
|
||||||
|
tower_project:
|
||||||
|
name: "{{ proj1 }}"
|
||||||
|
organization: Default
|
||||||
|
state: present
|
||||||
|
scm_type: git
|
||||||
|
scm_url: https://github.com/ansible/ansible-tower-samples.git
|
||||||
|
|
||||||
|
register: result
|
||||||
|
|
||||||
- name: Create Credential1
|
- name: Create Credential1
|
||||||
tower_credential:
|
tower_credential:
|
||||||
@@ -26,14 +41,10 @@
|
|||||||
organization: Default
|
organization: Default
|
||||||
kind: ssh
|
kind: ssh
|
||||||
|
|
||||||
- name: Create a Demo Project
|
- name: Create Label
|
||||||
tower_project:
|
tower_label:
|
||||||
name: "{{ proj1 }}"
|
name: "{{ lab1 }}"
|
||||||
organization: Default
|
organization: Default
|
||||||
state: present
|
|
||||||
scm_type: git
|
|
||||||
scm_url: https://github.com/ansible/ansible-tower-samples.git
|
|
||||||
register: result
|
|
||||||
|
|
||||||
- name: Create Job Template 1
|
- name: Create Job Template 1
|
||||||
tower_job_template:
|
tower_job_template:
|
||||||
@@ -50,6 +61,71 @@
|
|||||||
that:
|
that:
|
||||||
- "result is changed"
|
- "result is changed"
|
||||||
|
|
||||||
|
- name: Add a credential to this JT
|
||||||
|
tower_job_template:
|
||||||
|
name: "{{ jt1 }}"
|
||||||
|
project: "{{ proj1 }}"
|
||||||
|
playbook: hello_world.yml
|
||||||
|
credentials:
|
||||||
|
- "{{ cred1 }}"
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- "result is changed"
|
||||||
|
|
||||||
|
- name: Try to add the same credential to this JT
|
||||||
|
tower_job_template:
|
||||||
|
name: "{{ jt1 }}"
|
||||||
|
project: "{{ proj1 }}"
|
||||||
|
playbook: hello_world.yml
|
||||||
|
credentials:
|
||||||
|
- "{{ cred1 }}"
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- "result is not changed"
|
||||||
|
|
||||||
|
- name: Add another credential to this JT
|
||||||
|
tower_job_template:
|
||||||
|
name: "{{ jt1 }}"
|
||||||
|
project: "{{ proj1 }}"
|
||||||
|
playbook: hello_world.yml
|
||||||
|
credentials:
|
||||||
|
- "{{ cred1 }}"
|
||||||
|
- "{{ cred2 }}"
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- "result is changed"
|
||||||
|
|
||||||
|
- name: Remove a credential for this JT
|
||||||
|
tower_job_template:
|
||||||
|
name: "{{ jt1 }}"
|
||||||
|
project: "{{ proj1 }}"
|
||||||
|
playbook: hello_world.yml
|
||||||
|
credentials:
|
||||||
|
- "{{ cred1 }}"
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- "result is changed"
|
||||||
|
|
||||||
|
- name: Remove all credentials from this JT
|
||||||
|
tower_job_template:
|
||||||
|
name: "{{ jt1 }}"
|
||||||
|
project: "{{ proj1 }}"
|
||||||
|
playbook: hello_world.yml
|
||||||
|
credentials: []
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- "result is changed"
|
||||||
|
|
||||||
# This doesnt work if you include the credentials parameter
|
# This doesnt work if you include the credentials parameter
|
||||||
- name: Delete Job Template 1
|
- name: Delete Job Template 1
|
||||||
tower_job_template:
|
tower_job_template:
|
||||||
@@ -73,6 +149,8 @@
|
|||||||
playbook: hello_world.yml
|
playbook: hello_world.yml
|
||||||
credential: "{{ cred3 }}"
|
credential: "{{ cred3 }}"
|
||||||
job_type: run
|
job_type: run
|
||||||
|
labels:
|
||||||
|
- "{{ lab1 }}"
|
||||||
state: present
|
state: present
|
||||||
register: result
|
register: result
|
||||||
|
|
||||||
@@ -80,6 +158,88 @@
|
|||||||
that:
|
that:
|
||||||
- "result is changed"
|
- "result is changed"
|
||||||
|
|
||||||
|
- name: Add survey to Job Template 2
|
||||||
|
tower_job_template:
|
||||||
|
name: "{{ jt2 }}"
|
||||||
|
survey_enabled: True
|
||||||
|
survey_spec:
|
||||||
|
name: ""
|
||||||
|
description: ""
|
||||||
|
spec:
|
||||||
|
- question_name: "Q1"
|
||||||
|
question_description: "The first question"
|
||||||
|
required: True
|
||||||
|
type: "text"
|
||||||
|
variable: "q1"
|
||||||
|
min: 5
|
||||||
|
max: 15
|
||||||
|
default: "hello"
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- "result is changed"
|
||||||
|
|
||||||
|
- name: Re Add survey to Job Template 2
|
||||||
|
tower_job_template:
|
||||||
|
name: "{{ jt2 }}"
|
||||||
|
survey_enabled: True
|
||||||
|
survey_spec:
|
||||||
|
name: ""
|
||||||
|
description: ""
|
||||||
|
spec:
|
||||||
|
- question_name: "Q1"
|
||||||
|
question_description: "The first question"
|
||||||
|
required: True
|
||||||
|
type: "text"
|
||||||
|
variable: "q1"
|
||||||
|
min: 5
|
||||||
|
max: 15
|
||||||
|
default: "hello"
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- "result is not changed"
|
||||||
|
|
||||||
|
- name: Add question to survey to Job Template 2
|
||||||
|
tower_job_template:
|
||||||
|
name: "{{ jt2 }}"
|
||||||
|
survey_enabled: True
|
||||||
|
survey_spec:
|
||||||
|
name: ""
|
||||||
|
description: ""
|
||||||
|
spec:
|
||||||
|
- question_name: "Q1"
|
||||||
|
question_description: "The first question"
|
||||||
|
required: True
|
||||||
|
type: "text"
|
||||||
|
variable: "q1"
|
||||||
|
min: 5
|
||||||
|
max: 15
|
||||||
|
default: "hello"
|
||||||
|
choices: ""
|
||||||
|
- question_name: "Q2"
|
||||||
|
type: "text"
|
||||||
|
variable: "q2"
|
||||||
|
required: False
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- "result is changed"
|
||||||
|
|
||||||
|
- name: Remove survey from Job Template 2
|
||||||
|
tower_job_template:
|
||||||
|
name: "{{ jt2 }}"
|
||||||
|
survey_enabled: False
|
||||||
|
survey_spec: {}
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- "result is changed"
|
||||||
|
|
||||||
- name: Delete Job Template 2
|
- name: Delete Job Template 2
|
||||||
tower_job_template:
|
tower_job_template:
|
||||||
name: "{{ jt2 }}"
|
name: "{{ jt2 }}"
|
||||||
@@ -124,3 +284,5 @@
|
|||||||
organization: Default
|
organization: Default
|
||||||
kind: ssh
|
kind: ssh
|
||||||
state: absent
|
state: absent
|
||||||
|
|
||||||
|
# You can't delete a label directly so no cleanup needed
|
||||||
|
|||||||
Reference in New Issue
Block a user