zaki work log

作業ログやら生活ログやらなんやら

[Ansible] deprecatedになってるvsphere_guestを使ってESXi上でVMを自動生成する

10/15 21時追記: タイトルと、"はじめに"部分を大きく修正

Ansibleを使ったESXiでのVM作成などを行うモジュールがいくつかあり、deprecatedなのを承知でvsphere_guestを使ってみたときの作業手順。

ドキュメントの"DEPRECATED"の項目を見れば明記されてるけど、vsphere_guestはdeprecatedになっており、Ansible 2.9で削除予定のモジュール。現在はvmware_guestへのリプレースが進められている。

vsphere_guestモジュールは内部ではpysphereを使用している。
vmware_guestではPyVmomiが使われている。
以下のページに違いを割と簡素にまとめられてる。

qiita.com

以下のissueも参照(sky_jokerさん情報共有ありがとうございます)

github.com

一言で簡単にまとめると、vmware_guestはVMware製のモジュールでオープンなAPIを使って実装されているのに対して、vsphere_guestは個人作成(メンテも停止)のモジュールでVMware提供のAPIを使用せずにヤンチャな作りになっている…と解釈。

以上を踏まえてvsphere_guestを使う場合は承知の上で2.8以下の環境で使ってください。

playbook

だいたいこんな感じ(Debian 10のVMの場合)

---
- name: VMware
  hosts: localhost
  tasks:
  - name: create vm
    vsphere_guest:
      vcenter_hostname: '{{ esxi_ipaddres }}'
      username: '{{ esxi_username }}'
      password: '{{ esxi_password }}'
      guest: '{{ vmname }}'
      state: powered_off
      validate_certs: no
      vm_extra_config:
        vcpu.hotadd: yes
        mem.hotadd: yes
      vm_disk:
        disk1:
          size_gb: 60
          type: thin
          datastore: '{{ datastore_name }}'
      vm_nic:
        nic1:
          type: vmxnet3
          network: VM Network
          network_type: standard
      vm_hardware:
        memory_mb: 4096
        num_cpus: 2
        osid: debian10_64Guest
        scsi: paravirtual
        vm_cdrom:
          type: iso
          iso_path: '{{ iso_file_path }}'
      esxi:
        datacenter: ha-datacenter
        hostname: '{{ esxi_hostname }}'

変数はそれぞれ以下の通り

変数 指定するもの
{{ esxi_ipaddres }} ESXiのIPアドレス
{{ esxi_username }} ESXiログイン用ユーザ名(rootとか)
{{ esxi_password }} ESXiログイン用パスワード
{{ vmname }} 作成するVMのマシン名
{{ datastore_name }} VMの実データを配置するデータストア名
{{ iso_file_path }} 起動時にマウントするISOファイルのパス / 下記参照
{{ esxi_hostname }} ESXiのホスト名

vm_extra_configは指定が無いとVMが作成されなかった。
ただしvm_extra_config.folderを指定するとやはりVMが作成されない。

{{ iso_file_path }}については、データストアで下記の画像みたいな場合は、"cheddar-share/disk2/archive/iso/debian-10.1.0-amd64-netinst.iso"と書いておけばOK

f:id:zaki-hmkc:20191015065639p:plain

osidに指定するOS種類はこのサイト辺りを参照。
(オフィシャルな最新版がどこにあるか見当たらなかった)

よく使うのはこの辺かな

  • centos7_64Guest
  • rhel7_64Guest
  • ubuntu64Guest

Ubuntuはバージョン指定がないので楽。
CentOSは"7"を付けずにcentos64Guestとすると"CentOS 4/5 (64-bit)"になるので注意。

実行

ここは普通にansible-playbookを実行すればよい。localhostへのtask実行なので、インベントリファイルは不要。

[zaki@manager esxi]$ ansible-playbook playbook.yml
 [WARNING]: provided hosts list is empty, only localhost is available. Note
that the implicit localhost does not match 'all'

[DEPRECATION WARNING]: vsphere_guest is kept for backwards compatibility but 
usage is discouraged. The module documentation details page may explain more 
about this rationale.. This feature will be removed in a future release. 
Deprecation warnings can be disabled by setting deprecation_warnings=False in 
ansible.cfg.

PLAY [VMware] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [create vm] ***************************************************************
[DEPRECATION WARNING]: The 'vsphere_guest' module has been deprecated. Use 
'vmware_guest' instead.. This feature will be removed in version 2.9. 
Deprecation warnings can be disabled by setting deprecation_warnings=False in 
ansible.cfg.
changed: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0   

[zaki@manager esxi]$ 

vsphere_guestモジュールがdeprecatedなので警告が色々でるけど、、、こんな感じ。

f:id:zaki-hmkc:20191015064823p:plain

OSはがんばって入れよう。
そういえばDebian系ってkickstartみたいなのあったっけ?

pysphere module required

fatal: [localhost]: FAILED! => changed=false 
  msg: pysphere module required

Pythonpysphereモジュールが入ってないと上記エラーで失敗する。
sudo pip install pysphereでインストールすればOK。

CentOS7標準だとpipが入ってないので、

$ sudo yum install epel-release
$ sudo yum install python2-pip

すればOK
(これだと2020年1月サポート終了のPython2系のpipがインストールされる)