2018-08-29 20:58:35 +03:00
#!/usr/bin/python
# coding: utf-8 -*-
2018-09-19 17:02:27 +03:00
# Copyright: (c) 2018, Adrien Fleury <fleu42@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
2018-08-29 20:58:35 +03:00
from __future__ import absolute_import , division , print_function
__metaclass__ = type
ANSIBLE_METADATA = { ' status ' : [ ' preview ' ] ,
' supported_by ' : ' community ' ,
' metadata_version ' : ' 1.1 ' }
DOCUMENTATION = '''
- - -
module : tower_workflow_template
author : " Adrien Fleury (@fleu42) "
version_added : " 2.7 "
short_description : create , update , or destroy Ansible Tower workflow template .
description :
2020-03-14 06:05:01 +03:00
- A tower - cli based module for CRUD actions on workflow job templates .
- Enables use of the old schema functionality .
- Not updated for new features , convert to the modules for
workflow_job_template and workflow_job_template node instead .
2018-08-29 20:58:35 +03:00
options :
allow_simultaneous :
description :
- If enabled , simultaneous runs of this job template will be allowed .
type : bool
2019-06-20 20:55:37 +03:00
ask_extra_vars :
description :
- Prompt user for ( extra_vars ) on launch .
type : bool
version_added : " 2.9 "
ask_inventory :
description :
2019-09-12 17:53:05 +03:00
- Prompt user for inventory on launch .
2019-06-20 20:55:37 +03:00
type : bool
version_added : " 2.9 "
2018-08-29 20:58:35 +03:00
description :
description :
- The description to use for the workflow .
2019-09-30 23:01:44 +03:00
type : str
2018-08-29 20:58:35 +03:00
extra_vars :
description :
2018-09-19 17:02:27 +03:00
- Extra variables used by Ansible in YAML or key = value format .
2020-03-05 19:05:46 +03:00
type : dict
2019-06-20 20:55:37 +03:00
inventory :
description :
- Name of the inventory to use for the job template .
version_added : " 2.9 "
2019-09-30 23:01:44 +03:00
type : str
2018-08-29 20:58:35 +03:00
name :
description :
- The name to use for the workflow .
required : True
2019-09-30 23:01:44 +03:00
type : str
2018-08-29 20:58:35 +03:00
organization :
description :
- The organization the workflow is linked to .
2019-09-30 23:01:44 +03:00
type : str
2018-08-29 20:58:35 +03:00
schema :
description :
- >
The schema is a JSON - or YAML - formatted string defining the
hierarchy structure that connects the nodes . Refer to Tower
documentation for more information .
2020-03-05 19:05:46 +03:00
type : list
elements : dict
2018-08-29 20:58:35 +03:00
survey_enabled :
description :
- Setting that variable will prompt the user for job type on the
workflow launch .
type : bool
survey :
description :
- The definition of the survey associated to the workflow .
2020-03-14 06:05:01 +03:00
type : dict
required : false
2018-08-29 20:58:35 +03:00
state :
description :
- Desired state of the resource .
default : " present "
choices : [ " present " , " absent " ]
2019-09-30 23:01:44 +03:00
type : str
2020-02-17 17:20:54 +03:00
requirements :
- ansible - tower - cli > = 3.0 .2
2019-09-18 15:43:36 +03:00
extends_documentation_fragment : awx . awx . auth
2018-08-29 20:58:35 +03:00
'''
EXAMPLES = '''
- tower_workflow_template :
name : Workflow Template
2019-09-12 17:53:05 +03:00
description : My very first Workflow Template
2018-08-29 20:58:35 +03:00
organization : My optional Organization
2019-01-09 17:21:26 +03:00
schema : " {{ lookup( ' file ' , ' my_workflow.json ' ) }} "
2018-08-29 20:58:35 +03:00
2019-11-13 19:11:36 +03:00
- tower_workflow_template :
2018-08-29 20:58:35 +03:00
name : Workflow Template
state : absent
'''
RETURN = ''' # '''
2019-08-31 00:11:01 +03:00
from . . module_utils . ansible_tower import (
2018-08-29 20:58:35 +03:00
TowerModule ,
tower_auth_config ,
tower_check_mode
)
2020-03-05 19:05:46 +03:00
import json
2018-08-29 20:58:35 +03:00
try :
import tower_cli
import tower_cli . exceptions as exc
from tower_cli . conf import settings
except ImportError :
pass
def main ( ) :
argument_spec = dict (
name = dict ( required = True ) ,
description = dict ( required = False ) ,
2020-03-05 19:05:46 +03:00
extra_vars = dict ( type = ' dict ' , required = False ) ,
2018-08-29 20:58:35 +03:00
organization = dict ( required = False ) ,
allow_simultaneous = dict ( type = ' bool ' , required = False ) ,
2020-03-05 19:05:46 +03:00
schema = dict ( type = ' list ' , elements = ' dict ' , required = False ) ,
2020-03-14 06:05:01 +03:00
survey = dict ( type = ' dict ' ) ,
2018-08-29 20:58:35 +03:00
survey_enabled = dict ( type = ' bool ' , required = False ) ,
2019-06-20 20:55:37 +03:00
inventory = dict ( required = False ) ,
ask_inventory = dict ( type = ' bool ' , required = False ) ,
ask_extra_vars = dict ( type = ' bool ' , required = False ) ,
2018-08-29 20:58:35 +03:00
state = dict ( choices = [ ' present ' , ' absent ' ] , default = ' present ' ) ,
)
module = TowerModule (
argument_spec = argument_spec ,
supports_check_mode = False
)
2020-03-14 06:05:01 +03:00
module . deprecate ( msg = (
" This module is replaced by the combination of tower_workflow_job_template and "
" tower_workflow_job_template_node. This uses the old tower-cli and wll be "
" removed in 2022. "
) , version = ' 4.2.0 ' )
2018-08-29 20:58:35 +03:00
name = module . params . get ( ' name ' )
state = module . params . get ( ' state ' )
schema = None
if module . params . get ( ' schema ' ) :
schema = module . params . get ( ' schema ' )
if schema and state == ' absent ' :
module . fail_json (
msg = ' Setting schema when state is absent is not allowed ' ,
changed = False
)
json_output = { ' workflow_template ' : name , ' state ' : state }
tower_auth = tower_auth_config ( module )
with settings . runtime_values ( * * tower_auth ) :
tower_check_mode ( module )
wfjt_res = tower_cli . get_resource ( ' workflow ' )
params = { }
params [ ' name ' ] = name
if module . params . get ( ' description ' ) :
params [ ' description ' ] = module . params . get ( ' description ' )
if module . params . get ( ' organization ' ) :
organization_res = tower_cli . get_resource ( ' organization ' )
try :
organization = organization_res . get (
name = module . params . get ( ' organization ' ) )
params [ ' organization ' ] = organization [ ' id ' ]
except exc . NotFound as excinfo :
module . fail_json (
msg = ' Failed to update organization source, '
' organization not found: {0} ' . format ( excinfo ) ,
changed = False
)
if module . params . get ( ' survey ' ) :
params [ ' survey_spec ' ] = module . params . get ( ' survey ' )
2019-06-20 20:55:37 +03:00
if module . params . get ( ' ask_extra_vars ' ) :
params [ ' ask_variables_on_launch ' ] = module . params . get ( ' ask_extra_vars ' )
if module . params . get ( ' ask_inventory ' ) :
params [ ' ask_inventory_on_launch ' ] = module . params . get ( ' ask_inventory ' )
2020-03-05 19:05:46 +03:00
for key in ( ' allow_simultaneous ' , ' inventory ' ,
2019-06-20 20:55:37 +03:00
' survey_enabled ' , ' description ' ) :
2018-08-29 20:58:35 +03:00
if module . params . get ( key ) :
params [ key ] = module . params . get ( key )
2020-03-05 19:05:46 +03:00
# Special treatment for tower-cli extra_vars
extra_vars = module . params . get ( ' extra_vars ' )
if extra_vars :
params [ ' extra_vars ' ] = [ json . dumps ( extra_vars ) ]
2018-08-29 20:58:35 +03:00
try :
if state == ' present ' :
params [ ' create_on_missing ' ] = True
result = wfjt_res . modify ( * * params )
json_output [ ' id ' ] = result [ ' id ' ]
if schema :
2020-03-05 19:05:46 +03:00
wfjt_res . schema ( result [ ' id ' ] , json . dumps ( schema ) )
2018-08-29 20:58:35 +03:00
elif state == ' absent ' :
params [ ' fail_on_missing ' ] = False
result = wfjt_res . delete ( * * params )
2019-03-06 14:18:43 +03:00
except ( exc . ConnectionError , exc . BadRequest , exc . AuthError ) as excinfo :
2018-08-29 20:58:35 +03:00
module . fail_json ( msg = ' Failed to update workflow template: \
{ 0 } ' .format(excinfo), changed=False)
json_output [ ' changed ' ] = result [ ' changed ' ]
module . exit_json ( * * json_output )
if __name__ == ' __main__ ' :
main ( )