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
2018-09-19 17:02:27 +03:00
# Copyright: (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
2019-03-06 20:44:14 +03:00
type : str
2017-02-15 19:59:03 +03:00
description :
description :
- The description to use for the credential .
2019-03-06 20:44:14 +03:00
type : str
2017-02-15 19:59:03 +03:00
user :
description :
- User that should own this credential .
2019-03-06 20:44:14 +03:00
type : str
2017-02-15 19:59:03 +03:00
team :
description :
- Team that should own this credential .
2019-03-06 20:44:14 +03:00
type : str
2017-02-15 19:59:03 +03:00
project :
description :
2019-11-20 19:15:18 +03:00
- Project that should use this credential .
2019-03-06 20:44:14 +03:00
type : str
2017-02-15 19:59:03 +03:00
organization :
description :
- Organization that should own the credential .
2018-10-18 23:21:35 +03:00
required : True
2019-03-06 20:44:14 +03:00
type : str
2017-02-15 19:59:03 +03:00
kind :
description :
2019-03-06 20:44:14 +03:00
- Type of credential being added .
- The ssh choice refers to a Tower Machine credential .
2019-10-22 21:39:27 +03:00
required : False
2019-03-06 20:44:14 +03:00
type : str
2018-02-22 21:01:08 +03:00
choices : [ " ssh " , " vault " , " net " , " scm " , " aws " , " vmware " , " satellite6 " , " cloudforms " , " gce " , " azure_rm " , " openstack " , " rhv " , " insights " , " tower " ]
2019-10-22 21:39:27 +03:00
credential_type :
description :
- Name of credential type .
required : False
version_added : " 2.10 "
type : str
inputs :
description :
- > -
Credential inputs where the keys are var names used in templating .
Refer to the Ansible Tower documentation for example syntax .
required : False
version_added : " 2.9 "
type : dict
2017-02-15 19:59:03 +03:00
host :
description :
- Host for this credential .
2019-03-06 20:44:14 +03:00
type : str
2017-02-15 19:59:03 +03:00
username :
description :
2019-07-16 22:37:47 +03:00
- Username for this credential . ` ` access_key ` ` for AWS .
2019-03-06 20:44:14 +03:00
type : str
2017-02-15 19:59:03 +03:00
password :
description :
2019-07-16 22:37:47 +03:00
- Password for this credential . ` ` secret_key ` ` for AWS . ` ` api_key ` ` for RAX .
- Use " ASK " and launch in Tower to be prompted .
2019-03-06 20:44:14 +03:00
type : str
2017-02-15 19:59:03 +03:00
ssh_key_data :
description :
2018-09-07 20:41:09 +03:00
- SSH private key content . To extract the content from a file path , use the lookup function ( see examples ) .
required : False
2019-03-06 20:44:14 +03:00
type : str
2017-02-15 19:59:03 +03:00
ssh_key_unlock :
description :
2019-07-16 22:37:47 +03:00
- Unlock password for ssh_key .
- Use " ASK " and launch in Tower to be prompted .
2019-03-06 20:44:14 +03:00
type : str
2017-02-15 19:59:03 +03:00
authorize :
description :
2018-03-15 06:58:50 +03:00
- Should use authorize for net type .
2018-03-16 00:15:24 +03:00
type : bool
default : ' no '
2017-02-15 19:59:03 +03:00
authorize_password :
description :
2018-03-15 06:58:50 +03:00
- Password for net credentials that require authorize .
2019-03-06 20:44:14 +03:00
type : str
2017-02-15 19:59:03 +03:00
client :
description :
- Client or application ID for azure_rm type .
2019-03-06 20:44:14 +03:00
type : str
2018-03-16 20:28:19 +03:00
security_token :
description :
- STS token for aws type .
version_added : " 2.6 "
2019-03-06 20:44:14 +03:00
type : str
2017-02-15 19:59:03 +03:00
secret :
description :
- Secret token for azure_rm type .
2019-03-06 20:44:14 +03:00
type : str
2017-02-15 19:59:03 +03:00
subscription :
description :
- Subscription ID for azure_rm type .
2019-03-06 20:44:14 +03:00
type : str
2017-02-15 19:59:03 +03:00
tenant :
description :
- Tenant ID for azure_rm type .
2019-03-06 20:44:14 +03:00
type : str
2017-02-15 19:59:03 +03:00
domain :
description :
- Domain for openstack type .
2019-03-06 20:44:14 +03:00
type : str
2017-02-15 19:59:03 +03:00
become_method :
description :
2019-02-18 18:49:11 +03:00
- Become method to use for privilege escalation .
2019-09-30 23:01:44 +03:00
- Some examples are " None " , " sudo " , " su " , " pbrun "
- Due to become plugins , these can be arbitrary
2019-03-06 20:44:14 +03:00
type : str
2017-02-15 19:59:03 +03:00
become_username :
description :
2019-07-16 22:37:47 +03:00
- Become username .
- Use " ASK " and launch in Tower to be prompted .
2019-03-06 20:44:14 +03:00
type : str
2017-02-15 19:59:03 +03:00
become_password :
description :
2019-07-16 22:37:47 +03:00
- Become password .
- Use " ASK " and launch in Tower to be prompted .
2019-03-06 20:44:14 +03:00
type : str
2017-02-15 19:59:03 +03:00
vault_password :
description :
2019-07-16 22:37:47 +03:00
- Vault password .
- Use " ASK " and launch in Tower to be prompted .
2019-03-06 20:44:14 +03:00
type : str
vault_id :
description :
- Vault identifier .
- This parameter is only valid if C ( kind ) is specified as C ( vault ) .
type : str
version_added : " 2.8 "
2017-02-15 19:59:03 +03:00
state :
description :
- Desired state of the resource .
choices : [ " present " , " absent " ]
2018-03-16 00:15:24 +03:00
default : " present "
2019-03-06 20:44:14 +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
2017-02-15 19:59:03 +03:00
'''
EXAMPLES = '''
- name : Add tower credential
tower_credential :
name : Team Name
description : Team Description
organization : test - org
2018-10-18 23:21:35 +03:00
kind : ssh
2017-02-15 19:59:03 +03:00
state : present
tower_config_file : " ~/tower_cli.cfg "
2018-09-07 20:41:09 +03:00
- name : Create a valid SCM credential from a private_key file
tower_credential :
name : SCM Credential
organization : Default
state : present
kind : scm
username : joe
password : secret
ssh_key_data : " {{ lookup( ' file ' , ' /tmp/id_rsa ' ) }} "
ssh_key_unlock : " passphrase "
2018-10-18 23:21:35 +03:00
2019-06-11 17:25:41 +03:00
- name : Fetch private key
slurp :
src : ' $HOME/.ssh/aws-private.pem '
register : aws_ssh_key
2018-10-18 23:21:35 +03:00
- name : Add Credential Into Tower
tower_credential :
name : Workshop Credential
2019-06-11 17:25:41 +03:00
ssh_key_data : " {{ aws_ssh_key[ ' content ' ] | b64decode }} "
2018-10-18 23:21:35 +03:00
kind : ssh
organization : Default
tower_username : admin
tower_password : ansible
tower_host : https : / / localhost
run_once : true
delegate_to : localhost
2019-10-22 21:39:27 +03:00
- name : Add Credential with Custom Credential Type
tower_credential :
name : Workshop Credential
credential_type : MyCloudCredential
organization : Default
tower_username : admin
tower_password : ansible
tower_host : https : / / localhost
2017-02-15 19:59:03 +03:00
'''
2017-10-02 23:21:24 +03:00
import os
2018-03-16 20:28:19 +03:00
from ansible . module_utils . _text import to_text
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-15 19:59:03 +03:00
try :
import tower_cli
2019-01-09 17:31:21 +03:00
import tower_cli . exceptions as exc
2017-02-15 19:59:03 +03:00
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 ' ,
}
2019-10-22 21:39:27 +03:00
OLD_INPUT_NAMES = (
' authorize ' , ' authorize_password ' , ' client ' ,
' security_token ' , ' secret ' , ' tenant ' , ' subscription ' ,
' domain ' , ' become_method ' , ' become_username ' ,
' become_password ' , ' vault_password ' , ' project ' , ' host ' ,
' username ' , ' password ' , ' ssh_key_data ' , ' vault_id ' ,
' ssh_key_unlock '
)
def credential_type_for_kind ( params ) :
2018-02-22 19:17:56 +03:00
credential_type_res = tower_cli . get_resource ( ' credential_type ' )
2019-08-26 18:12:41 +03:00
kind = params . get ( ' kind ' )
2018-02-22 19:17:56 +03:00
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
2018-08-02 18:17:39 +03:00
argument_spec = dict (
2017-10-02 23:21:24 +03:00
name = dict ( required = True ) ,
user = dict ( ) ,
team = dict ( ) ,
2019-10-22 21:39:27 +03:00
kind = dict ( choices = KIND_CHOICES . keys ( ) ) ,
credential_type = dict ( ) ,
inputs = dict ( type = ' dict ' ) ,
2017-10-02 23:21:24 +03:00
host = dict ( ) ,
username = dict ( ) ,
password = dict ( no_log = True ) ,
2018-09-07 20:41:09 +03:00
ssh_key_data = dict ( no_log = True , type = ' str ' ) ,
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 ( ) ,
2018-03-16 20:28:19 +03:00
security_token = dict ( ) ,
2017-10-02 23:21:24 +03:00
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 ' ) ,
2019-03-06 20:44:14 +03:00
vault_id = dict ( ) ,
2018-08-02 18:17:39 +03:00
)
2017-10-02 23:21:24 +03:00
2019-10-22 21:39:27 +03:00
mutually_exclusive = [
( ' kind ' , ' credential_type ' )
]
for input_name in OLD_INPUT_NAMES :
mutually_exclusive . append ( ( ' inputs ' , input_name ) )
module = TowerModule ( argument_spec = argument_spec , supports_check_mode = True ,
mutually_exclusive = mutually_exclusive )
2017-02-15 19:59:03 +03:00
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-26 21:12:54 +03:00
try :
tower_cli . get_resource ( ' credential_type ' )
2018-03-01 18:36:18 +03:00
except ( ImportError , AttributeError ) :
2018-02-26 21:12:54 +03:00
# /api/v1/ backwards compat
# older versions of tower-cli don't *have* a credential_type
# resource
2019-10-22 21:39:27 +03:00
params [ ' kind ' ] = module . params . get ( ' kind ' )
2018-02-26 21:12:54 +03:00
else :
2019-10-22 21:39:27 +03:00
if module . params . get ( ' credential_type ' ) :
credential_type_res = tower_cli . get_resource ( ' credential_type ' )
try :
credential_type = credential_type_res . get ( name = module . params [ ' credential_type ' ] )
except ( exc . NotFound ) as excinfo :
module . fail_json ( msg = (
' Failed to update credential, credential_type not found: {0} '
) . format ( excinfo ) , changed = False )
params [ ' credential_type ' ] = credential_type [ ' id ' ]
if module . params . get ( ' inputs ' ) :
params [ ' inputs ' ] = module . params . get ( ' inputs ' )
elif module . params . get ( ' kind ' ) :
credential_type = credential_type_for_kind ( module . params )
params [ ' credential_type ' ] = credential_type [ ' id ' ]
else :
module . fail_json ( msg = ' must either specify credential_type or kind ' , changed = False )
2018-01-15 18:39:11 +03:00
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 ' ]
if module . params . get ( ' ssh_key_data ' ) :
2018-09-07 20:41:09 +03:00
data = module . params . get ( ' ssh_key_data ' )
if os . path . exists ( data ) :
module . deprecate (
2019-06-11 17:25:41 +03:00
msg = ' ssh_key_data should be a string, not a path to a file. ' ,
2018-09-07 20:41:09 +03:00
version = " 2.12 "
)
if os . path . isdir ( data ) :
module . fail_json ( msg = ' attempted to read contents of directory: %s ' % data )
with open ( data , ' rb ' ) as f :
module . params [ ' ssh_key_data ' ] = to_text ( f . read ( ) )
else :
module . params [ ' ssh_key_data ' ] = data
2018-03-16 20:28:19 +03:00
2019-03-06 20:44:14 +03:00
if module . params . get ( ' vault_id ' , None ) and module . params . get ( ' kind ' ) != ' vault ' :
module . fail_json ( msg = " Parameter ' vault_id ' is only valid if parameter ' kind ' is specified as ' vault ' " )
2019-10-22 21:39:27 +03:00
for key in OLD_INPUT_NAMES :
2018-03-01 23:22:29 +03:00
if ' kind ' in params :
params [ key ] = module . params . get ( key )
elif module . params . get ( key ) :
params . setdefault ( ' 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 )
2019-11-26 17:39:05 +03:00
except ( exc . ConnectionError , exc . BadRequest , exc . AuthError ) as excinfo :
2017-02-15 19:59:03 +03:00
module . fail_json ( msg = ' Failed to update credential: {0} ' . format ( excinfo ) , changed = False )
json_output [ ' changed ' ] = result [ ' changed ' ]
module . exit_json ( * * json_output )
if __name__ == ' __main__ ' :
main ( )