How to assign static IP with Python OpenStack SDK
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