Skip to content

Instantly share code, notes, and snippets.

@curious-eyes
Last active September 14, 2015 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save curious-eyes/f2e96cd54049e0779444 to your computer and use it in GitHub Desktop.
Save curious-eyes/f2e96cd54049e0779444 to your computer and use it in GitHub Desktop.
AnsibleでGCEインスタンスを管理する ref: http://qiita.com/curious-eyes/items/c7feb3edbeb7c7c640e6
$ pip install apache-libcloud
~/gce_ansible/
play.sh # playbook実行シェルスクリプト
master.yml # master playbook
credentials/ # 証明書管理Dir
cacert.pem # libcloud用 CA bundleファイル
pkey.pem # GCE用 証明書ファイル
secrets.py # 証明書指定ファイル
inventory/ # inventory管理用Dir
gce.ini # GCE用設定ファイル
gce.py # GCE用モジュール
hosts # inventoryファイル
vars/
gce_auth.yml # GCE認証情報変数
instance.yml # GCEinstance設定変数
- name: Create new GCE instances
hosts: localhost
gather_facts: no
vars_files:
- "vars/instance.yml"
- "vars/gce_auth.yml"
tasks:
- name: Launch instances
local_action:
module: gce
instance_names: "{{ names }}"
machine_type: "{{ type }}"
image: "{{ image }}"
zone: "{{ zone }}"
service_account_email: "{{ service_account_email }}"
pem_file: "{{ pem_file }}"
project_id: "{{ project_id }}"
tags: webserver
service_account_email: i...@project.googleusercontent.com
pem_file: ~/gce_ansible/credentials/pkey.pem
project_id: project-name
names: www1
type: f1-micro
image: debian-7
zone: us-central1-b
#!/bin/bash
PLAYBOOK="$1"
if [ -z $PLAYBOOK ]; then
echo "You need to pass a playback as argument to this script."
exit 1
fi
export GCE_INI_PATH=$(pwd)/inventory/gce.ini
export SSL_CERT_FILE=$(pwd)/credentials/cacert.pem
export ANSIBLE_HOST_KEY_CHECKING=False
if [ ! -f "$SSL_CERT_FILE" ]; then
curl -O http://curl.haxx.se/ca/cacert.pem
fi
ansible-playbook -v -i inventory/ "$PLAYBOOK"
$ cd ~/gce_ansible
$ ./play.sh master.yml
PLAY [Create new GCE instances] ******************
...(略)...
PLAY RECAP ********************************************************************
127.0.0.1 : ok=1 changed=1 unreachable=0 failed=0
RuntimeError: No CA Certificates were found in CA_CERTS_PATH.
$ openssl pkcs12 -in (p12ファイルパス).p12 -passin pass:notasecret -nodes -nocerts | openssl rsa -out ~/gce_ansible/credentials/pkey.pem
GCE_PARAMS = ('i...@project.googleusercontent.com', '~/gce_ansible/credentials/pkey.pem')
GCE_KEYWORD_PARAMS = {'project': 'project-name'}
[localhost]
127.0.0.1
- contrib/inventory/gce.ini
- contrib/inventory/gce.py
libcloud_secrets = /Users/shuhei/gce_ansible/credentials/secrets.py
$ cd ~/gce_ansible
$ export SSL_CERT_FILE=$HOME/gce_ansible/credentials/cacert.pem # Mac OSX の場合のみ
$ ./inventory/gce.py --list
$ cd ~/gce_ansible
$ export GCE_INI_PATH=$HOME/gce_ansible/inventory/gce.ini
$ ansible all -i inventory/gce.py -m setup
hostname | success >> {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"x.x.x.x"
],
....
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment