4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ansibleで実行できるVMwareのovftoolモジュールを作ってみた

Last updated at Posted at 2018-04-07

Ansibleモジュールでovfのダウンロードやデプロイをするモジュールが標準では無かったので作ってみました。(時間が経てば標準で出てくるとは思います。)

1. vmware_ovftool

https://github.com/sky-joker/ore-ore-ansible/blob/master/modules/cloud/vmware/vmware_ovftool.py

vmware_ovftool はESXi及びvCenterで動作確認をしています。

2. Playbook例

2-1. OVFをダウンロード

---
- name: OVF Download Task.
  vmware_ovftool:
    hostname: vCenter or ESXi
    username: Username
    password: Secret
    validate_certs: no
    method: download
    name: devel # myVMName
    path: ./devel # ovf download to local path(full or current directory path)

2-2. OVFをデプロイ

- name: OVF Deploy Task.
  vmware_ovftool:
    hostname: vCenter
    username: Username
    password: Secret
    validate_certs: no
    method: deploy
    datacenter: DC
    compute_resource: Cluster
    name: new_devel
    path: ./devel
    folder: /vm/example
    datastore: NFS
- name: OVF Deploy Task.
  vmware_ovftool:
    hostname: vCenter
    username: Username
    password: Secret
    validate_certs: no
    method: deploy
    datacenter: DC
    resource_pool: example
    name: new_devel
    path: ./devel
    datastore: NFS

3. 使用例

ESXiをホスト名で登録している場合は、vmware_ovftoolを実行するホストからESXiホストの名前解決ができなくてはいけません。

3-1. 必要なモジュールのインストール

[root@localhost ~]# pip install pyvmomi requests

3-2. モジュールのダウンロード

[root@localhost ~]# git clone https://github.com/sky-joker/ore-ore-ansible.git

3-3. OVFダウンロード

ESXiホストから devel という仮想マシンのOVFをダウンロードしてカレントパスの ./devel フォルダへ保存します。
Playbookは以下のものを使用します。

---
- name: OVF module test
  hosts: localhost
  gather_facts: no
  tasks:
    - name: OVF Download Task.
      vmware_ovftool:
        hostname: esxi-07.local
        username: root
        password: password
        validate_certs: no
        method: download
        name: devel
        path: ./devel

実行してみます。

[root@localhost ~]# mkdir devel
[root@localhost ~]# ansible-playbook ovf_download.yml --module-path ore-ore-ansible/modules/cloud/vmware/
 [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source

 [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 [OVF module test] ***************************************************************************************************************************************************************************

TASK [OVF Download Task.] ************************************************************************************************************************************************************************
ok: [localhost]

PLAY RECAP ***************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0
[root@localhost ~]# ls devel/
devel.ovf  disk-0.vmdk

3-4. OVFデプロイ

vCenterへダウンロードしたOVFをデプロイしてみます。
以下では、デプロイ先を Cluster というクラスタ名を指定しています。
/vm/example は仮想マシンをデプロイするvCenter側のフォルダパスです。
new_devel は新規で作成する仮想マシン名です。
Playbookは以下のものを使用します。

- name: OVF module test
  hosts: localhost
  gather_facts: no
  tasks:
    - name: OVF Deploy Task.
      vmware_ovftool:
        hostname: vcenter.local
        username: administrator@vsphere.local
        password: password
        validate_certs: no
        method: deploy
        datacenter: DC
        compute_resource: Cluster
        name: new_devel
        path: ./devel
        folder: /vm/example
        datastore: NFS

実行してみます。

[root@localhost ~]# ansible-playbook ovf_deploy.yml --module-path ore-ore-ansible/modules/cloud/vmware/
 [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source

 [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 [OVF module test] ***************************************************************************************************************************************************************************

TASK [OVF Deploy Task.] **************************************************************************************************************************************************************************
ok: [localhost]

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

4. 最後に

これで、ESXi単体でもマスターテンプレートの仮想マシンを作成してOVFでダウンロードしておけば、クローンちっくなことができるようになりました。
ただ、これだけだとPowershellとかでも出来てしまうのでAnsibleやAnsible Tower(AWXとかも)と組み合わせた旨味(デプロイ + VM構成変更 + サーバ構築 + テストの自動化など)が出ないか色々やってみようと思います。
作った後に思ったのが以下のツールのような argument_spec にすればよかったなと後悔...
https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/vmware/vmware_guest_file_operation.py

4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?