IBM Cloud PowerSystems Virtual Server を Ansible(galaxy: ibm.cloudcollection)でデプロイする


ansible-falaxy ibm.cloudcollection を使用して、Ansible で PowerVirtualServer(AIX)をデプロイした実行ログです。

次のコードを参考にしています
https://github.com/IBM-Cloud/ansible-collection-ibm/tree/master/examples/simple-vm-power-vs


実行環境

 Amsible Host: Local PC (Mac)
 Ansible version : 2.9.9
 Python : 3.8.3

デプロイ対象環境

 IBM Cloud PowerVirtualServer サービス

playbook作成のための用意

・ssh public key の作成を行っておく

・Public Network の事前定義 (CLI or GUI)
  (* playbook 内での作成もできたのですが、時間がかかって timeout error が発生したため、
  事前定義に変更しました。)

参考:
IBM Power Systems Virtual Servers CLI plug-in

ibmcloud pi network-create-public コマンド


実行

シナリオ: AIX をデプロイし ansible で ssh 接続


1) 実行環境で ansible-galaxy ibm.cloudcollection のインストール

$ ansible-galaxy collection install ibm.cloudcollection
Process install dependency map
Starting collection install process
Installing 'ibm.cloudcollection:1.13.1' to '/Users/test/.ansible/collections/ansible_collections/ibm/cloudcollection'

2) power_instance_id の事前取得(IBM Cloud 対象アカウントに CLI ログイン後にibmcloud pi sl の実行)

$ ibmcloud pi service-list
Listing services under account IBM - <USER ACCOUNT>  as user <login user> ..
ID                                                                                                                    Name
crn:v1:bluemix:public:power-iaas:us-south:a/52xxxxx0f3a4109ac0dxxx44xxxx6:<power_instance_id>   <Service Name>

"power_instance_id" の箇所をメモに控えておく。

・参考:ibmcloud pi service-list コマンド

3) Public ネットワークのID取得(ibmcloud pi networks)

$ ibmcloud pi networks
Listing networks under account IBM - ACCOUNT NAME as user test...
ID                                     Name                       Address
xxxx-39da-xxxx-84a1-xxxxx   demo-power-vm001-network   /pcloud/v1/cloud-instances/axxxfb6b4xxxx2b62xxxc3/networks/1fxxxxxxa-xxx45-84a1-xxxxd8f4xxx

ID列の値をメモに控える。

4) 使用するイメージのIDを取得 (ibmcloud pi images 実行)

$ ibmcloud pi images | grep 7200-04-01
xxxxxxxx-cxxc-4xxb-xx4e-7e4xxxxxx   7200-04-01                       /pcloud/v1/cloud-instances/adxxxxxxxx6b4xxxxx000a2xxxxx3/images/dxxxxx49-xx5c-xxxxx-xx4e-7exxxxx

左端の値をメモに控える。

5) 実行する playbook の用意 (create_vm.yml)

create_vm.yml
---
- name: POWER VSI Creation Demo
  hosts: localhost
  collections:
   - ibm.cloudcollection
  vars:
    pi_name: demo-power-vm001                       #VM名
    sys_type: e880                      #サーバー・タイプ
    pi_image_id: "xxxxxxxx-cxxc-4xxb-xx4e-7e4xxxxxx"         # 4)で確認したimage idを記載
    proc_type: shared               # プロセッサー・タイプ
    processors: "0.2"           # CPU
    memory: "2"                         # Memory(GB)
    pi_cloud_instance_id: "ef5xxxx-0e8c-42xxx7-81xxx-45b96xxxxfd"  # 2)で確認したpower_instance_idを記載
    ssh_public_key: "<ssh key public key の文字列>"
    pi_network_id: "xxxx-39da-xxxx-84a1-xxxxx"   # 3)で確認したpublic networkの idを記載

  tasks:
    - name: Add new SSH Key
      ibm_pi_key:
        pi_key_name: "{{ pi_name }}-ssh-key"
        pi_ssh_key: "{{ ssh_public_key }}"
        pi_cloud_instance_id: "{{ pi_cloud_instance_id }}"
      register: pi_ssh_key_create_output

    - name: Create a POWER Virtual Server Instance
      ibm_pi_instance:
        state: available
        pi_memory: "{{ memory }}"
        pi_processors: "{{ processors }}"
        pi_instance_name: "{{ pi_name }}"
        pi_proc_type: "{{ proc_type }}"
        pi_image_id: "{{ pi_image_id }}"
        pi_volume_ids: []
        pi_network_ids: "{{ pi_network_id }}"
        pi_key_pair_name: "{{ pi_name }}-ssh-key"
        pi_sys_type: "{{ sys_type }}"
        pi_replication_policy: none
        pi_replication_scheme: suffix
        pi_replicants: "1"
        pi_cloud_instance_id: "{{ pi_cloud_instance_id }}"
        id: "{{ pi_instance.resource.id | default(omit) }}"
      register: pi_instance_create_output

    - name: Save new Power VSI fact
      set_fact:
        cacheable: True
        pi_instance: "{{ pi_instance_create_output.resource }}"
      when: pi_instance_create_output.resource is defined

    - name: Print Public IP Address
      debug:
        var: pi_instance.addresses[0].external_ip

    - name: Add VSI to Ansible inventory
      add_host:
        name: "{{ pi_instance.addresses[0].external_ip }}"
        ansible_user: root
        groups: new_vsi
        ansible_ssh_extra_args: -o StrictHostKeyChecking=no

- name: Connect to VSI
  hosts: new_vsi
  gather_facts: False
  tasks:
    - name: Wait for VSI to become reachable over SSH
      wait_for_connection:

    - name: Collect OS login message
      command: cat /etc/motd
      register: os_motd

    - name: Print MOTD
      debug:
        var: os_motd.stdout_lines

6) ansible.cfg の用意

ansible.cfg
[default]
remote_user = root
private_key_file = ./id_rsa_demo_vm001    # デプロイするVMに登録した ssh-key のprivate key を指定

7) 実行環境で IC_API_KEY のexport

IBM Cloud API Key の作成参考 :API キーの作成
https://cloud.ibm.com/docs/account?topic=account-userapikey&locale=ja#create_user_key

Ansible 実行環境のターミナルで IC_API_KEY 変数の export を行う。

export IC_API_KEY=<API KEY>

( * API KEY 部分は実際の値に置き換えます)

8) デプロイ実行

$ ansible-playbook create_vm.yml
[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 Creation Demo] ***************************************************************************************************

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

TASK [Add new SSH Key] ***********************************************************************************************************
changed: [localhost]

TASK [Create a POWER Virtual Server Instance] ************************************************************************************
changed: [localhost]

TASK [Save new Power VSI fact] ***************************************************************************************************
ok: [localhost]

TASK [Print Public IP Address] ***************************************************************************************************
ok: [localhost] => {
    "pi_instance.addresses[0].external_ip": "XXX.XXX.XX.XXX"
}

TASK [Add VSI to Ansible inventory] **********************************************************************************************
changed: [localhost]

PLAY [Connect to VSI] ************************************************************************************************************

TASK [Wait for VSI to become reachable over SSH] *********************************************************************************
ok: [XXX.XXX.XX.XXX]

TASK [Collect OS login message] **************************************************************************************************
[WARNING]: Platform aix on host XXX.XXX.XX.XXX is using the discovered Python interpreter at /usr/bin/python, but future
installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
changed: [XXX.XXX.XX.XXX]

TASK [Print MOTD] ****************************************************************************************************************
ok: [XXX.XXX.XX.XXX] => {
    "os_motd.stdout_lines": [
        "*******************************************************************************",
        "*                                                                             *",
        "*                                                                             *",
        "*  Welcome to AIX Version 7.2!                                                *",
        "*                                                                             *",
        "*                                                                             *",
        "*  Please see the README file in /usr/lpp/bos for information pertinent to    *",
        "*  this release of the AIX Operating System.                                  *",
        "*                                                                             *",
        "*                                                                             *",
        "*******************************************************************************"
    ]
}

PLAY RECAP ***********************************************************************************************************************
XXX.XXX.XX.XXX             : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
localhost                  : ok=6    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

成功しました!

9) GUIで確認
・ssh_key が登録されています

・VMが作成されています


まとめ

Ansible Collection ibm.cloudcollectionで PowerVirtualServer のデプロイができることを確認しました。
Ansible の変数(イメージなど)を変更すれば、IBM i や Linux もデプロイできると思われます。

今回試していなかった、private ネットワークの作成、接続やボリューム作成、接続、AIX に対する ansible での構成変更の実行は今後の宿題です。


関連記事: ibmcloud CLI で IBM PowerSystems Virtual Server on IBM Cloud サービスに AIXサーバーをデプロイする

以上。