Show Table of Contents
2.14. 例: Python を使用した仮想マシン NIC の作成
新規作成した仮想マシンが確実にネットワークにアクセスできるようにするには、仮想 NIC を作成してアタッチする必要があります。
例2.13 Python を使用した仮想マシン NIC の作成
以下の Python の例は、
上記のオプションは、仮想マシンの
nic1 という名前の NIC を作成し、vm1 という名前の仮想マシンにアタッチします。この例の NIC は以下の条件を満たしている必要があります。
virtioネットワークデバイスであること。nic_interface = "virtio"
ovirtmgmt管理ネットワークにリンクされていること。nic_network = api.networks.get(name="ovirtmgmt")
nics コレクションの add メソッドを使用して NIC を作成する前に、NIC パラメーターオブジェクトに組み込みます。
from ovirtsdk.api import API
from ovirtsdk.xml import params
try:
api = API (url="https://HOST",
username="USER@DOMAIN",
password="PASS",
ca_file="ca.crt")
vm = api.vms.get(name="vm1")
nic_name = "nic1"
nic_interface = "virtio"
nic_network = api.networks.get(name="ovirtmgmt")
nic_params = params.NIC(name=nic_name, interface=nic_interface, network=nic_network)
try:
nic = vm.nics.add(nic_params)
print "Network interface '%s' added to '%s'." % (nic.get_name(), vm.get_name())
except Exception as ex:
print "Adding network interface to '%s' failed: %s" % (vm.get_name(), ex)
api.disconnect()
except Exception as ex:
print "Unexpected error: %s" % exadd 要求が成功した場合には、スクリプトにより以下のような出力が表示されます。
Network interface 'nic1' added to 'vm1'.

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.