# Buggy code
def update_resources(module, p):
    """update_resources attempts to fetch any of the resources given
    by name using their unique field (identity)
    """
    params = p.copy()
    for key in p:
        if key.startswith("tower_"):
            params.pop(key)
    params.pop("state", None)
    identity_map = {
        "user": "username",
        "team": "name",
        "target_team": "name",
        "inventory": "name",
        "job_template": "name",
        "credential": "name",
        "organization": "name",
        "project": "name",
    }
    for k, v in identity_map.items():
        try:
            if params[k]:
                key = "team" if k == "target_team" else k
                result = tower_cli.get_resource(key).get(**{v: params[k]})
                params[k] = result["id"]
        except exc.NotFound as excinfo:
            module.fail_json(
                msg="Failed to update role, {0} not found: {1}".format(k, excinfo),
                changed=False,
            )
    return params


# 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 update_resources(module, p):
    """update_resources attempts to fetch any of the resources given
    by name using their unique field (identity)
    """
    params = p.copy()
    identity_map = {
        "user": "username",
        "team": "name",
        "target_team": "name",
        "inventory": "name",
        "job_template": "name",
        "credential": "name",
        "organization": "name",
        "project": "name",
    }
    for k, v in identity_map.items():
        try:
            if params[k]:
                key = "team" if k == "target_team" else k
                result = tower_cli.get_resource(key).get(**{v: params[k]})
                params[k] = result["id"]
        except exc.NotFound as excinfo:
            module.fail_json(
                msg="Failed to update role, {0} not found: {1}".format(k, excinfo),
                changed=False,
            )
    return params