LoginSignup
1
1

More than 3 years have passed since last update.

IBM Cloud IAM アクセス・トークンの取得 3つの方法 (ibmcloud cli, curlコマンド, Ansible)

Last updated at Posted at 2020-11-10

IBM Cloud IAM アクセス・トークンの取得

IBM Cloud での操作に使用する アクセス・トークンの取得を、ibmcloud cli、curl コマンド、Ansible での実行を確認しました。備忘録用メモです。

前提となる API キーの取得: IBM Cloud Docs: API キーの作成


1. ibmcloud コマンド

IBM Cloud Docs:アクセス・トークンの取得

< API key > には事前に取得したAPI キーを指定。

ログイン

$ ibmcloud login --apikey < API key > -r us-south -g <Resource Group>

IAM TOKEN 取得

$ ibmcloud iam oauth-tokens
IAM token:  Bearer xxx< TOKEN の長い文字列の表示 > xxxxxxxxxxxxx

変数 ACCESS_TOKEN に代入したい場合。--output JSON とjq コマンドを使用。

$ export ACCESS_TOKEN=$(ibmcloud iam oauth-tokens --output JSON | jq -r '.iam_token')

-> "Bearer" も代入されていることに注意。


2. curl コマンド

IBM Cloud Docs:API キーを使用した IBM Cloud IAM トークンの生成

< API key > には事前に取得したAPI キーを指定。

$ curl -X POST "https://iam.cloud.ibm.com/identity/token" -H "content-type: application/x-www-form-urlencoded" -H "accept: application/json" -d 'grant_type=urn%3Aibm%3Aparams%3Aoauth%3Agrant-type%3Aapikey&apikey=< API key >' | jq -r '.access_token')

変数 ACCESS_TOKEN に代入したい場合

$ export ACCESS_TOKEN=$(curl -X POST "https://iam.cloud.ibm.com/identity/token" -H "content-type: application/x-www-form-urlencoded" -H "accept: application/json" -d 'grant_type=urn%3Aibm%3Aparams%3Aoauth%3Agrant-type%3Aapikey&apikey=< API key >' | jq -r '.access_token')

-> "Bearer" は代入されないことに注意。


3. Ansible

Ansible での発行は curl コマンドを参考に、uri モジュールを使用。

playbook "ibmcloud_iam_token.yml" を以下のように作成。
変数 OS_TOKEN に代入しています。

ibmcloud_iam_token.yml
---
- name: IAM TOKEN
  hosts: localhost
  gather_facts : no

  tasks: 
  - name: Get IAM access 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>     # <= 事前に取得した 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
    debug:
        var: OS_TOKEN

$ ansible-playbook ibmcloud_iam_token.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 [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 の長い文字列の表示>xxxxx"
}

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

以上です。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1