Ansible で vSphere 上に複数の仮想マシンを作成する
以前にAnsible で vSphere ESXi に仮想マシンをデプロイするにはというメモを書きました。 RancherOS の検証をする際、何度も複数の RancherOS 仮想マシンを作成するのが面倒だったので Ansible で自動化してみました。 今回はひとつの Playbook で RancherOS-1 ~ 9 という複数の仮想マシンを作成します。
検証環境
今回は以下の環境でテストを実施しました。
- CentOS 7.4.1708
- Ansible 2.4.0
ansible と pysphere のインストール
事前に pysphere をインストールしておきます。
| yum -y install gcc python-devel
curl -L https://bootstrap.pypa.io/get-pip.py | python
pip install ansible pysphere
|
Playbook
以下の内容で Playbook を用意します。 今回は site.yml
というファイル名にしました。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 | ---
- hosts: all
gather_facts: false
connection: local
user: remote
sudo: true
tasks:
- vsphere_guest:
validate_certs: false
vcenter_hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_user }}"
password: "{{ vcenter_pass }}"
guest: "{{ inventory_hostname }}"
state: powered_on
vm_extra_config:
folder: "{{ folder }}"
notes: "{{ notes }}"
vm_disk:
disk1:
size_gb: "{{ disk }}"
type: thin
datastore: "{{ datastore }}"
vm_nic:
nic1:
type: vmxnet3
network: "{{ network }}"
network_type: standard
vm_hardware:
memory_mb: "{{ memory }}"
num_cpus: "{{ cpucount }}"
osid: "{{ osid }}"
scsi: lsi
vm_cdrom:
type: "iso"
iso_path: "{{ iso_image }}"
esxi:
datacenter: "{{ datacenter }}"
hostname: "{{ esxi_host }}"
|
インベントリファイルの用意
以下の内容でインベントリファイルを用意します。 今回は hosts
というファイル名にしました。
1
2
3
4
5
6
7
8
9
10
11
12 | [vms]
RancherOS-[1:9] disk='20' datastore='datastore1' network='VLAN1000' memory='2048' cpucount='1' osid='other3xLinux64Guest'
[vms:vars]
vcenter_hostname='10.0.0.1'
vcenter_user='administrator@test.local'
vcenter_pass='password'
datacenter='Datacenter'
esxi_host='10.0.0.99'
folder='RancherOS'
notes='Created by Ansible'
iso_image='datastore1/RancherOS_v1.1.0.iso'
|
実行する
用意が出来たら、まずは DryRun してみます。
| ansible-playbook --check -i hosts site.yml
|
エラー無く完了するようであれば、実行します。
| ansible-playbook -i hosts site.yml
|