簡體   English   中英

使用Ansible Tower REST API創建憑證

[英]Creating credential using Ansible Tower REST API

在我的Ansible Tower中,我有一個名為Token的自定義憑證,我們在其中存儲令牌,因此使用該憑證就不必登錄,並且可以在各種作業中使用該憑證。

以下是必填字段-

名稱:

憑證類型:(我們選擇此自定義憑證類型)

API令牌值:(輸入令牌的地方,也表示為額外變量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模塊中的網址也正確嗎?

創建自定義證書的方式有兩種(我更喜歡第二種):

第一種選擇:您的方法-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憑證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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM