LoginSignup
1
2

More than 5 years have passed since last update.

AnsibleのVMwareモジュールを並列実行する

Last updated at Posted at 2018-05-16

AnsibleのVMwareのモジュールはvSphere API(SOAP)を使って操作する作りになっています。
接続先はvCenter1台に対して複数のVMを操作する場合、普通にPlaybookを書くとシングル処理で動いてしまっていたので並列に動かすやり方例について記録しておきます。

1. VMwareモジュール

ここでは、例として vmware_guest_file_operation モジュールを使います。

http://docs.ansible.com/ansible/latest/modules/vmware_guest_file_operation_module.html#vmware-guest-file-operation-module

1-1. シングル処理について

ドキュメントに従ってPlaybookを作ってみます。

---
- name: VMware Module Test
  hosts: localhost
  gather_facts: no
  tasks:
    - name: copy file to guest os
      vmware_guest_file_operation:
        hostname: vCenterIP or FQDN
        username: administrator@vsphere.local
        password: secret
        validate_certs: no
        vm_id: "{{ item }}"
        vm_username: root
        vm_password: secret
        copy:
          src: ./testfile
          dest: /root/upload/testfile
          overwrite: False
      with_items:
        - devel
        - devel2

このPlaybookでは devel devel2 に対して testfile をVMのゲストOSへコピーするものになっています。
実際に実行すると以下のようになります。

ansible_vmware_01.gif

画面左上が devel で 右上が devel2 です。
with_items では一つのタスクが終わったら次のタスクというふうに順番に実行されることがわかります。
これだと、一つ一つ実行していくのでVM数に比例して時間がかかります。

1-2. 並列処理について

並列処理にする例です。
inventory ファイルを使って並列化してみます。
以下はinventoryファイル例です。

[vms]
devel ansible_connection=local
devel2 ansible_connection=local

Playbookでは vm_id にマジック変数の inventory_hostname を指定します。

---
- name: VMware Module Test
  hosts: vms
  gather_facts: no
  tasks:
    - name: copy file to guest os
      vmware_guest_file_operation:
        hostname: vCenterIP or FQDN
        username: administrator@vsphere.local
        password: secret
        validate_certs: no
        vm_id: "{{ inventory_hostname }}"
        vm_username: root
        vm_password: secret
        copy:
          src: ./testfile
          dest: /root/upload/testfile
          overwrite: False

実際に実行すると並列にアップロードされていることがわかります。

ansible_vmware_02.gif

2. 注意

vSphere API経由からファイルをゲストOSへコピーする時はVMware Toolsが動作していなければなりません。
また、一度vCenter/ESXiを経由してゲストOSへコピーするためオーバーヘッドが大きいのでサイズがでかいファイルや一度に大量コネクションを張ると負荷が懸念されます。
※でかいサイズのファイルのコピーは正直遅いです。

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