Tower modules: move HAS_TOWER_CLI check in module_utils and minor improvements (#39809)

* tower_* modules: move HAS_TOWER_CLI in TowerModule

Besides this change allows to define other common parameters such as
mutually_exclusive.

* tower_*: config file can not be used with auth params

* tower module_utils: remove useless call to expanduser

'path' type: expanduser & expandvars are automatically called
This commit is contained in:
Pilou 2018-08-02 17:17:39 +02:00 committed by Brian Coca
commit 12973e0541
17 changed files with 89 additions and 162 deletions

View file

@ -37,6 +37,8 @@ try:
except ImportError:
HAS_TOWER_CLI = False
from ansible.module_utils.basic import AnsibleModule
def tower_auth_config(module):
'''tower_auth_config attempts to load the tower-cli.cfg file
@ -47,7 +49,6 @@ def tower_auth_config(module):
'''
config_file = module.params.pop('tower_config_file', None)
if config_file:
config_file = os.path.expanduser(config_file)
if not os.path.exists(config_file):
module.fail_json(msg='file not found: %s' % config_file)
if os.path.isdir(config_file):
@ -82,11 +83,26 @@ def tower_check_mode(module):
module.fail_json(changed=False, msg='Failed check mode: {0}'.format(excinfo))
def tower_argument_spec():
return dict(
tower_host=dict(),
tower_username=dict(),
tower_password=dict(no_log=True),
tower_verify_ssl=dict(type='bool', default=True),
tower_config_file=dict(type='path'),
)
class TowerModule(AnsibleModule):
def __init__(self, argument_spec, **kwargs):
args = dict(
tower_host=dict(),
tower_username=dict(),
tower_password=dict(no_log=True),
tower_verify_ssl=dict(type='bool', default=True),
tower_config_file=dict(type='path'),
)
args.update(argument_spec)
mutually_exclusive = kwargs.get('mutually_exclusive', [])
kwargs['mutually_exclusive'] = mutually_exclusive.extend((
('tower_config_file', 'tower_host'),
('tower_config_file', 'tower_username'),
('tower_config_file', 'tower_password'),
('tower_config_file', 'tower_verify_ssl'),
))
super(TowerModule, self).__init__(argument_spec=args, **kwargs)
if not HAS_TOWER_CLI:
self.fail_json(msg='ansible-tower-cli required for this module')