有两种创建自定义凭证的方法(我更喜欢第二种):
第一个选项:您的方法 - URI 模块
- 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
但是,要小心,因为这不是幂等的,您应该首先使用 GET Credentials ,注册您的结果并在您的变量method: GET
中找到您的凭证。register.json.results
第二种选择:我的首选方法 - tower-cli
您可以通过以下方式执行完全相同、更简单且幂等的操作:
- 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
你会得到类似的东西:
== ============= ===============
id name credential_type
== ============= ===============
46 MyCustomToken 34
== ============= ===============
很酷的东西是你可以完全自动化你的令牌,甚至自动生成它们:
token: "{{ lookup('password', '/dev/null length=20 chars=ascii_letters,digits') }}"
接着:
---
- 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