PowerVC API を使用したボリュームの削除を Ansible uri モジュールで実行

#### はじめに


PowerVC 管理のボリュームの削除を Ansible 経由での実行で確認しました。



・[Supported OpenStack Block storage (Cinder) APIs](https://www.ibm.com/docs/en/powervc/2.0.2?topic=apis-supported-openstack-block-storage-cinder)

---
#### 環境

・PowerVC 2.0.2 (PowerVC_Server, RHEL 8.4, ppc64le on S824)

---
#### 実行スクリプト

```delete_volume.yml
---
- name: Delete target volume
  hosts: localhost
  vars:
    powervc_ip: <PowerVC IP address>      #<= 要編集
    tenant_id: <PowerVC Tenant ID>　　　   #<= 要編集
    target_volume_name: test0824          #<= 対象のボリューム名, 要編集

  tasks:
   - name: Get auth token
     uri:
       url: "https://{{ powervc_ip }}:5000/v3/auth/tokens"
       method: POST
       headers:
         Accept: application/json
       body: "{{ lookup('template', 'vars/token.json') }}"    #<= 外部ファイル読み込み
       body_format: json
       return_content: yes
       status_code: 201
       validate_certs: no
     register: auth_token

   - name: Set variable OS_TOKEN
     set_fact: OS_TOKEN="{{ auth_token.x_subject_token }}"

   - name: Get volume list
     uri:
       url: 'https://{{ powervc_ip }}:9000/v3/{{ tenant_id }}/volumes'
       method: GET
       headers:
         X-Auth-Token: "{{ OS_TOKEN }}"
       return_content: yes
       validate_certs: no
     register: volume_list

   - name: Set Variable for target_volume_id 
     set_fact: volumetest={{ volume_list.json.volumes | selectattr("name", "eq",  target_volume_name ) }}

   - name: Set Variable target_volume_id 
     set_fact: target_volume_id="{{ item.id }}"
     with_flattened:
       - "{{ volumetest }}"
 
   - debug:   
       var: target_volume_id

   - name: Delete targe volume
     uri:
       url: 'https://{{ powervc_ip }}:9000/v3/{{ tenant_id }}/volumes/{{ target_volume_id }}'
       method: DELETE
       headers:
         X-Auth-Token: "{{ OS_TOKEN }}"
       return_content: yes
       status_code: 202
       validate_certs: no
     register: delete_volume

   - debug:   
       var: delete_volume
```


```vars/token.json
{
    "auth":{
       "scope":{
          "project":{
             "name":"ibm-default",
             "domain":{
                "name":"Default"
             }
          }
       },
       "identity":{
          "methods":[
             "password"
          ],
          "password":{
             "user":{
                "domain":{
                   "name":"Default"
                },
                "name":"<PowerVC 実行ユーザー>",                #<= 要編集
                "password":"<PowerVC 実行ユーザー・パスワード>"   #<= 要編集
             }
          }
       }
    }
}
```

---
・実行前に対象ボリュームの存在確認
![スクリーンショット 2022-08-24 15.43.30.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/517421/ca7655c3-4250-e025-3f4b-b9259e9b0b36.png)


---
#### 実行


```
$ ansible-playbook delete_volume.yml

PLAY [Delete target volume] *****************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [localhost]

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

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

TASK [Get volume list] **********************************************************************************************************************************************
ok: [localhost]

TASK [Set Variable for target_volume_id] ****************************************************************************************************************************
ok: [localhost]

TASK [Set Variable target_volume_id] ********************************************************************************************************************************
ok: [localhost] => (item={'id': 'xxxxxx8-0xx0-xxxx-xx00-02xxxx592xxx', 'name': 'test0824', 'links': [{'rel': 'self', 'href': 'https://xx.xxx.xx.xxx/v3/xxx85xxx15xxxfbbxxxx/volumes/xxxxxx8-0xx0-xxxx-xx00-02xxxx592xxx'}, {'rel': 'bookmark', 'href': 'https://xx.xxx.xx.xxx:9000/xxx85xxx15xxxfbbxxxx/volumes/xxxxxx8-0xx0-xxxx-xx00-02xxxx592xxx'}]})

TASK [debug] ********************************************************************************************************************************************************
ok: [localhost] => {
    "target_volume_id": "xxxxxx8-0xx0-xxxx-xx00-02xxxx592xxx"
}

TASK [Delete targe volume] ******************************************************************************************************************************************
ok: [localhost]

TASK [debug] ********************************************************************************************************************************************************
ok: [localhost] => {
    "delete_volume": {
        "changed": false,
        "connection": "close",
        "content": "",
        "content_length": "0",
        "content_type": "text/html; charset=UTF-8",
        "cookies": {},
        "cookies_string": "",
        "date": "Wed, 24 Aug 2022 06:48:43 GMT",
        "elapsed": 0,
        "failed": false,
        "msg": "OK (0 bytes)",
        "openstack_api_version": "volume 3.0",
        "redirected": false,
        "server": "Apache",
        "status": 202,
        "url": "https://xx.xxx.xx.xxx:9000/v3/xxx85xxx15xxxfbbxxxx/volumes/xxxxxx8-0xx0-xxxx-xx00-02xxxx592xxx",
        "vary": "OpenStack-API-Version,User-Agent",
        "x_openstack_request_id": "req-57a94786-bd68-4192-aede-ba857f4f4e80"
    }
}

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

$
```


---
・実行後に GUI で対象ボリュームが存在しないことを確認

![スクリーンショット 2022-08-24 15.52.14.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/517421/d7b41687-b869-f9b8-9e24-e78c6e95e939.png)



---
#### 課題

ボリューム名から削除するために、ボリューム名から削除対象のボリュームIDを取得する箇所で試行錯誤しました。
もう少し簡単な書き方があれば見つかれば修正する予定です。



以上です。