Ansible uri モジュールとPower Cloud API を使用した IBM Cloud PowerSystems Virtual Server のボリューム作成


API を直接実行すれば galaxy collection やプラグインを介さずにIBM Cloud PowerSystems Virtual Severs を操作できるのではと思い、 Ansible uri モジュールで Power Cloud API 実行を確認したログです。


環境:

・IBM Cloud PowerSystems Virtual Server サービス
参考:IBM Cloud Docs: IBM Power Systems Virtual Server の概説

・Ansible 2.9.9 (from Local Mac)

・前提となる API キーの取得: IBM Cloud Docs: API キーの作成
・前提となる CRN(クラウド・リソース名)の取得: IBM Cloud Docs クラウド・リソース名

・前提となる インスタンスIDの取得: IBM Cloud Docs : インスタンス ID の取得


使用する PowerCloud API、Ansible uri module

・PowerCloud API
使用するAPI : Power Cloud API "Create a new data volume"

・Ansible uri module

参考:Ansible "uri module"


実行する Playbook

uri_volume_create.yml を以下のように作成しました。

uri_volume_create.yml
---
- name: POWER VSI IAM TOKEN
  hosts: localhost
  gather_facts : no

  tasks: 
  - name: Get IAM access token  #<= TOKEN の発行                      
    uri:
      url: https://iam.cloud.ibm.com/identity/token
      method: POST
      headers:
        Accept: application/json
        Content-Type: application/x-www-form-urlencoded
      body: 
           grant_type: urn:ibm:params:oauth:grant-type:apikey
           apikey: < API key を入力 > 
      body_format: form-urlencoded
      validate_certs: no
    register: auth_token

  - name: Set Variable OS_TOKEN
    set_fact: 
      OS_TOKEN: "{{ auth_token.json.access_token }}"

  - name: Print OS_TOKEN  #<= TOKEN の確認
    debug:
        var: OS_TOKEN

  - name: Create Volume  #<= ボリュームの作成
    uri:
      url: https://us-south.power-iaas.cloud.ibm.com/pcloud/v1/cloud-instances/< 事前に取得した instance_id >/volumes
      method: POST
      status_code: [200, 202]   #<= なしで実行すると 202 が返りAnsible実行がfailed となったので追加しています。
      headers:
        Authorization: Bearer {{ OS_TOKEN }}
        Content-Type: application/json
        CRN: "< CRN を入力> "
      body_format: json
      body: 
         "diskType": "tier3"               #<= ディスク・タイプ(tier1 , tier3 など)
         "name": "ansible_testvol"         #<= volume 名 (任意)
         "size": 10                        #<= サイズ GB (任意)
         "shareable": true                 #<= 共有設定 (true or false)

実行

$ ansible-playbook uri_volume_create.yml
[WARNING]: Unable to parse /Users/c_u/Desktop/ansible_power_iaas/inventory as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [POWER VSI IAM TOKEN] **********************************************************************************************************************************************************

TASK [Get auth token] ***************************************************************************************************************************************************************
ok: [localhost]

TASK [Set Variable OS_TOKEN] ********************************************************************************************************************************************************
ok: [localhost]

TASK [Print OS_TOKEN] ***************************************************************************************************************************************************************
ok: [localhost] => {
    "OS_TOKEN": "xxxx TOKEN の長い文字列の表示 xxxx"
}

TASK [Create Volume] **************************************************************************************************************************************************************
ok: [localhost]

PLAY RECAP **************************************************************************************************************************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

確認

作成されていることを IBM cloud コンソール上で確認しました。


他のAPIでも実行できるのかな...

以上です。