LoginSignup
6
6

More than 5 years have passed since last update.

ansibleでdigital_oceanのdroplet作成

Last updated at Posted at 2014-07-29

まとめ

  • ansibleにはdigital_oceanモジュールが用意されている。http://docs.ansible.com/digital_ocean_module.html
  • digital_oceanモジュールは、ansible/ansible-playbookを実行するホスト上からではなく、そのターゲットホスト上からpython+dopyを使ってdigital oceanのAPIを叩く
  • なので、ターゲットホスト上にpythonとdopyがインストールされている必要がある
  • dopyはgithub上に公開されており、API v1, v2に対応している https://github.com/devo-ps/dopy
    • v1 こんな感じ do.new_droplet('new_droplet', 66, 1601, 1)
    • v2 こんな感じ do.new_droplet('new_droplet', '512mb', 'lamp', 'ams2')
  • digital_oceanモジュールは、API v1にもとづいており、region,size,imageなどを数字で指定せねばいけない(これはそのうちv2ベースになるのでは)
  • dopy単体だと、API v2が使える。

やりたかったこと

Vagrantでdigital_oceanのdropletを作り、ansibleでconfig設定していたが、ansibleだけでなんとかならないか調べてみた。

わかったこと

ansibleのdigital_oceanモジュールを使う。
http://docs.ansible.com/digital_ocean_module.html

直接Digital OceanのAPIを叩くのではなく、targetホスト上からpython+dopyを使ってAPIを叩く模様。
ただし、targetとなるホスト上に、pythonのdigital ocean操作ライブラリであるdopyをインストールしておく必要がある。

  • region_idは何番がどこか不明(4=nyc2だけ確認した)
  • image_idはcurl -X GET "https://api.digitalocean.com/v2/images" -H "Authorization: Bearer xxxxxxxxxxxxx"でわかる。(3448641=centos6.5 x64)
  • size_idは、拾ったが確認はしていない。

digital_ocean.yml
- hosts: host1
  user: root
  tasks:
    - yum: name=python-dopy enablerepo=epel
    - digital_ocean: >
          state=present
          command=droplet
          name=admin1
          client_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
          api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
          size_id=66
          region_id=4
          image_id=3448641
          wait_timeout=500
      register: my_droplet
    - debug: msg="ID is {{ my_droplet.droplet.id }}"
    - debug: msg="IP is {{ my_droplet.droplet.ip_address }}"

(追記) image, size, regionのid取得

その後、dopyを触ってみて、ansibleを通すより、python-dopyを使ってdropletを作ってもいいような気がしてきた。
https://pypi.python.org/pypi/dopy

image,size,regionのidを取得する方法がわかったので、2014/7/30時点の結果を貼っておく。

## images ######
id: 1601, name: CentOS 5.8 x64
id: 1602, name: CentOS 5.8 x32
id: 12573, name: Debian 6.0 x64
id: 12575, name: Debian 6.0 x32
id: 14097, name: Ubuntu 10.04 x64
id: 14098, name: Ubuntu 10.04 x32
id: 376568, name: CentOS 6.4 x32
id: 562354, name: CentOS 6.4 x64
id: 3100616, name: Ubuntu 12.04.4 x32
id: 3101045, name: Ubuntu 12.04.4 x64
id: 3102721, name: Fedora 19 x32
id: 3102879, name: Fedora 19 x64
id: 3121555, name: Ghost 0.4.2 on Ubuntu 12.04
id: 3243143, name: Fedora 20 x32
id: 3243145, name: Fedora 20 x64
id: 3445812, name: Debian 7.0 x64
id: 3445920, name: Debian 7.0 x32
id: 3448641, name: CentOS 6.5 x64
id: 3448674, name: CentOS 6.5 x32
id: 3935257, name: Django on Ubuntu 14.04
id: 3961756, name: LAMP on Ubuntu 14.04
id: 4204318, name: MEAN on Ubuntu 14.04
id: 4261622, name: Ruby on Rails on Ubuntu 14.04 (Nginx + Unicorn)
id: 4295378, name: node-v0.10.29 on Ubuntu 14.04
id: 4547332, name: LEMP on Ubuntu 14.04
id: 4582138, name: GitLab 7.0.0 CE
id: 4856048, name: CentOS 7.0 x64
id: 4869208, name: Redmine on Ubuntu 14.04
id: 4970653, name: Docker 1.1.1 on Ubuntu 14.04
id: 4991187, name: WordPress on Ubuntu 14.04
id: 5099646, name: Dokku v0.2.3 on Ubuntu 14.04 (w/ Docker 1.1.1)
id: 5141286, name: Ubuntu 14.04 x64
id: 5142677, name: Ubuntu 14.04 x32
## regions ######
id: 3, name: San Francisco 1
id: 4, name: New York 2
id: 5, name: Amsterdam 2
id: 6, name: Singapore 1
id: 7, name: London 1
## sizes ######
id: 60, name: 32GB
id: 61, name: 16GB
id: 62, name: 2GB
id: 63, name: 1GB
id: 64, name: 4GB
id: 65, name: 8GB
id: 66, name: 512MB
id: 69, name: 64GB
id: 70, name: 48GB

ソース

dopy_smpl.py
#!/bin/env python

def opts_and_args():
  import optparse

  parser = optparse.OptionParser()
  parser.add_option("-c", "--clientid")
  parser.add_option("-a", "--apikey")
  return parser.parse_args()


def dopy_manager(clientkey,apikey):
  from dopy.manager import DoManager
  return DoManager(clientkey,apikey)

if __name__ == '__main__':

  (opts,args) = opts_and_args()

  do = dopy_manager(opts.clientid, opts.apikey)

  import json

  print "## images ######"
  for item in do.all_images():
    print "id: {0}, name: {1}".format(item['id'],item['name'])

  print "## regions ######"
  for item in do.all_regions():
    print "id: {0}, name: {1}".format(item['id'],item['name'])

  print "## sizes ######"
  for item in do.sizes():
    print "id: {0}, name: {1}".format(item['id'],item['name'])

6
6
3

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
6
6