Fixing config loading issues when the config has no [general] section

Fixed typo in help documentation

Fix up sanity errors and update converted modules

Remove unnecessary test playbook file
This commit is contained in:
John Westcott IV
2020-02-06 11:07:19 -05:00
committed by beeankha
parent 4fc2c58ae7
commit 018dd4c1c3
4 changed files with 44 additions and 33 deletions

View File

@@ -7,7 +7,7 @@ from ansible.module_utils.six import PY2
from ansible.module_utils.six.moves.urllib.parse import urlparse, urlencode from ansible.module_utils.six.moves.urllib.parse import urlparse, urlencode
from ansible.module_utils.six.moves.urllib.error import HTTPError from ansible.module_utils.six.moves.urllib.error import HTTPError
from ansible.module_utils.six.moves.http_cookiejar import CookieJar from ansible.module_utils.six.moves.http_cookiejar import CookieJar
from ansible.module_utils.six.moves.configparser import ConfigParser, NoOptionError from ansible.module_utils.six.moves.configparser import ConfigParser, NoOptionError, MissingSectionHeaderError
from socket import gethostbyname from socket import gethostbyname
import re import re
from json import loads, dumps from json import loads, dumps
@@ -137,8 +137,10 @@ class TowerModule(AnsibleModule):
if not access(config_path, R_OK): if not access(config_path, R_OK):
raise ConfigFileException("The specified config file can not be read") raise ConfigFileException("The specified config file can not be read")
config.read(config_path) # If the config has not sections we will get a MissingSectionHeaderError
if not config.has_section('general'): try:
config.read(config_path)
except MissingSectionHeaderError:
self.warn("No general section in file, auto-appending") self.warn("No general section in file, auto-appending")
with open(config_path, 'r') as f: with open(config_path, 'r') as f:
config.read_string('[general]\n%s' % f.read()) config.read_string('[general]\n%s' % f.read())

View File

@@ -33,6 +33,11 @@ options:
- Fail loudly if the I(job_id) can not be canceled - Fail loudly if the I(job_id) can not be canceled
default: False default: False
type: bool type: bool
tower_oauthtoken:
description:
- The Tower OAuth token to use.
required: False
type: str
extends_documentation_fragment: awx.awx.auth extends_documentation_fragment: awx.awx.auth
''' '''
@@ -53,6 +58,7 @@ id:
from ..module_utils.tower_api import TowerModule from ..module_utils.tower_api import TowerModule
def main(): def main():
# Any additional arguments that are not fields of the item can be added here # Any additional arguments that are not fields of the item can be added here
argument_spec = dict( argument_spec = dict(
@@ -77,25 +83,25 @@ def main():
} }
}) })
if job == None: if job is None:
module.fail_json(msg="Unable to find job with id {0}".format(job_id)) module.fail_json(msg="Unable to find job with id {0}".format(job_id))
cancel_page = module.get_endpoint(job['related']['cancel']) cancel_page = module.get_endpoint(job['related']['cancel'])
if 'json' not in cancel_page or 'can_cancel' not in cancel_page['json']: if 'json' not in cancel_page or 'can_cancel' not in cancel_page['json']:
module.fail_json(msg="Failed to cancel job, got unexpected response from tower", **{ 'response': cancel_page }) module.fail_json(msg="Failed to cancel job, got unexpected response from tower", **{'response': cancel_page})
if not cancel_page['json']['can_cancel']: if not cancel_page['json']['can_cancel']:
if fail_if_not_running: if fail_if_not_running:
module.fail_json(msg="Job is not running") module.fail_json(msg="Job is not running")
else: else:
module.exit_json(**{ 'changed': False }) module.exit_json(**{'changed': False})
response = module.post_endpoint(job['related']['cancel'], **{ 'data': {} }) results = module.post_endpoint(job['related']['cancel'], **{'data': {}})
if response['status_code'] != 202: if results['status_code'] != 202:
module.fail_json(msg="Failed to cancel job, see response for details", **{'response': results }) module.fail_json(msg="Failed to cancel job, see response for details", **{'response': results})
module.exit_json(**{ 'changed': True }) module.exit_json(**{'changed': True})
if __name__ == '__main__': if __name__ == '__main__':

View File

@@ -60,16 +60,16 @@ options:
scm_branch: scm_branch:
description: description:
- A specific of the SCM project to run the template on. - A specific of the SCM project to run the template on.
- This is only applicable if your project allows for branch override - This is only applicable if your project allows for branch override.
type: str type: str
skip_tags: skip_tags:
decription: description:
- Specific tags to skip from the playbook. - Specific tags to skip from the playbook.
type: list type: list
verbosity: verbosity:
description: description:
- Verbosity level for this job run - Verbosity level for this job run
tpye: int type: int
choices: [ 0, 1, 2, 3, 4, 5 ] choices: [ 0, 1, 2, 3, 4, 5 ]
diff_mode: diff_mode:
description: description:
@@ -78,6 +78,12 @@ options:
credential_passwords: credential_passwords:
description: description:
- Passwords for credentials which are set to prompt on launch - Passwords for credentials which are set to prompt on launch
type: dict
tower_oauthtoken:
description:
- The Tower OAuth token to use.
required: False
type: str
extends_documentation_fragment: awx.awx.auth extends_documentation_fragment: awx.awx.auth
''' '''
@@ -130,22 +136,23 @@ status:
from ..module_utils.tower_api import TowerModule from ..module_utils.tower_api import TowerModule
def main(): def main():
# Any additional arguments that are not fields of the item can be added here # Any additional arguments that are not fields of the item can be added here
argument_spec = dict( argument_spec = dict(
name=dict(type=str, required=True, aliases=['job_template']), name=dict(type='str', required=True, aliases=['job_template']),
job_type=dict(type=str, choices=['run', 'check']), job_type=dict(type='str', choices=['run', 'check']),
inventory=dict(type=str, default=None), inventory=dict(type='str', default=None),
# Credentials will be a str instead of a list for backwards compatability # Credentials will be a str instead of a list for backwards compatability
credentials=dict(type='list', default=None, aliases=['credential']), credentials=dict(type='list', default=None, aliases=['credential']),
limit=dict(), limit=dict(),
tags=dict(type='list'), tags=dict(type='list'),
extra_vars=dict(type=dict, required=False), extra_vars=dict(type='dict', required=False),
scm_branch=dict(type=str, required=False), scm_branch=dict(type='str', required=False),
skip_tags=dict(type=list, required=False), skip_tags=dict(type='list', required=False),
verbosity=dict(type=int, required=False, choices=[0,1,2,3,4,5]), verbosity=dict(type='int', required=False, choices=[0, 1, 2, 3, 4, 5]),
diff_mode=dict(type=bool, required=False), diff_mode=dict(type='bool', required=False),
credential_passwords=dict(type=dict, required=False), credential_passwords=dict(type='dict', required=False),
) )
# Create a module for ourselves # Create a module for ourselves
@@ -179,7 +186,7 @@ def main():
if credentials: if credentials:
post_data['credentials'] = [] post_data['credentials'] = []
for credential in credentials: for credential in credentials:
post_data['credentials'].append( module.resolve_name_to_id('credentials', credential) ) post_data['credentials'].append(module.resolve_name_to_id('credentials', credential))
# Attempt to look up job_template based on the provided name # Attempt to look up job_template based on the provided name
job_template = module.get_one('job_templates', **{ job_template = module.get_one('job_templates', **{
@@ -188,7 +195,7 @@ def main():
} }
}) })
if job_template == None: if job_template is None:
module.fail_json(msg="Unable to find job template by name {0}".format(name)) module.fail_json(msg="Unable to find job template by name {0}".format(name))
# The API will allow you to submit values to a jb launch that are not prompt on launch. # The API will allow you to submit values to a jb launch that are not prompt on launch.
@@ -211,13 +218,13 @@ def main():
if module.params.get(variable_name) and not job_template[check_vars_to_prompts[variable_name]]: if module.params.get(variable_name) and not job_template[check_vars_to_prompts[variable_name]]:
param_errors.append("The field {0} was specified but the job template does not allow for it to be overridden".format(variable_name)) param_errors.append("The field {0} was specified but the job template does not allow for it to be overridden".format(variable_name))
if len(param_errors) > 0: if len(param_errors) > 0:
module.fail_json(msg="Parameters specified which can not be passed into job template, see errors for details", **{ 'errors': param_errors }) module.fail_json(msg="Parameters specified which can not be passed into job template, see errors for details", **{'errors': param_errors})
# Launch the job # Launch the job
results = module.post_endpoint(job_template['related']['launch'], **{ 'data': post_data }) results = module.post_endpoint(job_template['related']['launch'], **{'data': post_data})
if results['status_code'] != 201: if results['status_code'] != 201:
module.fail_json(msg="Failed to launch job, see response for details", **{'response': results }) module.fail_json(msg="Failed to launch job, see response for details", **{'response': results})
module.exit_json(**{ module.exit_json(**{
'changed': True, 'changed': True,
@@ -225,5 +232,6 @@ def main():
'status': results['json']['status'], 'status': results['json']['status'],
}) })
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@@ -101,11 +101,6 @@ def main():
# instance_group_names = module.params.get('instance_groups') # instance_group_names = module.params.get('instance_groups')
state = module.params.get('state') state = module.params.get('state')
# Attempt to look up the related items the user specified (these will fail the module if not found)
# instance_group_objects = []
# for instance_name in instance_group_names:
# instance_group_objects.append(module.resolve_name_to_id('instance_groups', instance_name))
# Attempt to look up organization based on the provided name # Attempt to look up organization based on the provided name
organization = module.get_one('organizations', **{ organization = module.get_one('organizations', **{
'data': { 'data': {