LoginSignup
3
2

More than 5 years have passed since last update.

libcloudを使用しないでGoogleComputeEngineのDynamicInventoryScriptを使ってみる

Posted at

環境

  • OSX 10.10.2 (Yosemite)
  • Python 2.7.9 (OSXにデフォルトインストールされてるPython)

必要なライブラリのインストール

※ 自分の環境ではこれを入れないと動かなかった

$ brew install gmp
$ sudo pip install pycrypto

インベントリスクリプトとiniを取得

$ wget https://github.com/ansible/ansible/blob/devel/plugins/inventory/gce.ini -P ~/.ansible
$ wget https://raw.githubusercontent.com/ansible/ansible/devel/plugins/inventory/gce.py -P ~/.ansible
$ chmod 755 ~/.ansible/gce.py

gce.iniに設定を記述(libcloudを使わない)

#!/usr/bin/python
# Copyright 2013 Google Inc.
#
# This file is part of Ansible
#

...

[gce]
# GCE Service Account configuration information can be stored in the
# libcloud 'secrets.py' file.  Ideally, the 'secrets.py' file will already
# exist in your PYTHONPATH and be picked up automatically with an import
# statement in the inventory script.  However, you can specify an absolute
# path to the secrets.py file with 'libcloud_secrets' parameter.
libcloud_secrets = 

# If you are not going to use a 'secrets.py' file, you can set the necessary
# authorization parameters here.
gce_service_account_email_address = example@developer.gserviceaccount.com
gce_service_account_pem_file_path = ~/.ansible/example.pem
gce_project_id = example-test-12345

動作確認

$ ~/.ansible/gce.py | jq .

{
  "tag_http-server": [
    "example"
  ],
  "asia-east1-a": [
    "example"
  ],
  "f1-micro": [
    "example"
  ],
  "status_running": [
    "example"
  ],
  "persistent_disk": [
    "example"
  ],
  "network_default": [
    "example"
  ],
  "_meta": {
    "hostvars": {
      "example": {
        "gce_uuid": "aaaaaaaaaaaaaaaaaaaaaaaaaaa",
        "gce_public_ip": "000.000.000.000",
        "ansible_ssh_host": "000.000.000.000",
        "gce_private_ip": "00.000.00.0",
        "gce_id": "aaaaaaaaaaaaaaaaaaaa",
        "gce_image": null,
        "gce_description": null,
        "gce_machine_type": "f1-micro",
        "gce_tags": [
          "http-server"
        ],
        "gce_name": "example",
        "gce_zone": "asia-east1-a",
        "gce_status": "RUNNING",
        "gce_network": "default",
        "gce_metadata": {
          "os": "example"
        }
      }
    }
  }
}
3
2
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
3
2