How to assign static IP with Python OpenStack SDK

asked 2018-06-26 12:16:44 -0500

nakrule gravatar image

updated 2018-06-27 13:01:13 -0500

Hi, using the openstack Python SDK, I can create a new VM like this:

def create_server(conn, name):
    At minimum, a server requires a name, an image, a flavor, and a network on creation. You can discover the names and IDs of these attributes by listing them as above and then using the find methods to get the appropriate resources.

    Ideally you'll also create a server using a keypair so you can login to that server with the private key.

    Servers take time to boot so we call wait_for_server to wait for it to become active.
    print("Create server:")

    server = conn.compute.find_server(name)
    if server is not None:
        print("  Server {} already exist".format(name))
        image = conn.compute.find_image('Ubuntu Xenial 16.04 (SWITCHengines)')
        flavor = conn.compute.find_flavor('c1.large')
        network ='my-lan')
        keypair = create_keypair(conn)

        print("  Name:     {}".format(name))
        print("  Image:    {}".format(
        print("  Flavor:   {}".format(
        print("  Network:  {}".format(
        print("  Keypair:  {}".format(

        server = conn.compute.create_server(
                user_data=init_script, addresses = { 
                "private": [ {"addr": "", "version": "4"} ],  
                "LAN-B": [ {"addr": "", "version": "4"} ] 

        server = conn.compute.wait_for_server(server)

The network called "my-lan" has no has been created with Horizon. The gateway and DHCP are disabled. However, then I use my command to create an instance, an IP is automatically assigned to it. How can I choose a static IP ? Running the code above should work, but my instance still has random IP.

Thank you

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2018-06-26 18:17:20 -0500

updated 2018-06-29 02:43:37 -0500

I suppose the addressesattribute should help. See

While I have never tried it myself, I understand the documentation as follows:

addresses = { “private”: [ {“addr”: “”, “version”: “4”} ] }


Well, the documentation is, shall we say, misleading. When I tried to create a server with "addresses" as parameters, it resulted in an exception from Nova saying that there is no such field. It seems that the arguments are passed verbatim into the API request.

So, after reading the API documentation I wrote this:

server = conn.compute.create_server(
        networks=[{"uuid":, "fixed_ip": ""} ]

i.e. I included the address in the network dictionary. This works, at least with Newton (that's the version I have available right now).

edit flag offensive delete link more


The syntax you gave me is correct and running (see my updated code above). However, my VM still has another IP than the one I set in the code.

nakrule gravatar imagenakrule ( 2018-06-27 12:59:05 -0500 )edit

I can't try it out right now. Do you mean that the instance gets addresses in addition to and Or is the addresses parameter totally ignored?

Bernd Bausch gravatar imageBernd Bausch ( 2018-06-28 05:32:01 -0500 )edit

The parameters are ignored. Unfortunately, I don't have enough karma to add a picture to help you understand the situation.

nakrule gravatar imagenakrule ( 2018-06-28 06:13:47 -0500 )edit

Get to know Ask OpenStack

Resources for moderators

Question Tools


Asked: 2018-06-26 12:16:44 -0500

Seen: 358 times

Last updated: Jun 29 '18