-
-
Save atsaki/552bdef03b995f3ef732 to your computer and use it in GitHub Desktop.
AnsibleでCloudStackを操作する(基礎編:仮想マシン作成とプロビジョニング) ref: http://qiita.com/atsaki/items/6db23d3f4aa3a3c5da41
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CLOUDSTACK_ENDPOINT=<APIのエンドポイント> | |
CLOUDSTACK_KEY=<APIキー> | |
CLOUDSTACK_SECRET=<シークレットキー> | |
CLOUDSTACK_TIMEOUT=60 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ git clone https://github.com/resmo/ansible-cloudstack.git library |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ pip install cs sshpubkeys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ansible-playbook -i hosts PLAYBOOK.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ docker run --env-file=.env -v $(pwd):/data -w /data --rm ansible ¥ | |
ansible-playbook -i hosts PLAYBOOK.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ssh -i ansible_ssh root@$(cat cache/nginx01/ip_address) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ docker build -t ansible . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ docker run --rm ansible ansible --version | |
ansible 1.9.4 | |
configured module search path = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ export CLOUDSTACK_ENDPOINT=<APIのエンドポイント> | |
$ export CLOUDSTACK_KEY=<APIキー> | |
$ export CLOUDSTACK_SECRET=<シークレットキー> | |
$ export CLOUDSTACK_TIMEOUT=60 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ローカルのAnsibleを使用する場合は | |
$ cs listZones | |
# Dockerを使用する場合 | |
$ docker run --env-file=.env --rm ansible cs listZones |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ssh-keygen -f ansible_ssh -N "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- 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