PowerCLI で vCenter Server 管理下の ESXi の NTP 設定を一括変更

Share on:

Table of contents

はじめに

ESXi 7.0 Update 3 以降では /etc/ntp.conf の内容が configstore 内に移行されファイル自体が read-only となり、ntp.conf の直接編集がサポートされなくなりました。ntp.conf への追記が必要な場合、代わりに esxcli system ntp set の -f オプションで設定ファイルを投入する必要があります。

vSphere ESXi 7.0 U3 and later versions configuration files for NTP and PTP can no longer be edited (87176)

The ESXi configuration files /etc/ntp.conf and /etc/ptp.conf can no longer be edited directly.
It is no longer possible to make changes to NTP, PTP by editing these config files directly. From ESXi 7.0 Update 3, files under /etc are read-only and edits do not persist across reboot.

To change configuration:
・# esxcli system ntp set

今回の記事では PowerCLI から esxcli を呼び出すことで、複数の ESXi ホストに ntp.conf の設定を一括で投入してみます。

なお、vSphere Client などの GUI での NTP 設定は従来通り可能ですので、ntp.conf の編集が不要な場合1には esxcli を使用する必要はありません。

背景

ESXi の NTP サーバに Active Directory ドメインコントローラ (Windows Time サービス) を使用している環境では、Windows Time サービス側の設定の修正ではなく、ESXi 側の ntp.conf に tos maxdist を追記して同期させるワークアラウンドを実施されている方もいらっしゃると思います。

Microsoft ドメイン コントローラを使用して ESXi/ESX の時刻を同期する (1035833)
→ ESXi 6.x/ 7.0.1/ 7.0.2

  1. Open the /etc/ntp.conf file in a text editor. For more information, see Editing configuration files in VMware ESXi and ESX (1017022).

  2. Add the tos maxdist command on its own line:
    tos maxdist 30

ESXi 7.0 Update 3 以降では ntp.conf が直接編集できなくなっているため、このようなケースでは esxcli コマンドで ntp.conf の設定を投入する必要がでてきます。

vSphere ESXi 7.0 U3 and later versions NTP configuration steps loading a text file containing NTP configuration commands (87488)

Example:
[root@localhost:~] vi /scratch/ntpconfig.txt
[root@localhost:~] cat /scratch/ntpconfig.txt
server ntpserver.yourdomain.com
tos maxdist 30
[root@localhost:~] esxcli system ntp set -f /scratch/ntpconfig.txt
[root@localhost:~] esxcli system ntp set -e 1

esxcli の選択肢としては以下の3つがあげられますが、今回は色々と融通が利く PowerCLI を使います。

  • ESXi の組み込みの esxcli
  • Standalone ESXCLI
  • PowerCLI の EsxCli オブジェクト

PowerCLI の esxcli による ntp.conf の設定

上述の KB87488 に記載があるように、ntp.conf の設定は以下の流れで行うことになります。

  1. ntp.conf に追記する設定を記載したファイルをデータストア等に配置。
  2. 「esxcli system ntp set -f <1.で用意したファイル(絶対パス)>」で設定
  3. 「esxcli system ntp set -e 1」で NTP を有効化 (任意)

以下、これらを PowerCLI の esxcli オブジェクトを使用して実行します。

設定ファイルを作成し共有データストアに配置

複数の ESXi に同じファイルを投入するため、共有しているデータストア上に設定ファイルを配置します。配置する手段は vSphere Client のデータストアブラウザや scp など任意の方法でかまいません。

1[root@esxi-01:~] cat /vmfs/volumes/Shared-NFS/ntp.conf.txt
2server ntp.nict.jp
3tos maxdist 30

なお、不足している ntp.conf の設定は esxcli の実行時に自動的に補完されるため、上述のように必要な設定の記載のみで OK です。

PowerCLI から esxcli を実行

以下のように PowerCLI で vCenter Server に接続して各種コマンドレットを実行していきます。

1Connect-VIServer -Server vcsa.jangari.lab
2
3$esxcli_arr = Get-VMHost | Get-EsxCli -V2
4$arguments = $esxcli_arr[0].system.ntp.set.CreateArgs()
5$arguments.file = "/vmfs/volumes/Shared-NFS/ntp.conf.txt"
6$arguments.enabled = 1
7
8foreach ($esxcli in $esxcli_arr) { $esxcli.system.ntp.set.Invoke($arguments) }

ポイントとして、Get-EsxCli は VMHost オブジェクトの配列を受け取ることが可能で、この場合には対応する EsxCli オブジェクトの配列を返すようになっています。

Get-VMHost を Get-EsxCli にパイプで繋ぐことで vCenter Server 配下の全ホストの EsxCli オブジェクトの配列を取得し、最後の foreach でイテレータとして回すことで一括設定を行っています。

Get-VMHost は Location パラメータにデータセンターやクラスタ、フォルダといったコンテナオブジェクトを指定することで、指定したオブジェクト配下のホストのみを取得することができます。

ESXi の組み込みの esxcli や Standalone ESXCLI では明示的にホストの指定が必要であるため、ホストの台数が多い場合には PowerCLI を使用した方が操作対象の記述が少なくて済みます。

おわりに

今回の記事では PowerCLI の EsxCli オブジェクトを使用して複数の ESXi に ntp.conf の設定を投入してみました。

vSphere Client や ESXi Shell では一台ずつの設定が必要でも、PowerCLI とちょっとした工夫を加えれば一括で設定できるケースも多くあります。ホストの台数が多い場合には PowerCLI で容易に設定が行えないか検討してみるのも良いと思います。


  1. GUI 上の設定で事足りる、ホスト一台ずつの設定でも手が足りる等々 ↩︎