Skip to content

Instantly share code, notes, and snippets.

@atsaki
Last active December 23, 2015 17:35
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 atsaki/552bdef03b995f3ef732 to your computer and use it in GitHub Desktop.
Save atsaki/552bdef03b995f3ef732 to your computer and use it in GitHub Desktop.
AnsibleでCloudStackを操作する(基礎編:仮想マシン作成とプロビジョニング) ref: http://qiita.com/atsaki/items/6db23d3f4aa3a3c5da41
CLOUDSTACK_ENDPOINT=<APIのエンドポイント>
CLOUDSTACK_KEY=<APIキー>
CLOUDSTACK_SECRET=<シークレットキー>
CLOUDSTACK_TIMEOUT=60
---
- hosts: cloudstack
connection: local
tasks:
- set_fact:
vm_cache_dir: "{{ cache_dir + '/' + inventory_hostname }}"
- name: "SSH公開鍵の削除"
cs_sshkeypair:
name: "{{ inventory_hostname }}"
state: absent
- name: "取得済みIPアドレスの確認"
stat:
path: "{{ vm_cache_dir + '/ip_address' }}"
register: cache_ip_address
- name: "IPアドレスのキャッシュ読み込み"
set_fact:
ip_address: "{{ lookup('file', vm_cache_dir + '/ip_address') }}"
when: cache_ip_address.stat.exists
- name: "IPアドレスの解放"
cs_ip_address:
zone: "{{ zone_name }}"
network: "{{ network_name }}"
ip_address: "{{ ip_address }}"
state: absent
when: cache_ip_address.stat.exists
- name: "IPアドレスキャッシュの削除"
file:
path: "{{ vm_cache_dir + '/ip_address' }}"
state: absent
when: cache_ip_address.stat.exists
- name: "VMの削除"
cs_instance:
name: "{{ inventory_hostname }}"
zone: "{{ zone_name }}"
state: expunged
- name: "キャッシュ用ディレクトリの削除"
file:
path: "{{ vm_cache_dir }}"
state: absent
---
ssh_public_key_file: "./ansible_ssh.pub"
zone_name: "joule"
network_name: "joule-network1"
service_offering_name: "light.S1"
template_name: "CentOS 7.1 64-bit"
firewall_rules:
- { protocol: "tcp", start_port: 22, end_port: 22 }
port_forwarding_rules:
- { protocol: "tcp", public_port: 22, private_port: 22 }
- name: "SSHで接続可能になるまで待機"
wait_for:
host: "{{ ip_address }}"
port: "{{ ansible_ssh_port|default(22) }}"
search_regex: "OpenSSH"
- name: "authorized_keysの設定に時間がかかる場合があるので余分に待機"
pause: minutes=2
when: vm|changed
FROM python:2.7
RUN pip install cs ansible sshpubkeys
RUN mkdir -p /usr/share/ansible && \
cd /usr/share/ansible && \
git clone https://github.com/resmo/ansible-cloudstack.git
RUN mkdir -p /etc/ansible ; \
echo "[defaults]" > /etc/ansible/ansible.cfg; \
echo "host_key_checking = False" >> /etc/ansible/ansible.cfg; \
echo "library = /usr/share/ansible/ansible-cloudstack" >> /etc/ansible/ansible.cfg
$ git clone https://github.com/resmo/ansible-cloudstack.git library
$ pip install cs sshpubkeys
$ ansible-playbook -i hosts PLAYBOOK.yml
$ docker run --env-file=.env -v $(pwd):/data -w /data --rm ansible ¥
ansible-playbook -i hosts PLAYBOOK.yml
$ ssh -i ansible_ssh root@$(cat cache/nginx01/ip_address)
$ docker build -t ansible .
$ docker run --rm ansible ansible --version
ansible 1.9.4
configured module search path = None
$ export CLOUDSTACK_ENDPOINT=<APIのエンドポイント>
$ export CLOUDSTACK_KEY=<APIキー>
$ export CLOUDSTACK_SECRET=<シークレットキー>
$ export CLOUDSTACK_TIMEOUT=60
# ローカルのAnsibleを使用する場合は
$ cs listZones
# Dockerを使用する場合
$ docker run --env-file=.env --rm ansible cs listZones
$ ssh-keygen -f ansible_ssh -N ""
[nginx]
nginx01
[cloudstack:children]
nginx
[all:vars]
ansible_ssh_user=root
ansible_ssh_private_key_file=./ansible_ssh
ansible_python_interpreter=python
cache_dir=./cache
---
firewall_rules:
- { protocol: "tcp", start_port: 22, end_port: 22 }
- { protocol: "tcp", start_port: 80, end_port: 80 }
port_forwarding_rules:
- { protocol: "tcp", public_port: 22, private_port: 22 }
- { protocol: "tcp", public_port: 80, private_port: 80 }
```yaml:nginx.yml
---
- hosts: nginx
gather_facts: no
pre_tasks:
- include: set_ansible_ssh_host.yml
tasks:
- name: "Nginxをインストール(Ubuntu)"
apt:
name: "nginx"
update_cache: yes
when: ansible_distribution == 'Ubuntu'
- name: "EPELをインストール(CentOS)"
yum:
name: "epel-release"
when: ansible_distribution == 'CentOS'
- name: "Nginxをインストール(CentOS)"
yum:
name: "nginx"
when: ansible_distribution == 'CentOS'
- name: "Nginxを起動"
service:
name: "nginx"
enabled: yes
state: started
---
- set_fact:
vm_cache_dir: "{{ cache_dir + '/' + inventory_hostname }}"
- name: "取得済みIPアドレスの確認"
stat:
path: "{{ vm_cache_dir + '/ip_address' }}"
register: cache_ip_address
connection: local
failed_when: not cache_ip_address.stat.exists
- name: "ansible_ssh_hostに取得済みのIPをセット"
set_fact:
ansible_ssh_host: "{{ lookup('file', vm_cache_dir + '/ip_address') }}"
- name: "仮想マシンのステータスを確認"
cs_instance:
name: "{{ inventory_hostname }}"
zone: "{{ zone_name }}"
state: present
connection: local
register: instance
- name: "ホスト情報の取得"
setup:
when: instance.state == "Running"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment