2017-02-15 19:59:03 +03:00
#!/usr/bin/python
2017-05-11 19:26:36 +03:00
# coding: utf-8 -*-
2017-02-15 19:59:03 +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-15 19:59:03 +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-15 19:59:03 +03:00
DOCUMENTATION = '''
- - -
module : tower_credential
2017-03-09 19:20:25 +03:00
author : " Wayne Witzel III (@wwitzel3) "
2017-02-15 19:59:03 +03:00
version_added : " 2.3 "
short_description : create , update , or destroy Ansible Tower credential .
description :
- Create , update , or destroy Ansible Tower credentials . See
U ( https : / / www . ansible . com / tower ) for an overview .
options :
name :
description :
- The name to use for the credential .
required : True
description :
description :
- The description to use for the credential .
user :
description :
- User that should own this credential .
required : False
default : null
team :
description :
- Team that should own this credential .
required : False
default : null
project :
description :
- Project that should for this credential .
required : False
default : null
organization :
description :
- Organization that should own the credential .
required : False
default : null
kind :
description :
- Type of credential being added .
required : True
choices : [ " ssh " , " net " , " scm " , " aws " , " rax " , " vmware " , " satellite6 " , " cloudforms " , " gce " , " azure " , " azure_rm " , " openstack " ]
host :
description :
- Host for this credential .
required : False
default : null
username :
description :
- Username for this credential . access_key for AWS .
required : False
default : null
password :
description :
- Password for this credential . Use ASK for prompting . secret_key for AWS . api_key for RAX .
required : False
default : null
ssh_key_data :
description :
- Path to SSH private key .
required : False
default : null
ssh_key_unlock :
description :
- Unlock password for ssh_key . Use ASK for prompting .
authorize :
description :
- Should use authroize for net type .
required : False
default : False
authorize_password :
description :
- Password for net credentials that require authroize .
required : False
default : null
client :
description :
- Client or application ID for azure_rm type .
required : False
default : null
secret :
description :
- Secret token for azure_rm type .
required : False
default : null
subscription :
description :
- Subscription ID for azure_rm type .
required : False
default : null
tenant :
description :
- Tenant ID for azure_rm type .
required : False
default : null
domain :
description :
- Domain for openstack type .
required : False
default : null
become_method :
description :
- Become method to Use for privledge escalation .
required : False
2017-05-20 00:24:54 +03:00
choices : [ " None " , " sudo " , " su " , " pbrun " , " pfexec " , " pmrun " ]
2017-02-15 19:59:03 +03:00
default : " None "
become_username :
description :
- Become username . Use ASK for prompting .
required : False
default : null
become_password :
description :
- Become password . Use ASK for prompting .
required : False
default : null
vault_password :
description :
- Valut password . Use ASK for prompting .
state :
description :
- Desired state of the resource .
required : False
default : " present "
choices : [ " present " , " absent " ]
2017-10-02 23:21:24 +03:00
extends_documentation_fragment : tower
2017-02-15 19:59:03 +03:00
'''
EXAMPLES = '''
- name : Add tower credential
tower_credential :
name : Team Name
description : Team Description
organization : test - org
state : present
tower_config_file : " ~/tower_cli.cfg "
'''
2017-10-02 23:21:24 +03:00
import os
from ansible . module_utils . ansible_tower import tower_argument_spec , tower_auth_config , tower_check_mode , HAS_TOWER_CLI
2017-02-15 19:59:03 +03:00
try :
import tower_cli
import tower_cli . utils . exceptions as exc
from tower_cli . conf import settings
except ImportError :
2017-10-02 23:21:24 +03:00
pass
2017-02-15 19:59:03 +03:00
2018-02-22 19:17:56 +03:00
KIND_CHOICES = {
' ssh ' : ' Machine ' ,
' vault ' : ' Ansible Vault ' ,
' net ' : ' Network ' ,
' scm ' : ' Source Control ' ,
' aws ' : ' Amazon Web Services ' ,
' vmware ' : ' VMware vCenter ' ,
' satellite6 ' : ' Red Hat Satellite 6 ' ,
' cloudforms ' : ' Red Hat CloudForms ' ,
' gce ' : ' Google Compute Engine ' ,
' azure_rm ' : ' Microsoft Azure Resource Manager ' ,
' openstack ' : ' OpenStack ' ,
' rhv ' : ' Red Hat Virtualization ' ,
' insights ' : ' Insights ' ,
' tower ' : ' Ansible Tower ' ,
}
def credential_type_for_v1_kind ( params , module ) :
credential_type_res = tower_cli . get_resource ( ' credential_type ' )
kind = params . pop ( ' kind ' )
arguments = { ' managed_by_tower ' : True }
if kind == ' ssh ' :
if params . get ( ' vault_password ' ) :
arguments [ ' kind ' ] = ' vault '
else :
arguments [ ' kind ' ] = ' ssh '
elif kind in ( ' net ' , ' scm ' , ' insights ' , ' vault ' ) :
arguments [ ' kind ' ] = kind
elif kind in KIND_CHOICES :
arguments . update ( dict (
kind = ' cloud ' ,
name = KIND_CHOICES [ kind ]
) )
return credential_type_res . get ( * * arguments )
2018-01-15 18:39:11 +03:00
2017-02-15 19:59:03 +03:00
def main ( ) :
2017-10-02 23:21:24 +03:00
argument_spec = tower_argument_spec ( )
argument_spec . update ( dict (
name = dict ( required = True ) ,
user = dict ( ) ,
team = dict ( ) ,
kind = dict ( required = True ,
2018-02-22 19:17:56 +03:00
choices = KIND_CHOICES . keys ( ) ) ,
2017-10-02 23:21:24 +03:00
host = dict ( ) ,
username = dict ( ) ,
password = dict ( no_log = True ) ,
2017-10-08 00:57:13 +03:00
ssh_key_data = dict ( no_log = True , type = ' path ' ) ,
2017-10-02 23:21:24 +03:00
ssh_key_unlock = dict ( no_log = True ) ,
authorize = dict ( type = ' bool ' , default = False ) ,
authorize_password = dict ( no_log = True ) ,
client = dict ( ) ,
secret = dict ( ) ,
tenant = dict ( ) ,
subscription = dict ( ) ,
domain = dict ( ) ,
become_method = dict ( ) ,
become_username = dict ( ) ,
become_password = dict ( no_log = True ) ,
vault_password = dict ( no_log = True ) ,
description = dict ( ) ,
organization = dict ( required = True ) ,
project = dict ( ) ,
state = dict ( choices = [ ' present ' , ' absent ' ] , default = ' present ' ) ,
) )
module = AnsibleModule ( argument_spec = argument_spec , supports_check_mode = True )
2017-02-15 19:59:03 +03:00
if not HAS_TOWER_CLI :
module . fail_json ( msg = ' ansible-tower-cli required for this module ' )
name = module . params . get ( ' name ' )
organization = module . params . get ( ' organization ' )
state = module . params . get ( ' state ' )
json_output = { ' credential ' : name , ' state ' : state }
tower_auth = tower_auth_config ( module )
with settings . runtime_values ( * * tower_auth ) :
tower_check_mode ( module )
credential = tower_cli . get_resource ( ' credential ' )
try :
2018-01-15 18:39:11 +03:00
params = { }
2017-02-15 19:59:03 +03:00
params [ ' create_on_missing ' ] = True
2018-01-15 18:39:11 +03:00
params [ ' name ' ] = name
2017-02-15 19:59:03 +03:00
if organization :
org_res = tower_cli . get_resource ( ' organization ' )
org = org_res . get ( name = organization )
params [ ' organization ' ] = org [ ' id ' ]
2018-02-22 19:17:56 +03:00
credential_type = credential_type_for_v1_kind ( module . params , module )
2018-01-15 18:39:11 +03:00
params [ ' credential_type ' ] = credential_type [ ' id ' ]
if module . params . get ( ' description ' ) :
params [ ' description ' ] = module . params . get ( ' description ' )
if module . params . get ( ' user ' ) :
user_res = tower_cli . get_resource ( ' user ' )
2018-02-22 19:23:38 +03:00
user = user_res . get ( username = module . params . get ( ' user ' ) )
2018-01-15 18:39:11 +03:00
params [ ' user ' ] = user [ ' id ' ]
if module . params . get ( ' team ' ) :
team_res = tower_cli . get_resource ( ' team ' )
team = team_res . get ( name = module . params . get ( ' team ' ) )
params [ ' team ' ] = team [ ' id ' ]
params [ ' inputs ' ] = { }
if module . params . get ( ' ssh_key_data ' ) :
filename = module . params . get ( ' ssh_key_data ' )
2017-02-15 19:59:03 +03:00
if not os . path . exists ( filename ) :
module . fail_json ( msg = ' file not found: %s ' % filename )
if os . path . isdir ( filename ) :
module . fail_json ( msg = ' attempted to read contents of directory: %s ' % filename )
with open ( filename , ' rb ' ) as f :
2018-01-15 18:39:11 +03:00
params [ ' inputs ' ] [ ' ssh_key_data ' ] = f . read ( )
2018-02-22 19:17:56 +03:00
for key in ( ' authorize ' , ' authorize_password ' , ' client ' , ' secret ' ,
' tenant ' , ' subscription ' , ' domain ' , ' become_method ' ,
' become_username ' , ' become_password ' , ' vault_password ' ,
' project ' , ' host ' , ' username ' , ' password ' ,
' ssh_key_unlock ' ) :
if module . params . get ( key ) :
params [ ' inputs ' ] [ key ] = module . params . get ( key )
2017-02-15 19:59:03 +03:00
if state == ' present ' :
result = credential . modify ( * * params )
json_output [ ' id ' ] = result [ ' id ' ]
elif state == ' absent ' :
result = credential . delete ( * * params )
except ( exc . NotFound ) as excinfo :
module . fail_json ( msg = ' Failed to update credential, organization not found: {0} ' . format ( excinfo ) , changed = False )
except ( exc . ConnectionError , exc . BadRequest , exc . NotFound ) as excinfo :
module . fail_json ( msg = ' Failed to update credential: {0} ' . format ( excinfo ) , changed = False )
json_output [ ' changed ' ] = result [ ' changed ' ]
module . exit_json ( * * json_output )
from ansible . module_utils . basic import AnsibleModule
if __name__ == ' __main__ ' :
main ( )