How do I programmatically upload a credential to Ansible Tower with Microsoft Azure key Vault as the credential type

108 Views Asked by At

I am writing a python script to interact with Ansible Tower to upload credentials. I had no trouble doing this programmatically when the credential type was 'machine', however, I run in to trouble when I try to update my code to reference a secret stored in Microsoft Azure Key Vault. I can manually create these keys, but when I try to do it in python I get vague http errors that are no help.

Here is a snippet of my python code. I am sure that the request headers are correct, as it worked flawlessly before attempting to integrate Azure KV. I am also sure that the url, client, secret, and tenant values (which I excluded from here for security) are correect, as Ansible tower allows me to test these strings before saving the credential when created manually.

        credential_extension = f"job_templates/{template_id}/credentials/"
        data = {
            "name": f'{device_name}-credential',
            "credential_type": 21,
            "organization": 1,
            "inputs": {
                "url" : <AZUREURL>,
                "client" : <AZURECLIENTID>,
                "secret" : <AZURECLIENTSECRET>,
                "tenant" : <AZURETENANTID>,
                "cloud_name": "AzureCloud"
            }
        }

        postCredentialResponse = requests.post(ANSIBLEURL+credential_extension,headers=headers,json=data)
        postCredentialResponse.raise_for_status()

I am given the error requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: http://<url>/api/v2/job_templates/143/credentials/.

I am new to API calls and I find this error vague and unhelpful. I assume my key-value pairs are off in the data{} dict. I have tried slightly renaming these values (ie. from client to client_id, etc) but I havent found a combination that successfully handles the request.

0

There are 0 best solutions below