Ansible azure module 測試小記 - azure_rm_virtualmachine
OS:  openSUSE Leap 42.3 in Azure
測試模組 
- azure_rm_publicipaddress
- azure_rm_virtualmachine
上次的文章 http://sakananote2.blogspot.tw/2018/01/ansible-azure-module.html 進行 azure 工具的初始化, 今天要針對模組其他功能來進行測試
執行登入 ( 如果還沒有裝 az 指令就請參考文章安裝 )
# az   login
To sign in, use a web browser to open the page https://aka.ms/devicelogin and enter the code XXXXXXX  to authenticate.
測試一下指令
# az   vm  list  --output   table
Name          ResourceGroup    Location
------------  ---------------  ----------
test20180214  SAKANATEST       eastus
之前因為是測試, 所以建立完驗證之後, 我就會立馬刪除驗證, 接下來就動手再建立一個
還沒有建立驗證機制前, 先觀察一下
# az  ad  sp list --output table | grep azure-cli
建立驗證相關資料
# az  ad  sp  create-for-rbac --query  '{"client_id": appId, "secret": password, "tenant": tenant}'
{
  "client_id": "33a44043-xxxx-xxxx-xxxx-663eee4fd112",
  "secret": "27dd256a-xxxx-xxxx-xxxx-b28128f641f9",
  "tenant": "4cd326d7-xxxx-xxxx-xxxx-df56dc9dabd4"
}
建立完成之後觀察, 這個時候會有一筆 azure-cli-時間的授權
# az  ad  sp  list --output  table | grep azure-cli
33a44043-xxxx-xxxx-xxxx-663eee4fd112  azure-cli-2018-02-14-06-03-46             0c2d4a46-xxxx-xxxx-xxxx-8b71baf02fb7  ServicePrincipal
接下來還要有 subscription_id
使用 az account 指令取得
$ az  account  show  --query  "{ subscription_id: id }"
{
  "subscription_id": "6a2bdf3b-XXXX-XXXX-XXXX-3371d3401feb"
}
建立 存放認証的目錄( 如果沒有的話 )
# mkdir   ~/.azure
使用剛剛的資料建立認証的檔案
# vi   ~/.azure/credentials
[default]
subscription_id=6a2bdf3b-XXXX-XXXX-XXXX-3371d3401feb
client_id=d06f8905-XXXX-XXXX-XXXX-3e0bcf22a853
secret=b7f0df5b-XXXX-XXXX-XXXX-8aaca284f706
tenant=4cd326d7-XXXX-XXXX-XXXX-df56dc9dabd4
建立好了就可以開始測試了
==== 測試 Module name:  azure_rm_publicipaddress  ====
目標要從 sakanatest 這個資源群組, 建立一個 mypublic_ip 的固定 IP
先以單一指令的方式來實施吧
# ansible  localhost  -m  azure_rm_publicipaddress  -a  'resource_group=sakanatest name=mypublic_ip  allocation_method=Static'
這邊出現錯誤訊息在 openSUSE Leap in Azure
Using /etc/ansible/ansible.cfg as config file
 [WARNING]: Could not match supplied host pattern, ignoring: all
 [WARNING]: provided hosts list is empty, only localhost is available
localhost | FAILED! => {
    "changed": false, 
    "failed": true, 
    "msg": "Do you have msrestazure installed? Try `pip install msrestazure`- No module named msrest.serialization"
}
但是 Cloud-Shell 那邊沒有問題 …… Orz
==== 以下操作是在 cloud-shell 的終端機 ====
# ansible  localhost  -m  azure_rm_publicipaddress  -a  'resource_group=sakanatest name=mypublic_ip  allocation_method=Static'
 [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
 [WARNING]: No inventory was parsed, only implicit localhost is available
 [WARNING]: Could not match supplied host pattern, ignoring: all
 [WARNING]: provided hosts list is empty, only localhost is available
localhost | SUCCESS => {
    "changed": true, 
    "state": {
        "dns_settings": {}, 
        "etag": "W/\"ff593fe3-XXXX-XXXX-XXXX-a3eb90006d47\"", 
        "idle_timeout_in_minutes": 4, 
        "ip_address": "213.192.72.82", 
        "location": "eastus", 
        "name": "mypublic_ip", 
        "provisioning_state": "Succeeded", 
        "public_ip_allocation_method": "Static", 
        "tags": null, 
        "type": "Microsoft.Network/publicIPAddresses"
    }
}
可以到 Azure Portal 上面確認 :)
接下來測試移除這個 IP
# ansible  localhost  -m  azure_rm_publicipaddress  -a  'resource_group=sakanatest name=mypublic_ip   state=absent'
 [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
 [WARNING]: No inventory was parsed, only implicit localhost is available
 [WARNING]: Could not match supplied host pattern, ignoring: all
 [WARNING]: provided hosts list is empty, only localhost is available
localhost | SUCCESS => {
    "changed": true, 
    "state": {
        "status": "Deleted"
    }
}
接下來嘗試除了靜態 IP 之外還指定 FQDN
$ ansible  localhost  -m  azure_rm_publicipaddress  -a  'resource_group=sakanatest name=mypublic_ip   domain_name_label=sakana   allocation_method=Static'
 [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
 [WARNING]: No inventory was parsed, only implicit localhost is available
 [WARNING]: Could not match supplied host pattern, ignoring: all
 [WARNING]: provided hosts list is empty, only localhost is available
localhost | SUCCESS => {
    "changed": true,
    "state": {
        "dns_settings": {
            "domain_name_label": "sakana",
            "fqdn": "sakana.eastus.cloudapp.azure.com",
            "reverse_fqdn": null
        },
        "etag": "W/\"a0f0b9ec-XXXX-XXXX-XXXX-1a040d22b080\"",
        "idle_timeout_in_minutes": 4,
        "ip_address": "52.234.150.39",
        "location": "eastus",
        "name": "mypublic_ip",
        "provisioning_state": "Succeeded",
        "public_ip_allocation_method": "Static",
        "tags": null,
        "type": "Microsoft.Network/publicIPAddresses"
    }
}
這個部份也沒有問題
接下來實驗動態 IP, 這邊發現一個有趣的現象, 即使是動態 IP, 在 Azure 上面也可以設定 FQDN, 在該IP 沒有被使用的時候, 微軟的 DNS 會回應 255.255.255.255 :)
$ ansible localhost -m azure_rm_publicipaddress  -a  'resource_group=sakanatest name=mypublic_ip   domain_name_label=sakana   allocation_method=Dynamic'
 [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
 [WARNING]: No inventory was parsed, only implicit localhost is available
 [WARNING]: Could not match supplied host pattern, ignoring: all
 [WARNING]: provided hosts list is empty, only localhost is available
localhost | SUCCESS => {
    "changed": true,
    "state": {
        "dns_settings": {
            "domain_name_label": "sakana",
            "fqdn": "sakana.eastus.cloudapp.azure.com",
            "reverse_fqdn": null
        },
        "etag": "W/\"82b70d89-XXXX-XXXX-XXXX-14df3a57f139\"",
        "idle_timeout_in_minutes": 4,
        "ip_address": null,
        "location": "eastus",
        "name": "mypublic_ip",
        "provisioning_state": "Succeeded",
        "public_ip_allocation_method": "Dynamic",
        "tags": null,
        "type": "Microsoft.Network/publicIPAddresses"
    }
}
==== 測試 Module name:  azure_rm_virtualmachine 模組 ====
在開始之前要先知道 vm image 相關資訊
所以使用 az vm image list 取得相關資訊
$ az  vm  image  list --output table
You are viewing an offline list of images, use --all to retrieve an up-to-date list
Offer          Publisher               Sku                 Urn                                                             UrnAlias             Version
-------------  ----------------------  ------------------  --------------------------------------------------------------  -------------------  ---------
CentOS         OpenLogic               7.3                 OpenLogic:CentOS:7.3:latest                                     CentOS               latest
CoreOS         CoreOS                  Stable              CoreOS:CoreOS:Stable:latest                                     CoreOS               latest
Debian         credativ                8                   credativ:Debian:8:latest                                        Debian               latest
openSUSE-Leap  SUSE                    42.2                SUSE:openSUSE-Leap:42.2:latest                                  openSUSE-Leap        latest
RHEL           RedHat                  7.3                 RedHat:RHEL:7.3:latest                                          RHEL                 latest
SLES           SUSE                    12-SP2              SUSE:SLES:12-SP2:latest                                         SLES                 latest
UbuntuServer   Canonical               16.04-LTS           Canonical:UbuntuServer:16.04-LTS:latest                         UbuntuLTS            latest
先來進行初步實驗, 
Lab: 使用預設值的方式來建立 VM
建立一個 azure_create_vm.yml 來測試
$ vi   azure_create_vm.yml
---
# Azure VM 相關測試
- name: use when conditionals and setup module
  hosts: localhost
  tasks:
   - name: Create Azure test VM
     azure_rm_virtualmachine:
       resource_group: sakanatest
       name: test20180214-5
       admin_username: sakana
       admin_password: xxxxxxxx
# 經實驗 vm_size 是必須的
       vm_size: Basic_A1
# image 相關資訊可以使用 az vm image list --output table 查詢
       image:
         offer: CentOS
         publisher: OpenLogic
         sku: '7.1'
         version: latest
使用 ansible-playbook 指令建立 VM
$ ansible-playbook    azure_create_vm.yml
建立完成後可以到 Portal 觀察
Lab: 移除剛剛建立的 VM
建立一個 azure_remove_vm.yml 來測試
$ vi   azure_remove_vm.yml
---
# Azure VM 相關測試
- name: use when conditionals and setup module
  hosts: localhost
  tasks:
   - name: Remove Azure test VM
     azure_rm_virtualmachine:
       resource_group: sakanatest
       name: test20180214-5
       state: absent
使用 ansible-playbook 進行 VM 移除
$ ansible-playbook  -v  azure_remove_vm.yml
PLAY [use when conditionals and setup module] **************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [localhost]
TASK [Remove Azure test VM] ********************************************************************************************************************************************
changed: [localhost] => {"ansible_facts": {"azure_vm": null}, "changed": true, "deleted_managed_disk_ids": [], "deleted_network_interfaces": ["test20180214-501"], "dele
ted_public_ips": ["test20180214-501"], "deleted_vhd_uris": ["https://test2018021456904.blob.core.windows.net/vhds/test20180214-5.vhd"], "powerstate_change": null}
PLAY RECAP *************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0
- 這邊觀察一下, 如果沒有下額外參數, 會移除
- Managed_disk
- Network_interfaces
- public _ips
- Deleted_vhd_uris
- NSG 是不會跟著移除的, 儲存體也不會跟著移除
Lab: 嘗試修改範例建立 openSUSE Leap 42.3
修改 azure_create_vm.yml 來測試
$ cat   azure_create_vm.yml
---
# Azure VM 相關測試
- name: use when conditionals and setup module
  hosts: localhost
  tasks:
   - name: Create Azure test VM
     azure_rm_virtualmachine:
       resource_group: sakanatest
       name: test20180214-6
       admin_username: sakana
       admin_password: xxxxxxxx
# 經實驗 vm_size 是必須的
       vm_size: Basic_A1
# image 相關資訊可以使用 az vm image list --output table 查詢
       image:
         offer: openSUSE-Leap
         publisher: SUSE
         sku: '42.3'
         version: latest
使用 ansible-playbook 建立 VM with openSUSE Leap 42.3
$ ansible-playbook  azure_create_vm.yml
到 Portal 觀察資訊
Lab: 實驗控制 VM 狀態
停止指定的 VM
建立 azure_stop_vm.yml
$ vi   azure_stop_vm.yml
---
# Azure VM 相關測試
- name: use when conditionals and setup module
  hosts: localhost
  tasks:
   - name: Stop Azure test VM
     azure_rm_virtualmachine:
       resource_group: sakanatest       
       name: test20180214-6       
       started: no
使用 ansible-playbook 指令停止 VM
$ ansible-playbook   azure_stop_vm.yml
到 Portal 觀察資訊
接下來測試啟動 VM
$ vi   azure_start_vm.yml
---
# Azure VM 相關測試
- name: use when conditionals and setup module
  hosts: localhost
  tasks:
   - name: Start Azure test VM
     azure_rm_virtualmachine:
       resource_group: sakanatest
       name: test20180214-6
       started: yes
使用 ansible-playbook 指令啟動 VM
$ ansible-playbook   azure_start_vm.yml
到 Portal 觀察資訊
接下來嘗試 de-allocate VM
$ vi   azure_deallocate_vm.yml
---
# Azure VM 相關測試- name: use when conditionals and setup module
  hosts: localhost
  tasks:
   - name: Deallocate Azure test VM
     azure_rm_virtualmachine:
       resource_group: sakanatest
       name: test20180214-6
       allocated: no
使用 ansible-playbook 指令 進行 deallocate VM 
$ ansible-playbook  -v azure_deallocate_vm.yml
- De-allocate 與 Stop 差異看網路上面的說法是 Stop 還是會分配核心, 還是會計費, de-allocate 不會
以上就是針對 azure_rm_publicipaddress 與 azure_rm_virtualmachine 兩個模組的練習
又往Azure module 前進一步
Reference
~ enjoy it
 
 
沒有留言:
張貼留言