pythonopenstackopenstack-novanovaclient

Openstack NovaClient : Error while launching an instance using servers.create()


I tried to spawn a cirros instance using nova python client with below api call,

server = nova.servers.create(name = "cirros_vm",
                             image = image.id,
                             flavor = flavor.id )

I am getting the following error:

novaclient.exceptions.BadRequest: Multiple possible networks
found, use a Network ID to be more specific. (HTTP 400)
(Request-ID: req-c3aba1d2-23e9-4751-badc-60142286232e)

This is because I an having multiple networks inside my tenant.The help documentation shows that the optional nics argument should be used.

 :param nics:  (optional extension) an ordered list of nics to be
                  added to this server, with information about
                  connected networks, fixed ips, port etc.

But I am not able to figure out how to pass network ids to this api. When I give network ids as an ordered list,

server = nova.servers.create(name = "api_cir_test",
                             image = image.id,
                             flavor = flavor.id,
                             nics=[ network1.id, network2.id])

I am getting the following error:

AttributeError: 'unicode' object has no attribute 'get'

Below are the details of variables used:

nova is an object of Client in module novaclient.v1_1.client 
image = nova.images.find(name="cirros")
flavor =nova.flavors.find(name="m1.small")
network =nova.networks.find(label="test_net")

Solution

  • The nics argument requires the same information you would supply on the nova boot command line...which means it accepts things other than network ids (you can pass in explicit neutron port IDs, for example, or you can provide information about fixed ip addresses). The nics argument needs an ordered list of dictionaries, such as:

    server = nova.servers.create(name = "api_cir_test",
                                 image = image.id,
                                 flavor = flavor.id,
                                 nics=[{'net-id': network1.id},
                                       {'net-id': network2.id}])
    

    That should successfully create your server.