Ansible with Digital Ocean 小記
OS: Ubuntu 16.04 in Digital Ocean
Digital Ocean API v2
建立 Access Token
首先登入 Digital Ocean 管理介面
點選 API
點選 Generate New Token
輸入 Token 名稱
點選 Generate Token
接下來就會看到剛剛建立的 token
Token 只會出現一次 ~~ 不會回來, 記得複製下來 ( 不然就重新建立一個 XD )
先來熟悉一下 API 用法好了
把剛剛的 token 設成變數
# export TOKEN=79314465c0318bc962873XXXXXXXXXX305ba65204032f9ff27cc88a53fa9ce81
列出 action
# curl -X GET "https://api.digitalocean.com/v2/actions" -H "Authorization: Bearer $TOKEN"
但是我覺得這樣的方式可讀性比較低, 所以我是搭配 jq
# curl -X GET "https://api.digitalocean.com/v2/actions" -H "Authorization: Bearer $TOKEN" | jq
列出 Droplets
把剛剛的 actions 換成 droplets 就可以
# curl -X GET "https://api.digitalocean.com/v2/droplets" -H "Authorization: Bearer $TOKEN" | jq
建立 Droplet
# curl -X POST "https://api.digitalocean.com/v2/droplets" -d '{"name":"My-Droplet","region":"nyc3","size":"512mb","image":"ubuntu-14-04-x64"}' -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" | jq
- 這邊就是指定 Droplet 的名稱, region, size 還有 image
Droplet API 的參數用法要看這邊
可以回到 dashboard 觀察
Name的部份, 支援 array 的方式, 所以如果要建立多個 Droplets, 就在 name 那邊用 array 的方式就可以建立多 Droplets
# curl -X POST "https://api.digitalocean.com/v2/droplets" -d '{"names":["sakana1",”sakana2”],"region":"nyc3","size":"512mb","image":"ubuntu-14-04-x64"}' -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" | jq
- 這邊要注意, 如果要建立多 droplets, 帶入的是 names 不是 name
那如果要刪除某一個 Droplet, 就要先去查詢他的 droplet id
- 可以用剛剛的方式加上 grep 來列 # curl -X GET "https://api.digitalocean.com/v2/droplets" -H "Authorization: Bearer $TOKEN" | grep '"id":'
然後就是 curl 使用 DELETE 加上 droplet 的 id
例如如果某一個 droplet id 是 88444260, 就會像下面這樣
# curl -X DELETE "https://api.digitalocean.com/v2/droplets/88444260" -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" | jq
另外有一個我們該掌握的就是有哪些 images 可以使用
# curl -X GET "https://api.digitalocean.com/v2/images" -H "Authorization: Bearer $TOKEN" | jq
接下來進入 ansible with Digital Ocean 的部份
因為 Digital Ocean 沒有 openSUSE ~~~ QQ
所以用 Ubuntu 16.04
登入到 ubuntu 16.04 終端機
安裝 python-pip
#apt-get install python-pip
使用 pip安裝 dopy
#pip install dopy
升級 pip
#pip install --upgrade pip
使用 pip 安裝 ansible
#pip2 install ansible
確認一下 ansible 的資訊
#ansible --version
在 ansible 裏面用的是 digital_ocean 的 module
我寫了幾個 playbook 在 github 上面, 可以參考
測試 1: 建立 單一 Droplet
# ansible-playbook create_do_vm.yml
[WARNING]: Unable to parse /etc/ansible/hosts 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'
Enter Digital Ocean API Token: 79314465c0318bc96287XXXXXXXXX4305ba65204032f9ff27cc88a53fa9ce81
Enter Droplet's name [test01]:
Enter region name [sgp1]:
Enter image name [ubuntu-16-04-x64]:
PLAY [use when conditional and setup module] ********************
- 這邊就是把 TOKEN 貼上, 然後輸入 Droplet 名稱, 區域還有 image 名稱就好, 我預設是 2gb
到 Dashboard 驗證一下
測試 2: 建立多個 Droplets
# ansible-playbook create_multi_do_vm.yml
[WARNING]: Unable to parse /etc/ansible/hosts 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'
Enter Digital Ocean API Token: 79314465c0318bc9628XXXXXXX73a455ba65204032f9ff27cc88a53fa9ce81
Enter sequence start number [1]: 2
Enter sequence end number, Max is 99 [10]: 3
Enter region name [sgp1]:
Enter image name [ubuntu-16-04-x64]:
PLAY [use when conditional and setup module] ******************************
- 這邊就是把 TOKEN 貼上, 然後輸入起始編號, 還有結束編號 , 區域還有 image 名稱就好, 我預設是 2gb
到 Dashboard 驗證一下
測試 3: 刪除 Droplet
因為刪除一個跟多個 Droplets 一樣
所以我就測試刪除 多個 Droplets
# ansible-playbook delete_multi_do_vm.yml
[WARNING]: Unable to parse /etc/ansible/hosts 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'
Enter Digital Ocean API Token: 79314465c0318bc962c94XXXXXXX305ba65204032f9ff27cc88a53fa9ce81
Enter sequence start number [1]:
Enter sequence end number, Max is 99 [10]: 3
PLAY [use when conditional and setup module] *************
今天又多學一個 ansible module :)
~ enjoy it
Reference
沒有留言:
張貼留言