简体   繁体   中英

Specify post-installation script when creating an instance in OpenStack [python-novaclient]

I have a working python program which is able to create instances on OpenStack thanks to the python-novaclient library.

Now I'd like to give a post-installation script at the creation time. I looked at the documentation of the Servers .create() method but it doesn't seem to be implemented.

Did anyone faced this problem?


EDIT

In Horizon, when we create an instance, there is this information next to the textarea for the post-installation script:

The "Customisation Script" field is analogous to "User Data" in other systems.

Does it mean userdata is the parameter I need?

userdata – user data to pass to be exposed by the metadata server this can be a file type object as well or a string.

Indeed the solution is on userdata

Here's the Python code I wrote to solve my problem:

## Return the new created instance
# @param name Name of the instance to create in a String format
# @param image OpenStack image to deploy on the virtual machine
# @param flavor OpenStack flavor to use for the virtual machine
# @param keypair Name of the keypair to copy on the instance
# @param sec_groups List of security groups to link to the instance
def create_instance(self,name,image,flavor,keypair=None,sec_groups=None):
  instance = self.client.servers.create(
    name=name,
    image=image,
    flavor=flavor,
    key_name=keypair,
    security_groups=sec_groups,
    userdata="#!/bin/bash \n echo 'AMAZING TEST' > /root/test"
  )
  return instance

Try enabling config drive. The user data can be sent to VM via config drive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM