1
0

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_vm_shellで実行したコマンドの実行完了を待つモジュールを作ってみた

Last updated at Posted at 2018-05-19

AnsibleのモジュールでvSphere API経由からゲストOSにコマンドを実行するモジュール vmware_vm_shell があります。
このモジュールを使ってコマンドを実行すると、コマンドはバックグラウンドで動作します。
モジュールは問題なくコマンドがバックグラウンドで動作したことが確認できれば処理が完了してしまいます。
そのためバックグラウンドで実行しているプロセスの実行を待つことはしません。
そこで vmware_vm_shell モジュールで実行したコマンドの処理完了を待つモジュールを作ってみました。

1. 課題のおさらい

vmware_vm_shell モジュールでコマンドを順次処理する場合、前のコマンドが完了してから次のコマンドを実行するということができません。
例えば、以下のPlaybookを実行してみます。

---
- name: VMware Module TEST
  hosts: localhost
  gather_facts: no
  tasks:
    - vmware_vm_shell:
        hostname: vcenter.local
        username: administrator@vsphere.local
        password: secret
        validate_certs: no
        vm_id: devel
        vm_username: root
        vm_password: secret
        vm_shell: /root/loop.sh
        vm_shell_args: 3

このPlaybookではdevelというVMで /root/loop.sh 3 を実行するものです。
loop.sh は単純にsleepするだけのものです。

# !/usr/bin/bash
for (( i = 0; i <= $1; i++ )) ; do
  sleep 1
done

実行してみます。

ansible_vm_shell_01.gif

コマンドが実行されバックグラウンドで動作しモジュール側の処理は完了していることが確認できます。

2. vmware_vm_shell_waitモジュール

そこで vmware_vm_shell で実行したコマンドの処理完了を待つモジュール vmware_vm_shell_wait を作ってみました。

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

2-1. 使い方

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

$ curl -L https://raw.githubusercontent.com/sky-joker/ore-ore-ansible/master/modules/cloud/vmware/vmware_vm_shell_wait.py -O

2-1-2. Playbook

vmware_vm_shell でコマンドを実行して vmware_vm_shell_wait で実行完了を待つPlaybook例です。

---
- name: VMware Module TEST
  hosts: localhost
  gather_facts: no
  tasks:
    - vmware_vm_shell:
        hostname: vcenter.local
        username: administrator@vsphere.local
        password: secret
        validate_certs: no
        vm_id: devel
        vm_username: root
        vm_password: secret
        vm_shell: /root/loop.sh
        vm_shell_args: 3

    - vmware_vm_shell_wait:
        hostname: vcenter.local
        username: administrator@vsphere.local
        password: secret
        validate_certs: no
        vm_id: devel
        vm_username: root
        vm_password: secret
        vm_shell: /root/loop.sh
        vm_shell_args: 3

2-1-3. 実行する

ansible_vm_shell_02.gif

バックグラウンドで実行されたコマンドの処理が完了したのを確認して vmware_vm_shell_wait の処理が完了していることが確認できました :-)

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?