Skip to content

Instantly share code, notes, and snippets.

@rhoboro
Last active July 30, 2017 02:37
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 rhoboro/46076a9286d2951a28656b9f3eb6af8b to your computer and use it in GitHub Desktop.
Save rhoboro/46076a9286d2951a28656b9f3eb6af8b to your computer and use it in GitHub Desktop.
GCEインスタンスを作成=>ping=>削除するAnsibleのPlaybook
# [http://docs.ansible.com/ansible/latest/guide_gce.html](http://docs.ansible.com/ansible/latest/guide_gce.html)
# [gce - create or terminate GCE instances](http://docs.ansible.com/ansible/latest/gce_module.html)
# [AnsibleでGCEサーバーをセットアップする](https://blog.1q77.com/2014/07/ansible-gce/)
---
- name: Create instance
hosts: localhost
connection: local
gather_facts: no
vars:
# GCE管理者に加えてService Account User権限も必要?
service_account_email: XXX@XXX.iam.gserviceaccount.com
credentials_file: ./XXX.json
project_id: XXX
machine_type: f1-micro
image: debian-7
zone: asia-east1-a
tasks:
- name: Launch instances
gce:
instance_names: dev1
machine_type: "{{ machine_type }}"
image: "{{ image }}"
service_account_email: "{{ service_account_email }}"
credentials_file: "{{ credentials_file }}"
project_id: "{{ project_id }}"
zone: "{{ zone }}"
register: gce
- name: Wait for SSH to come up
wait_for: host={{ item.public_ip }} port=22 delay=10 timeout=60
with_items: "{{ gce.instance_data }}"
- name: Add host to groupname
add_host: hostname={{ item.public_ip }} groupname=new_instances
with_items: "{{ gce.instance_data }}"
- name: Manage new instances
hosts: new_instances
connection: ssh
vars:
ansible_ssh_private_key_file: ~/.ssh/google_compute_engine
tasks:
- name: Ping
ping:
- name: Delete instance
hosts: localhost
connection: local
gather_facts: no
vars:
service_account_email: XXX@XXX.iam.gserviceaccount.com
credentials_file: ./XXX.json
project_id: XXX
machine_type: f1-micro
image: debian-7
zone: asia-east1-a
tasks:
- name: Delete instances
gce:
instance_names: dev1
machine_type: "{{ machine_type }}"
image: "{{ image }}"
service_account_email: "{{ service_account_email }}"
credentials_file: "{{ credentials_file }}"
project_id: "{{ project_id }}"
zone: "{{ zone }}"
state: absent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment