無償版ライセンスのESXiだとread系APIしか使用できないので無理だけど、60日間の評価版ライセンスだとwrite系APIもフルに使えるので、Ansibleのvmware_guestモジュールでVMを作成してみる。
playbook
だいたい こんな感じ。
NICやストレージの追加がある場合は良い感じにしてください。
varsに分けている環境情報や作成対象のVM情報はインベントリファイルなどに分ける方が良いでしょう。
- hosts: localhost vars: vcenter_hostname: 0.0.0.0 vcenter_username: root vcenter_password: ******** datastore: datastore1 vm_name: client01 os_guestid: centos7_64Guest memory: 2048 cpus: 2 disk_size: 20 iso_path: "[nfsserv] /disk2/archive/iso/CentOS-7-x86_64-Minimal-1908.iso" tasks: - name: create vmware_guest: hostname: "{{ vcenter_hostname }}" username: "{{ vcenter_username }}" password: "{{ vcenter_password }}" validate_certs: no datacenter: ha-datacenter folder: "" name: "{{ vm_name }}" hardware: nested_virt: yes memory_mb: "{{ memory }}" num_cpus: "{{ cpus }}" networks: - name: VM Network device_type: vmxnet3 disk: - size_gb: "{{ disk_size }}" type: thin datastore: "{{ datastore }}" cdrom: type: iso iso_path: "{{ iso_path }}" guest_id: "{{ os_guestid }}" state: poweredoff
varsで定義してる各変数については以下の通り。
| 変数 | 意味 |
|---|---|
| vcenter_hostname | ESXiのIPアドレス |
| vcenter_username | ESXiのログインユーザー名(root) |
| vcenter_password | ESXiのログインパスワード(text/plain) |
| datastore | 作成するVMを置くデータストア |
| vm_name | VM名 |
| os_guestid | VMのOS種 |
| memory | VMのメモリ(MB) |
| cpus | VMのCPU数 |
| disk_size | VMのディスクサイズ(GB) |
| iso_path | インストールメディアのISOファイルのパス |
ネットワークについてはデフォルトのVM Network(type:vmxnet3)をセットしてる。追加があればリストの項目を増やせばいいはず。
ISOファイルのパスは、vsphere_guestモジュールのときはcheddar-share/disk2/archive/iso/debian-10.1.0-amd64-netinst.isoと書いていたものは、同じ表記ではなく[cheddar-share] /disk2/archive/iso/CentOS-7-x86_64-Minimal-1908.isoのように記述する。([データストア名] パスっぽい)
これで実行すればVMが作成される。
(2.9) [zaki@cloud-dev ansible-vmware]$ ansible-playbook playbook.yml [WARNING]: No inventory was parsed, only implicit localhost is available [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [localhost] ********************************************************************************************************************* TASK [Gathering Facts] *************************************************************************************************************** ok: [localhost] TASK [create] ************************************************************************************************************************ changed: [localhost] PLAY RECAP *************************************************************************************************************************** localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

OS種別はこのへん
(いつもここ見てるけど公式情報ここでいいの?)
datacenterはESXiで使用する場合はha-datacenter固定値。少なくとも手動で作ってgovcとかで情報取得すると全てそうなっている。
同様にfolderも必須パラメタだけどESXiでは指定できないので、こちらは空文字""を指定する。
PyVmomi
vmware_guestはPythonライブラリPyVmomiを必要とするのでインストールしておく。
無いとエラーになるが「"PyVmomiが"無いよ!」とは言われないので気を付ける。
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ModuleNotFoundError: No module named 'requests' fatal: [localhost]: FAILED! => changed=false msg: Failed to import the required Python library (requests) on cloud-dev's Python /usr/bin/python3. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter
(2.9) [zaki@cloud-dev ansible-vmware]$ pip3 install PyVmomi
Collecting PyVmomi
Downloading pyvmomi-7.0.tar.gz (587 kB)
|████████████████████████████████| 587 kB 3.0 MB/s
Collecting requests>=2.3.0
Downloading requests-2.24.0-py2.py3-none-any.whl (61 kB)
|████████████████████████████████| 61 kB 1.3 MB/s
Requirement already satisfied: six>=1.7.3 in /home/zaki/ansible/2.9/lib/python3.6/site-packages (from PyVmomi) (1.15.0)
Collecting certifi>=2017.4.17
Downloading certifi-2020.6.20-py2.py3-none-any.whl (156 kB)
|████████████████████████████████| 156 kB 7.7 MB/s
Collecting idna<3,>=2.5
Downloading idna-2.10-py2.py3-none-any.whl (58 kB)
|████████████████████████████████| 58 kB 11.7 MB/s
Collecting chardet<4,>=3.0.2
Downloading chardet-3.0.4-py2.py3-none-any.whl (133 kB)
|████████████████████████████████| 133 kB 3.8 MB/s
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
Downloading urllib3-1.25.10-py2.py3-none-any.whl (127 kB)
|████████████████████████████████| 127 kB 13.0 MB/s
Building wheels for collected packages: PyVmomi
Building wheel for PyVmomi (setup.py) ... done
Created wheel for PyVmomi: filename=pyvmomi-7.0-py2.py3-none-any.whl size=250633 sha256=5b249f4cde71c3578c5306d0e75d97828d29b63c3fc4c09a90bcdbca33150e29
Stored in directory: /home/zaki/.cache/pip/wheels/3f/8f/9c/241d58c8db3b3a94e267bc08678dc26eda7f2659a504986db1
Successfully built PyVmomi
Installing collected packages: certifi, idna, chardet, urllib3, requests, PyVmomi
Successfully installed PyVmomi-7.0 certifi-2020.6.20 chardet-3.0.4 idna-2.10 requests-2.24.0 urllib3-1.25.10
WARNING: You are using pip version 20.1.1; however, version 20.2.2 is available.
You should consider upgrading via the '/home/zaki/ansible/2.9/bin/python -m pip install --upgrade pip' command.
(2.9) [zaki@cloud-dev ansible-vmware]$
環境
Ansible実行側は以下の通り。
(2.9) [zaki@cloud-dev ansible-vmware]$ python --version Python 3.6.8 (2.9) [zaki@cloud-dev ansible-vmware]$ cat /etc/redhat-release CentOS Linux release 7.8.2003 (Core) (2.9) [zaki@cloud-dev ansible-vmware]$ ansible --version ansible 2.9.10 config file = /home/zaki/src/ansible-vmware/ansible.cfg configured module search path = ['/home/zaki/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /home/zaki/ansible/2.9/lib/python3.6/site-packages/ansible executable location = /home/zaki/ansible/2.9/bin/ansible python version = 3.6.8 (default, Apr 2 2020, 13:34:55) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] (2.9) [zaki@cloud-dev ansible-vmware]$ ansible-config dump --only-changed DEFAULT_STDOUT_CALLBACK(/home/zaki/src/ansible-vmware/ansible.cfg) = yaml
ESXiバージョンは6.7.0 Update 3 (Build 14320388)
関連
ところで、60日の評価版ライセンスの「60日」って、もしかして「インストールした日から60日間」じゃなくて「総稼働時間が60日」なのかな。
今回の環境って4月にgovc試したときに作ったESXiを久しぶりに点火したんだけど…
