LoginSignup
1
4

More than 3 years have passed since last update.

Ansible:vsphere_guestモジュールでテンプレートを利用したVM作成

Last updated at Posted at 2019-07-11

Ansibleでvsphere_guestモジュールを使ったVM作成方法

試験実施時の環境リセットを毎回VM作り直すことで実現したい!

やりたいこと

  • CentOS7の自動インストール
  • Windows Server2016の自動インストール
  • 上2つのアンインストール

環境

  • Esxi6.5
  • vCenter
  • Ansible2.9

vsphere_guestについて

  • 非推奨となっているが実証実験中で諸々のライセンスが評価版の為、vsphere_guestを使用
  • 正式採用ではvmware_guestを使用する予定

ISOとテンプレートどちらからインストールするか

  • CentOSのISOイメージをkicakstartを使って自動インストールする方法もあるが、WindowsServerでそれができるか分からなかったのでテンプレートから復元する方法を採用した。
  • ISOからのインストールよりテンプレートから復元したほうが所要時間かなり短くなることも決め手となった。

Esxi上でテンプレートの作成

  • テンプレート化したいVMは予めインストール済みの前提です。
  • vCenterにログインして、ホスト及びクラスタからテンプレートにしたいVMを右クリックする、テンプレート->テンプレートに変換を選択することでテンプレートに出来ます。この際テンプレートにしたVMは消えるので気をつけます。

OSインストールのplaybook

install.yml
- hosts: all
  gather_facts: false
  connection: local
  user: root
  sudo: true

  tasks:
    - vsphere_guest:
        vcenter_hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_user }}"
        password: "{{ vcenter_pass }}"
        guest: "{{ guest_name }}"
        validate_certs: False
        power_on_after_clone: yes
        from_template: yes
        template_src: "{{ template_src }}"
        vm_extra_config:
          notes: "{{ notes }}"
        esxi:
          datacenter: "{{ datacenter }}"
          hostname: "{{ esxi_host }}"

OSアンインストールのplaybook

uninstall.yml
- hosts: all
  gather_facts: false
  connection: local
  user: root
  sudo: true

  tasks:
    - vsphere_guest:
        vcenter_hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_user }}"
        password: "{{ vcenter_pass }}"
        guest: "{{ guest_name }}"
        validate_certs: False
        force: yes
        state: absent

Ansibleの変数ファイル

vars
[vms]
TEST_Server   #ここは消しても動く?未検証
[vms:vars]
vcenter_hostname='vCenterのホスト又はIP'
vcenter_user='administrator@vsphere.local'
vcenter_pass='vCenterのパスワード'
datacenter='データセンター名'
esxi_host='Esxiのホスト又はIP'
notes='注意書き、いろんなサンプルで「Created by Ansible」が書かれている'
template_src='保存したテンプレートの名前'
guest_name='VMの名前'

インストール実行

ansible-playbook -i vars install.yml

アンインストール実行

ansible-playbook -i vars uninstall.yml
1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4