2017-02-17 19:19:33 +03:00
#!/usr/bin/python
2017-05-11 19:26:36 +03:00
# coding: utf-8 -*-
2017-02-17 19:19:33 +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-17 19:19:33 +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-17 19:19:33 +03:00
DOCUMENTATION = '''
- - -
module : tower_project
2017-03-09 19:20:25 +03:00
author : " Wayne Witzel III (@wwitzel3) "
2017-02-17 19:19:33 +03:00
version_added : " 2.3 "
short_description : create , update , or destroy Ansible Tower projects
description :
- Create , update , or destroy Ansible Tower projects . See
U ( https : / / www . ansible . com / tower ) for an overview .
options :
name :
description :
- Name to use for the project .
required : True
description :
description :
- Description to use for the project .
scm_type :
description :
2018-03-15 06:58:50 +03:00
- Type of SCM resource .
2017-02-17 19:19:33 +03:00
choices : [ " manual " , " git " , " hg " , " svn " ]
2018-03-16 00:15:24 +03:00
default : " manual "
2017-02-17 19:19:33 +03:00
scm_url :
description :
2018-03-15 06:58:50 +03:00
- URL of SCM resource .
2017-02-17 19:19:33 +03:00
local_path :
description :
- The server playbook directory for manual projects .
scm_branch :
description :
2018-03-15 06:58:50 +03:00
- The branch to use for the SCM resource .
2017-02-17 19:19:33 +03:00
scm_credential :
description :
2018-03-15 06:58:50 +03:00
- Name of the credential to use with this SCM resource .
2017-02-17 19:19:33 +03:00
scm_clean :
description :
- Remove local modifications before updating .
2018-03-16 00:15:24 +03:00
type : bool
default : ' no '
2017-02-17 19:19:33 +03:00
scm_delete_on_update :
description :
- Remove the repository completely before updating .
2018-03-16 00:15:24 +03:00
type : bool
default : ' no '
2017-02-17 19:19:33 +03:00
scm_update_on_launch :
description :
- Before an update to the local repository before launching a job with this project .
2018-03-16 00:15:24 +03:00
type : bool
default : ' no '
2019-01-25 16:42:14 +03:00
scm_update_cache_timeout :
version_added : " 2.8 "
description :
- Cache Timeout to cache prior project syncs for a certain number of seconds .
Only valid if scm_update_on_launch is to True , otherwise ignored .
default : 0
job_timeout :
version_added : " 2.8 "
description :
- The amount of time ( in seconds ) to run before the SCM Update is canceled . A value of 0 means no timeout .
default : 0
custom_virtualenv :
version_added : " 2.8 "
description :
- Local absolute file path containing a custom Python virtualenv to use
2017-02-17 19:19:33 +03:00
organization :
description :
- Primary key of organization for project .
state :
description :
- Desired state of the resource .
default : " present "
choices : [ " present " , " absent " ]
2019-09-18 15:43:36 +03:00
extends_documentation_fragment : awx . awx . auth
2017-02-17 19:19:33 +03:00
'''
EXAMPLES = '''
- name : Add tower project
tower_project :
name : " Foo "
description : " Foo bar project "
organization : " test "
state : present
tower_config_file : " ~/tower_cli.cfg "
2019-01-25 16:42:14 +03:00
- name : Add Tower Project with cache timeout and custom virtualenv
tower_project :
name : " Foo "
description : " Foo bar project "
organization : " test "
scm_update_on_launch : True
scm_update_cache_timeout : 60
custom_virtualenv : " /var/lib/awx/venv/ansible-2.2 "
state : present
tower_config_file : " ~/tower_cli.cfg "
2017-02-17 19:19:33 +03:00
'''
2019-08-31 00:11:01 +03:00
from . . module_utils . ansible_tower import TowerModule , tower_auth_config , tower_check_mode
2017-10-02 23:21:24 +03:00
2017-02-17 19:19:33 +03:00
try :
import tower_cli
2019-01-09 17:31:21 +03:00
import tower_cli . exceptions as exc
2017-02-17 19:19:33 +03:00
from tower_cli . conf import settings
except ImportError :
2017-10-02 23:21:24 +03:00
pass
2017-02-17 19:19:33 +03:00
def main ( ) :
2018-08-02 18:17:39 +03:00
argument_spec = dict (
2017-10-02 23:21:24 +03:00
name = dict ( ) ,
description = dict ( ) ,
organization = dict ( ) ,
scm_type = dict ( choices = [ ' manual ' , ' git ' , ' hg ' , ' svn ' ] , default = ' manual ' ) ,
scm_url = dict ( ) ,
scm_branch = dict ( ) ,
scm_credential = dict ( ) ,
scm_clean = dict ( type = ' bool ' , default = False ) ,
scm_delete_on_update = dict ( type = ' bool ' , default = False ) ,
scm_update_on_launch = dict ( type = ' bool ' , default = False ) ,
2019-01-25 16:42:14 +03:00
scm_update_cache_timeout = dict ( type = ' int ' , default = 0 ) ,
job_timeout = dict ( type = ' int ' , default = 0 ) ,
custom_virtualenv = dict ( ) ,
2017-10-02 23:21:24 +03:00
local_path = dict ( ) ,
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-17 19:19:33 +03:00
name = module . params . get ( ' name ' )
description = module . params . get ( ' description ' )
organization = module . params . get ( ' organization ' )
scm_type = module . params . get ( ' scm_type ' )
if scm_type == " manual " :
scm_type = " "
scm_url = module . params . get ( ' scm_url ' )
local_path = module . params . get ( ' local_path ' )
scm_branch = module . params . get ( ' scm_branch ' )
scm_credential = module . params . get ( ' scm_credential ' )
scm_clean = module . params . get ( ' scm_clean ' )
scm_delete_on_update = module . params . get ( ' scm_delete_on_update ' )
scm_update_on_launch = module . params . get ( ' scm_update_on_launch ' )
2019-01-25 16:42:14 +03:00
scm_update_cache_timeout = module . params . get ( ' scm_update_cache_timeout ' )
job_timeout = module . params . get ( ' job_timeout ' )
custom_virtualenv = module . params . get ( ' custom_virtualenv ' )
2017-02-17 19:19:33 +03:00
state = module . params . get ( ' state ' )
json_output = { ' project ' : name , ' state ' : state }
tower_auth = tower_auth_config ( module )
with settings . runtime_values ( * * tower_auth ) :
tower_check_mode ( module )
project = tower_cli . get_resource ( ' project ' )
try :
if state == ' present ' :
2017-05-19 01:44:33 +03:00
try :
org_res = tower_cli . get_resource ( ' organization ' )
org = org_res . get ( name = organization )
2019-08-31 00:11:01 +03:00
except exc . NotFound :
2017-05-19 01:44:33 +03:00
module . fail_json ( msg = ' Failed to update project, organization not found: {0} ' . format ( organization ) , changed = False )
2017-02-17 19:19:33 +03:00
2018-07-24 18:18:50 +03:00
if scm_credential :
2018-06-15 13:36:52 +03:00
try :
cred_res = tower_cli . get_resource ( ' credential ' )
2018-12-19 15:01:12 +03:00
try :
cred = cred_res . get ( name = scm_credential )
2019-08-31 00:11:01 +03:00
except tower_cli . exceptions . MultipleResults :
2018-12-19 15:01:12 +03:00
module . warn ( ' Multiple credentials found for {0} , falling back looking in project organization ' . format ( scm_credential ) )
cred = cred_res . get ( name = scm_credential , organization = org [ ' id ' ] )
2018-07-24 18:18:50 +03:00
scm_credential = cred [ ' id ' ]
2019-08-31 00:11:01 +03:00
except exc . NotFound :
2018-06-15 13:36:52 +03:00
module . fail_json ( msg = ' Failed to update project, credential not found: {0} ' . format ( scm_credential ) , changed = False )
2019-01-25 16:42:14 +03:00
if ( scm_update_cache_timeout is not None ) and ( scm_update_on_launch is not True ) :
module . warn ( ' scm_update_cache_timeout will be ignored since scm_update_on_launch was not set to true ' )
2017-02-17 19:19:33 +03:00
result = project . modify ( name = name , description = description ,
organization = org [ ' id ' ] ,
scm_type = scm_type , scm_url = scm_url , local_path = local_path ,
2018-07-24 18:18:50 +03:00
scm_branch = scm_branch , scm_clean = scm_clean , credential = scm_credential ,
2017-02-17 19:19:33 +03:00
scm_delete_on_update = scm_delete_on_update ,
scm_update_on_launch = scm_update_on_launch ,
2019-01-25 16:42:14 +03:00
scm_update_cache_timeout = scm_update_cache_timeout ,
job_timeout = job_timeout ,
custom_virtualenv = custom_virtualenv ,
2017-02-17 19:19:33 +03:00
create_on_missing = True )
json_output [ ' id ' ] = result [ ' id ' ]
elif state == ' absent ' :
result = project . delete ( name = name )
2019-03-06 14:18:43 +03:00
except ( exc . ConnectionError , exc . BadRequest , exc . AuthError ) as excinfo :
2017-02-17 19:19:33 +03:00
module . fail_json ( msg = ' Failed to update project: {0} ' . format ( excinfo ) , changed = False )
json_output [ ' changed ' ] = result [ ' changed ' ]
module . exit_json ( * * json_output )
if __name__ == ' __main__ ' :
main ( )