2017-02-20 23:18:04 +03:00
#!/usr/bin/python
2017-05-11 19:26:36 +03:00
# coding: utf-8 -*-
2017-02-20 23:18:04 +03:00
# (c) 2017, Wayne Witzel III <wayne@riotousliving.com>
Remove wildcard imports
Made the following changes:
* Removed wildcard imports
* Replaced long form of GPL header with short form
* Removed get_exception usage
* Added from __future__ boilerplate
* Adjust division operator to // where necessary
For the following files:
* web_infrastructure modules
* system modules
* linode, lxc, lxd, atomic, cloudscale, dimensiondata, ovh, packet,
profitbricks, pubnub, smartos, softlayer, univention modules
* compat dirs (disabled as its used intentionally)
2017-07-28 08:55:24 +03:00
# 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
2017-02-20 23:18:04 +03:00
2017-08-16 06:16:38 +03:00
ANSIBLE_METADATA = { ' metadata_version ' : ' 1.1 ' ,
2017-03-14 19:07:22 +03:00
' status ' : [ ' preview ' ] ,
' supported_by ' : ' community ' }
2017-02-20 23:18:04 +03:00
DOCUMENTATION = '''
- - -
module : tower_job_template
2017-03-09 19:20:25 +03:00
author : " Wayne Witzel III (@wwitzel3) "
2017-02-20 23:18:04 +03:00
version_added : " 2.3 "
2018-03-15 06:58:50 +03:00
short_description : create , update , or destroy Ansible Tower job template .
2017-02-20 23:18:04 +03:00
description :
- Create , update , or destroy Ansible Tower job templates . See
U ( https : / / www . ansible . com / tower ) for an overview .
options :
name :
description :
2018-06-05 08:33:51 +03:00
- Name to use for the job template .
2017-02-20 23:18:04 +03:00
required : True
description :
description :
2018-03-15 06:58:50 +03:00
- Description to use for the job template .
2017-02-20 23:18:04 +03:00
job_type :
description :
2018-06-05 08:33:51 +03:00
- The job type to use for the job template .
2017-02-20 23:18:04 +03:00
required : True
choices : [ " run " , " check " , " scan " ]
inventory :
description :
2018-06-05 08:33:51 +03:00
- Name of the inventory to use for the job template .
2017-02-20 23:18:04 +03:00
project :
description :
2018-06-05 08:33:51 +03:00
- Name of the project to use for the job template .
2017-02-20 23:18:04 +03:00
required : True
playbook :
description :
2018-06-05 08:33:51 +03:00
- Path to the playbook to use for the job template within the project provided .
2017-02-20 23:18:04 +03:00
required : True
2018-06-05 08:33:51 +03:00
credential :
2017-02-20 23:18:04 +03:00
description :
2018-06-05 08:33:51 +03:00
- Name of the credential to use for the job template .
version_added : 2.7
vault_credential :
2017-02-20 23:18:04 +03:00
description :
2018-06-05 08:33:51 +03:00
- Name of the vault credential to use for the job template .
version_added : 2.7
2017-02-20 23:18:04 +03:00
forks :
description :
- The number of parallel or simultaneous processes to use while executing the playbook .
limit :
description :
- A host pattern to further constrain the list of hosts managed or affected by the playbook
verbosity :
description :
2018-06-05 08:33:51 +03:00
- Control the output level Ansible produces as the playbook runs . 0 - Normal , 1 - Verbose , 2 - More Verbose , 3 - Debug , 4 - Connection Debug .
choices : [ 0 , 1 , 2 , 3 , 4 ]
default : 0
extra_vars_path :
description :
- Path to the C ( extra_vars ) YAML file .
2017-02-20 23:18:04 +03:00
job_tags :
description :
2018-06-05 08:33:51 +03:00
- Comma separated list of the tags to use for the job template .
force_handlers_enabled :
description :
- Enable forcing playbook handlers to run even if a task fails .
version_added : 2.7
type : bool
default : ' no '
2017-02-20 23:18:04 +03:00
skip_tags :
description :
2018-06-05 08:33:51 +03:00
- Comma separated list of the tags to skip for the job template .
start_at_task :
description :
- Start the playbook at the task matching this name .
version_added : 2.7
2019-02-01 10:18:18 +03:00
diff_mode_enabled :
description :
- Enable diff mode for the job template .
version_added : 2.7
type : bool
default : ' no '
2018-06-05 08:33:51 +03:00
fact_caching_enabled :
description :
- Enable use of fact caching for the job template .
version_added : 2.7
type : bool
default : ' no '
2017-02-20 23:18:04 +03:00
host_config_key :
description :
- Allow provisioning callbacks using this host config key .
2018-06-05 08:33:51 +03:00
ask_diff_mode :
2017-02-20 23:18:04 +03:00
description :
2018-06-05 08:33:51 +03:00
- Prompt user to enable diff mode ( show changes ) to files when supported by modules .
version_added : 2.7
type : bool
default : ' no '
2017-02-20 23:18:04 +03:00
ask_extra_vars :
description :
2018-06-05 08:33:51 +03:00
- Prompt user for ( extra_vars ) on launch .
type : bool
default : ' no '
ask_limit :
description :
- Prompt user for a limit on launch .
version_added : 2.7
2018-03-16 00:15:24 +03:00
type : bool
default : ' no '
2017-02-20 23:18:04 +03:00
ask_tags :
description :
- Prompt user for job tags on launch .
2018-03-16 00:15:24 +03:00
type : bool
default : ' no '
2018-06-05 08:33:51 +03:00
ask_skip_tags :
description :
- Prompt user for job tags to skip on launch .
version_added : 2.7
type : bool
default : ' no '
2017-02-20 23:18:04 +03:00
ask_job_type :
description :
- Prompt user for job type on launch .
2018-03-16 00:15:24 +03:00
type : bool
default : ' no '
2018-06-05 08:33:51 +03:00
ask_verbosity :
description :
- Prompt user to choose a verbosity level on launch .
version_added : 2.7
type : bool
default : ' no '
2017-02-20 23:18:04 +03:00
ask_inventory :
description :
2019-09-12 17:53:05 +03:00
- Prompt user for inventory on launch .
2018-03-16 00:15:24 +03:00
type : bool
default : ' no '
2017-02-20 23:18:04 +03:00
ask_credential :
description :
- Prompt user for credential on launch .
2018-03-16 00:15:24 +03:00
type : bool
default : ' no '
2018-06-05 08:33:51 +03:00
survey_enabled :
description :
- Enable a survey on the job template .
version_added : 2.7
type : bool
default : ' no '
2018-11-07 15:09:40 +03:00
survey_spec :
description :
- JSON / YAML dict formatted survey definition .
version_added : 2.8
type : dict
required : False
2017-02-20 23:18:04 +03:00
become_enabled :
description :
2018-03-15 06:58:50 +03:00
- Activate privilege escalation .
2018-03-16 00:15:24 +03:00
type : bool
default : ' no '
2018-06-05 08:33:51 +03:00
concurrent_jobs_enabled :
description :
- Allow simultaneous runs of the job template .
version_added : 2.7
type : bool
default : ' no '
2017-02-20 23:18:04 +03:00
state :
description :
- Desired state of the resource .
default : " present "
choices : [ " present " , " absent " ]
2017-10-02 23:21:24 +03:00
extends_documentation_fragment : tower
2018-11-07 15:09:40 +03:00
notes :
- JSON for survey_spec can be found in Tower API Documentation . See
U ( https : / / docs . ansible . com / ansible - tower / latest / html / towerapi / api_ref . html #/Job_Templates/Job_Templates_job_templates_survey_spec_create)
for POST operation payload example .
2017-02-20 23:18:04 +03:00
'''
EXAMPLES = '''
- name : Create tower Ping job template
tower_job_template :
2018-06-05 08:33:51 +03:00
name : " Ping "
job_type : " run "
inventory : " Local "
project : " Demo "
playbook : " ping.yml "
credential : " Local "
state : " present "
2017-02-20 23:18:04 +03:00
tower_config_file : " ~/tower_cli.cfg "
2018-11-07 15:09:40 +03:00
survey_enabled : yes
survey_spec : " {{ lookup( ' file ' , ' my_survey.json ' ) }} "
2017-02-20 23:18:04 +03:00
'''
2018-08-02 18:17:39 +03:00
from ansible . module_utils . ansible_tower import TowerModule , tower_auth_config , tower_check_mode
2017-10-02 23:21:24 +03:00
2017-02-20 23:18:04 +03:00
try :
import tower_cli
2019-01-09 17:31:21 +03:00
import tower_cli . exceptions as exc
2017-02-20 23:18:04 +03:00
from tower_cli . conf import settings
except ImportError :
2017-10-02 23:21:24 +03:00
pass
2017-02-20 23:18:04 +03:00
def update_fields ( p ) :
''' This updates the module field names
to match the field names tower - cli expects to make
calling of the modify / delete methods easier .
'''
params = p . copy ( )
field_map = {
2018-06-05 08:33:51 +03:00
' fact_caching_enabled ' : ' use_fact_cache ' ,
' ask_diff_mode ' : ' ask_diff_mode_on_launch ' ,
2017-02-20 23:18:04 +03:00
' ask_extra_vars ' : ' ask_variables_on_launch ' ,
2017-05-11 19:26:36 +03:00
' ask_limit ' : ' ask_limit_on_launch ' ,
2017-02-20 23:18:04 +03:00
' ask_tags ' : ' ask_tags_on_launch ' ,
2018-06-05 08:33:51 +03:00
' 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 ' ,
2017-02-20 23:18:04 +03:00
' ask_job_type ' : ' ask_job_type_on_launch ' ,
2018-06-05 08:33:51 +03:00
' diff_mode_enabled ' : ' diff_mode ' ,
' concurrent_jobs_enabled ' : ' allow_simultaneous ' ,
' force_handlers_enabled ' : ' force_handlers ' ,
2017-02-20 23:18:04 +03:00
}
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_path ' )
if extra_vars is not None :
2017-06-02 16:29:53 +03:00
params_update [ ' extra_vars ' ] = [ ' @ ' + extra_vars ]
2017-02-20 23:18:04 +03:00
params . update ( params_update )
return params
def update_resources ( module , p ) :
params = p . copy ( )
identity_map = {
' project ' : ' name ' ,
' inventory ' : ' name ' ,
2018-06-05 08:33:51 +03:00
' credential ' : ' name ' ,
' vault_credential ' : ' name ' ,
2017-02-20 23:18:04 +03:00
}
2017-05-11 19:26:36 +03:00
for k , v in identity_map . items ( ) :
2017-02-20 23:18:04 +03:00
try :
if params [ k ] :
key = ' credential ' if ' _credential ' in k else k
2017-05-11 19:26:36 +03:00
result = tower_cli . get_resource ( key ) . get ( * * { v : params [ k ] } )
2017-02-20 23:18:04 +03:00
params [ k ] = result [ ' id ' ]
2018-06-05 08:33:51 +03:00
elif k in params :
# unset empty parameters to avoid ValueError: invalid literal for int() with base 10: ''
del ( params [ k ] )
2017-02-20 23:18:04 +03:00
except ( exc . NotFound ) as excinfo :
module . fail_json ( msg = ' Failed to update job template: {0} ' . format ( excinfo ) , changed = False )
return params
def main ( ) :
2018-08-02 18:17:39 +03:00
argument_spec = dict (
2017-10-02 23:21:24 +03:00
name = dict ( required = True ) ,
2018-06-05 08:33:51 +03:00
description = dict ( default = ' ' ) ,
2017-10-02 23:21:24 +03:00
job_type = dict ( choices = [ ' run ' , ' check ' , ' scan ' ] , required = True ) ,
2018-06-05 08:33:51 +03:00
inventory = dict ( default = ' ' ) ,
2017-10-02 23:21:24 +03:00
project = dict ( required = True ) ,
playbook = dict ( required = True ) ,
2018-06-05 08:33:51 +03:00
credential = dict ( default = ' ' ) ,
vault_credential = dict ( default = ' ' ) ,
2017-10-02 23:21:24 +03:00
forks = dict ( type = ' int ' ) ,
2018-06-05 08:33:51 +03:00
limit = dict ( default = ' ' ) ,
verbosity = dict ( type = ' int ' , choices = [ 0 , 1 , 2 , 3 , 4 ] , default = 0 ) ,
2017-10-02 23:21:24 +03:00
extra_vars_path = dict ( type = ' path ' , required = False ) ,
2018-06-05 08:33:51 +03:00
job_tags = dict ( default = ' ' ) ,
force_handlers_enabled = dict ( type = ' bool ' , default = False ) ,
skip_tags = dict ( default = ' ' ) ,
start_at_task = dict ( default = ' ' ) ,
timeout = dict ( type = ' int ' , default = 0 ) ,
fact_caching_enabled = dict ( type = ' bool ' , default = False ) ,
host_config_key = dict ( default = ' ' ) ,
ask_diff_mode = dict ( type = ' bool ' , default = False ) ,
2017-10-02 23:21:24 +03:00
ask_extra_vars = dict ( type = ' bool ' , default = False ) ,
ask_limit = dict ( type = ' bool ' , default = False ) ,
ask_tags = dict ( type = ' bool ' , default = False ) ,
2018-06-05 08:33:51 +03:00
ask_skip_tags = dict ( type = ' bool ' , default = False ) ,
2017-10-02 23:21:24 +03:00
ask_job_type = dict ( type = ' bool ' , default = False ) ,
2018-06-05 08:33:51 +03:00
ask_verbosity = dict ( type = ' bool ' , default = False ) ,
2017-10-02 23:21:24 +03:00
ask_inventory = dict ( type = ' bool ' , default = False ) ,
ask_credential = dict ( type = ' bool ' , default = False ) ,
2018-06-05 08:33:51 +03:00
survey_enabled = dict ( type = ' bool ' , default = False ) ,
2018-11-07 15:09:40 +03:00
survey_spec = dict ( type = ' dict ' , required = False ) ,
2017-10-02 23:21:24 +03:00
become_enabled = dict ( type = ' bool ' , default = False ) ,
2018-06-05 08:33:51 +03:00
diff_mode_enabled = dict ( type = ' bool ' , default = False ) ,
concurrent_jobs_enabled = dict ( type = ' bool ' , default = False ) ,
2017-10-02 23:21:24 +03:00
state = dict ( choices = [ ' present ' , ' absent ' ] , default = ' present ' ) ,
2018-08-02 18:17:39 +03:00
)
2017-10-02 23:21:24 +03:00
2018-08-02 18:17:39 +03:00
module = TowerModule ( argument_spec = argument_spec , supports_check_mode = True )
2017-02-20 23:18:04 +03:00
name = module . params . get ( ' name ' )
2018-05-26 02:03:12 +03:00
state = module . params . pop ( ' state ' )
2017-02-20 23:18:04 +03:00
json_output = { ' job_template ' : name , ' state ' : state }
tower_auth = tower_auth_config ( module )
with settings . runtime_values ( * * tower_auth ) :
tower_check_mode ( module )
jt = tower_cli . get_resource ( ' job_template ' )
params = update_resources ( module , module . params )
params = update_fields ( params )
params [ ' create_on_missing ' ] = True
try :
if state == ' present ' :
result = jt . modify ( * * params )
json_output [ ' id ' ] = result [ ' id ' ]
elif state == ' absent ' :
result = jt . delete ( * * params )
2019-03-06 14:18:43 +03:00
except ( exc . ConnectionError , exc . BadRequest , exc . NotFound , exc . AuthError ) as excinfo :
2017-02-20 23:18:04 +03:00
module . fail_json ( msg = ' Failed to update job template: {0} ' . format ( excinfo ) , changed = False )
json_output [ ' changed ' ] = result [ ' changed ' ]
module . exit_json ( * * json_output )
if __name__ == ' __main__ ' :
main ( )