0

在我的 Ansible Tower 中,我有一个名为 Token 的自定义凭证,我们在其中存储令牌,因此使用此凭证我们不必登录并且可以在各种工作中使用此凭证。

以下是必填字段 -

姓名:

凭证类型:(我们选择此自定义凭证类型)

API Token Value:(输入token的地方,也表示为一个额外的变量my_token)

下面是我用来做必要的 yml 文件 -

—-

   Required info

   tasks:

      - name: Create credential

         uri:

             url: “https://ans........../api/v1/credentials/“

             method: “POST”

             kind: SecureCloud

             name: Token

             body:

                  extra_vars:

                      my_token: “{ key }”

             body_format: json

我对如何在上述剧本中输入字段值 Name 和 Credential Types 感到困惑。这样做时我是否还需要任何其他字段?uri模块中的url也正确吗?

4

1 回答 1

0

有两种创建自定义凭证的方法(我更喜欢第二种):

第一个选项:您的方法 - 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
于 2018-11-17T15:33:16.387 回答