イキり黒豹の爪とぎ

イキるために爪を研ぐ

govcsimにvmware_guestを叩いてみた

はじめに

vccimなるvCenterシミュレータがあると聞いたので、こいつに対してAnsibleのVMwareモジュールを叩いてみました。

vcsimのインストール

vcsimと、動作確認用にgovcをインストールします。

sudo yum install epel-release
sudo yum install golang
go get -u github.com/vmware/govmomi/govc
go get -u github.com/vmware/govmomi/vcsim
$ govc version
govc 0.23.0

vcsimの起動と動作確認

必要な環境変数を設定して、vcsimをバックグラウンドで起動します。

export PATH=${PATH}:${HOME}/go/bin
export GOVC_URL=https://user:pass@127.0.0.1:8989 GOVC_INSECURE=1
vcsim &

ちなみに、上記でGOVC_INSECURE=1を設定しないと、govcで下記のようなTLSエラーが出ます。

$ govc find -l
govc: Post https://127.0.0.1:8989/sdk: x509: certificate signed by unknown authority
2020/08/08 07:01:17 http: TLS handshake error from 127.0.0.1:38494: remote error: tls: bad certificate
$ govc find -l
Folder                       /
Datacenter                   /DC0
Folder                       /DC0/vm
VirtualMachine               /DC0/vm/DC0_H0_VM0
VirtualMachine               /DC0/vm/DC0_H0_VM1
VirtualMachine               /DC0/vm/DC0_C0_RP0_VM0
VirtualMachine               /DC0/vm/DC0_C0_RP0_VM1
Folder                       /DC0/host
ComputeResource              /DC0/host/DC0_H0
HostSystem                   /DC0/host/DC0_H0/DC0_H0
ResourcePool                 /DC0/host/DC0_H0/Resources
ClusterComputeResource       /DC0/host/DC0_C0
HostSystem                   /DC0/host/DC0_C0/DC0_C0_H0
HostSystem                   /DC0/host/DC0_C0/DC0_C0_H1
HostSystem                   /DC0/host/DC0_C0/DC0_C0_H2
ResourcePool                 /DC0/host/DC0_C0/Resources
Folder                       /DC0/datastore
Datastore                    /DC0/datastore/LocalDS_0
Folder                       /DC0/network
Network                      /DC0/network/VM Network
DistributedVirtualSwitch     /DC0/network/DVS0
DistributedVirtualPortgroup  /DC0/network/DVS0-DVUplinks-9
DistributedVirtualPortgroup  /DC0/network/DC0_DVPG0

無事govcで構成情報が取得できました。

Playbookの実行

手順は割愛しますが、ansible 2.10.0rc4で検証したので、community.vmwareコレクションを別途インストールしています。

inventory.ini

[vcsim]
127.0.0.1

[vcsim:vars]
vcenter_username = user
vcenter_password = pass
vcenter_port = 8989

vmware_guest.yml

- hosts: vcsim
  gather_facts: false
  connection: local
  collections:
    - community.vmware
  tasks:

  - name: Create a virtual machine
    vmware_guest:
      hostname: "{{ inventory_hostname }}"
      username: "{{ vcenter_username }}"
      password: "{{ vcenter_password }}"
      port: '{{ vcenter_port }}'
      validate_certs: no
      datacenter: DC0
      folder: /DC0/vm/
      name: created_by_ansible
      state: poweredon
      guest_id: centos64Guest
      esxi_hostname: DC0_H0
      disk:
      - size_gb: 10
        type: thin
        datastore: LocalDS_0
      hardware:
        memory_mb: 512
        num_cpus: 4
        scsi: paravirtual
      networks:
      - name: VM Network
        device_type: vmxnet3

  - name: Gather info from vcsim
    vmware_guest_info:
      hostname: "{{ inventory_hostname }}"
      username: "{{ vcenter_username }}"
      password: "{{ vcenter_password }}"
      port: '{{ vcenter_port }}'
      datacenter: DC0
      name: created_by_ansible
      validate_certs: no
    register: info

  - name: debug
    debug:
      var: info

実行結果

$ ansible-playbook -i inventory.ini vmware_guest.yml 

PLAY [vcsim] ************************************************************************************************************

TASK [Create a virtual machine] *****************************************************************************************
changed: [127.0.0.1]

TASK [Gather info from vcsim] *******************************************************************************************
ok: [127.0.0.1]

TASK [debug] ************************************************************************************************************
ok: [127.0.0.1] => 
  info:
    changed: false
    failed: false
    instance:
      annotation: null
      current_snapshot: null
      customvalues: {}
      guest_consolidation_needed: false
      guest_question: null
      guest_tools_status: guestToolsNotRunning
      guest_tools_version: '0'
      hw_cluster: null
      hw_cores_per_socket: 1
      hw_datastores:
      - LocalDS_0
      hw_esxi_host: DC0_H0
      hw_eth0:
        addresstype: generated
        ipaddresses: []
        label: Network Adapter 1
        macaddress: 00:0c:29:33:31:38
        macaddress_dash: 00-0c-29-33-31-38
        portgroup_key: null
        portgroup_portkey: null
        summary: VM Network
      hw_files:
      - '[LocalDS_0] created_by_ansible/created_by_ansible.vmx'
      - '[LocalDS_0] created_by_ansible/created_by_ansible.nvram'
      - '[LocalDS_0] created_by_ansible/vmware.log'
      - '[LocalDS_0] created_by_ansible/created_by_ansible.vmdk'
      hw_folder: /DC0/vm
      hw_guest_full_name: null
      hw_guest_ha_state: null
      hw_guest_id: centos64Guest
      hw_interfaces:
      - eth0
      hw_is_template: false
      hw_memtotal_mb: 512
      hw_name: created_by_ansible
      hw_power_status: poweredOn
      hw_processor_count: 4
      hw_product_uuid: ddb07137-d11d-5ab8-8b1a-7813e1933918
      hw_version: vmx-13
      instance_uuid: 546d0d69-d972-5fed-81f1-6e98c69313d0
      ipv4: null
      ipv6: null
      module_hw: true
      moid: vm-191
      snapshots: []
      vimref: vim.VirtualMachine:vm-191
      vnc: {}

PLAY RECAP **************************************************************************************************************
127.0.0.1                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
$ govc find -l
Folder                       /
Datacenter                   /DC0
Folder                       /DC0/vm
VirtualMachine               /DC0/vm/DC0_H0_VM0
VirtualMachine               /DC0/vm/DC0_H0_VM1
VirtualMachine               /DC0/vm/DC0_C0_RP0_VM0
VirtualMachine               /DC0/vm/DC0_C0_RP0_VM1
VirtualMachine               /DC0/vm/created_by_ansible★
Folder                       /DC0/host
ComputeResource              /DC0/host/DC0_H0
HostSystem                   /DC0/host/DC0_H0/DC0_H0
ResourcePool                 /DC0/host/DC0_H0/Resources
ClusterComputeResource       /DC0/host/DC0_C0
HostSystem                   /DC0/host/DC0_C0/DC0_C0_H0
HostSystem                   /DC0/host/DC0_C0/DC0_C0_H1
HostSystem                   /DC0/host/DC0_C0/DC0_C0_H2
ResourcePool                 /DC0/host/DC0_C0/Resources
Folder                       /DC0/datastore
Datastore                    /DC0/datastore/LocalDS_0
Folder                       /DC0/network
Network                      /DC0/network/VM Network
DistributedVirtualSwitch     /DC0/network/DVS0
DistributedVirtualPortgroup  /DC0/network/DVS0-DVUplinks-9
DistributedVirtualPortgroup  /DC0/network/DC0_DVPG0

補足

  • 上記のPlaybookを再実行すると、created_by_ansibleが実行した分だけ作成されます。
  • 作成済みの仮想マシンを特定するには、use_instance_uuid: yesuuid: ****で、UUIDを指定する必要があります。
  • 上記で仮想マシンを誤って量産した場合、govc vm.destroy created_by_ansiblecreated_by_ansibleという名前の仮想マシンがまとめて消えます。

おわりに

vcsim、非常に便利です。飽くまでシミュレータなので限界はありますが、vCenter、EXSiがなくても、モジュールの動作確認が可能なのは嬉しいですね。

参考

github.com

speakerdeck.com