# Buggy code
def tower_auth_config(module):
    """tower_auth_config attempts to load the tower-cli.cfg file
    specified from the `tower_config_file` parameter. If found,
    if returns the contents of the file as a dictionary, else
    it will attempt to fetch values from the module pararms and
    only pass those values that have been set.
    """
    config_file = module.params.get("tower_config_file")
    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):
            module.fail_json(
                msg="directory can not be used as config file: %s" % config_file
            )

        with open(config_file, "rb") as f:
            return parser.string_to_dict(f.read())
    else:
        auth_config = {}
        host = module.params.get("tower_host")
        if host:
            auth_config["host"] = host
        username = module.params.get("tower_username")
        if username:
            auth_config["username"] = username
        password = module.params.get("tower_password")
        if password:
            auth_config["password"] = password
        verify_ssl = module.params.get("tower_verify_ssl")
        if verify_ssl is not None:
            auth_config["verify_ssl"] = verify_ssl
        return auth_config


# Traceback (tower_cli.exceptions.BadRequest)
[...]
TASK [Add myself to the member role] ***********************************************************************                                                                                               [0/1598]
fatal: [localhost]: FAILED! => {
"changed": false,
"rc": 1
}

MSG:

MODULE FAILURE


MODULE_STDERR:

*** DETAILS: Getting the record. **********************************************
GET http://localhost:8087/api/v2/users/
Params: {'username': 'pierre'}

*** DETAILS: Getting the record. **********************************************
GET http://localhost:8087/api/v2/teams/
Params: {'name': 'backend'}

*** DETAILS: Checking if role exists. *****************************************
GET http://localhost:8087/api/v2/teams/1/object_roles/
Params: {'tower_verify_ssl': True, 'state': 'present', 'tower_config_file': 'awx_cli.cfg', 'role_field': 'member_role'}

Traceback (most recent call last):
File "/tmp/ansible_0DBZC3/ansible_module_tower_role.py", line 177, in <module>
main()
File "/tmp/ansible_0DBZC3/ansible_module_tower_role.py", line 164, in main
result = role.grant(**params)
File "/usr/local/lib/python2.7/dist-packages/tower_cli/resources/role.py", line 364, in grant
return self.role_write(fail_on_found=fail_on_found, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tower_cli/resources/role.py", line 241, in role_write
fail_on_multiple_results=True, **data)
File "/usr/local/lib/python2.7/dist-packages/tower_cli/models/base.py", line 288, in read
r = client.get(url, params=kwargs)
File "/home/piroux/.local/lib/python2.7/site-packages/requests/sessions.py", line 521, in get
return self.request('GET', url, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tower_cli/api.py", line 286, in request
kwargs.get('data', None), r.content.decode('utf8'))
tower_cli.exceptions.BadRequest: The Tower server claims it was sent a bad request.

GET http://localhost:8087/api/v2/teams/1/object_roles/
Params: {'tower_verify_ssl': True, 'state': 'present', 'tower_config_file': 'awx_cli.cfg', 'role_field': 'member_role'}
Data: None

Response: {"detail":"Role has no field named 'tower_config_file'"}


to retry, use: --limit @/home/piroux/project_first_week/tut_ansible/qusible/awx_init.retry

PLAY RECAP *************************************************************************************************

# Fixed code
def tower_auth_config(module):
    """tower_auth_config attempts to load the tower-cli.cfg file
    specified from the `tower_config_file` parameter. If found,
    if returns the contents of the file as a dictionary, else
    it will attempt to fetch values from the module pararms and
    only pass those values that have been set.
    """
    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):
            module.fail_json(
                msg="directory can not be used as config file: %s" % config_file
            )

        with open(config_file, "rb") as f:
            return parser.string_to_dict(f.read())
    else:
        auth_config = {}
        host = module.params.pop("tower_host", None)
        if host:
            auth_config["host"] = host
        username = module.params.pop("tower_username", None)
        if username:
            auth_config["username"] = username
        password = module.params.pop("tower_password", None)
        if password:
            auth_config["password"] = password
        verify_ssl = module.params.pop("tower_verify_ssl", None)
        if verify_ssl is not None:
            auth_config["verify_ssl"] = verify_ssl
        return auth_config