Ask Your Question
0

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))
    else:
        image = conn.compute.find_image('Ubuntu Xenial 16.04 (SWITCHengines)')
        flavor = conn.compute.find_flavor('c1.large')
        network = conn.network.find_network('my-lan')
        keypair = create_keypair(conn)

        print("  Name:     {}".format(name))
        print("  Image:    {}".format(image.name))
        print("  Flavor:   {}".format(flavor.name))
        print("  Network:  {}".format(network.name))
        print("  Keypair:  {}".format(keypair.name))

        server = conn.compute.create_server(
                name=name, image_id=image.id, flavor_id=flavor.id,
                networks=[{"uuid": network.id}], key_name=keypair.name),
                user_data=init_script, addresses = { 
                "private": [ {"addr": "10.0.1.1", "version": "4"} ],  
                "LAN-B": [ {"addr": "192.168.1.100", "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
1

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

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

I suppose the addressesattribute should help. See https://docs.openstack.org/openstacks....

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

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

EDIT

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(
        name=name, image_id=image.id, flavor_id=flavor.id,
        networks=[{"uuid": network.id, "fixed_ip": "10.0.0.111"} ]
        )

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

Comments

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 10.0.1.1 and 192.168.1.100? 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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Get to know Ask OpenStack

Resources for moderators

Question Tools

Stats

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

Seen: 275 times

Last updated: Jun 29 '18