There are two ways of creating a custom credential (I prefer the second one):
First Option: Your Approach - URI Module
- name: Create Custom Credential
uri:
url: "https://endpoint/api/v2/credentials/"
method: POST
user: admin
password: password
headers:
Content-Type: "application/json"
body: '{"name":"myfirsttoken","description":"","organization":34,"credential_type":34,"inputs":{"token":"MyToken"}}'
force_basic_auth: true
validate_certs: false
status_code: 200, 201
no_log: false
But, be careful because this is not idempotent and you should do a GET Credentials First with the method: GET
, register your results and find your credential in your register.json.results
variable.
Second Option: My Preferred Approach - tower-cli
You can do exactly the same, easier and idempotent with:
- name: Add Custom Credential
command: tower-cli credential create --name="{{ item }}" --credential-type "{{ credential_type }}" --inputs "{'token':'123456'}" -h endpoint -u admin -p password --organization Default
no_log: true
with_items:
- MyCustomToken
You will get something like:
== ============= ===============
id name credential_type
== ============= ===============
46 MyCustomToken 34
== ============= ===============
The cool stuff is that you can fully automate your tokens and even autogenerate them with:
token: "{{ lookup('password', '/dev/null length=20 chars=ascii_letters,digits') }}"
And then:
---
- name: Create Custom Credential Token
hosts: localhost
connection: local
gather_facts: false
vars:
token: "{{ lookup('password', '/dev/null length=20 chars=ascii_letters,digits') }}"
credential_type: MyCustom
tasks:
- name: Create Credential Type
tower_credential_type:
name: "{{ credential_type }}"
description: Custom Credentials type
kind: cloud
inputs: {"fields":[{"secret":true,"type":"string","id":"token","label":"token"}],"required":["token"]}
state: present
tower_verify_ssl: false
tower_host: endpoint
tower_username: admin
tower_password: password
- name: Add Custom Credential
command: tower-cli credential create --name="{{ item }}" --credential-type "{{ credential_type }}" --inputs "{'token':'{{ token }}'}" -h endpoint -u admin -p password --organization Default
no_log: true
with_items:
- MyCustomToken