Using cloud-init to run post-installation commands

Cloud-init was originally developed by Canonical and is the de facto standard for running post-installation commands and configuration on a cloud instance. When an instance is launched, if cloud-init is part of the image being used, it will look for metadata information passed to it at launch time to do post-installation execution of commands. When a shell script is used (as demonstrated in the following How to do it… section of this recipe), it can be analogous to running commands in the /etc/rc.local working directory of a Linux machine. Cloud-init relies on data being sent from the nova-metadata API service. An instance looks for data associated with the particular instance and executes it accordingly. This section will cover the basics of using cloud-init.

Getting ready

Ensure that you are logged into a Ubuntu host that has access to our OpenStack environment on the 192.168.100.0/24 public network. This host will be used to run client tools against the OpenStack environment created. If you are using the accompanying Vagrant environment, as described in the Preface, you can use the controller node. This has the python-novaclient package that provides the nova command-line client.

If you created this node with Vagrant, you can execute the following command:

vagrant ssh controller 

Ensure that you have set the following credentials (adjust the path to your certificates and key file to match your environment if you're not using the Vagrant environment):

export OS_TENANT_NAME=cookbook
export OS_USERNAME=admin
export OS_PASSWORD=openstack
export OS_AUTH_URL=https://192.168.100.200:5000/v2.0/
export OS_NO_CACHE=1
export OS_KEY=/vagrant/cakey.pem
export OS_CACERT=/vagrant/ca.pem

How to do it...

For this section, we will demonstrate how to run a script that brings up all interfaces on a standard Ubuntu image. Without this approach, if you run a Ubuntu image with more than one network interface, only the first interface eth0 is brought up.

In this example, we will launch a Ubuntu instance with two Neutron networks and pass a shell script on the command line to bring up all interfaces. The steps are as follows:

  1. We first create a small shell script to demonstrate the ability to pass shell scripts to an instance. Create a file called multi-nic.sh in the current directory with the following content:
    #!/bin/bash
    ifconfig -a | awk '/^eth/ {print $1}' | while read I
    do 
        dhclient $I
    done
  2. We can then simply pass this file as an argument to the nova boot line with the --user-data flag as follows:
    nova boot
        --flavor m1.tiny
        --image trusty-image
        --nic net-id=e8e4ed14-97a6-4715-a065-3ff0347f40dd
        --nic net-id=8cb0f8f3-c529-45fe-ac5f-bd00c9814005
        --user-data ./multi-nic.sh
        myInstance
    

    The second nic, and eth1, lines will now have an IP associated with it.

    Tip

    The net-id value can be found by running the neutron net-list command and looking up the ID associated with the network you want to use. You can specify any number of --nic flags (limited by the amount the image supports).

How it works...

Cloud-init is a very powerful system that is the cornerstone of hands-free orchestration of your instances. By providing the ability to run post-installation scripts, cloud-init paves the way for full-stack system automation and integration with third-party configuration management utilities.

To be able to execute a cloud-init script that is similar to an rc.local script, start the file with #!, followed by the interpreter such as /bin/bash or /bin/python. This tells cloud-init to run this script very late into the boot sequence after the initial services have started.

Cloud-init also supports a wide range of other features ranging from running upstart jobs (if the file begins with #upstart-job), which will place the file in /etc/init and execute this as any other type of upstart job, to the ability to consume the gzip files. These files get unzipped and executed for that file type as normal. Using the gzip files is very common as the input size is limited to 16,384 bytes.

There's more…

More information on cloud-init can be found at http://cloud-init.readthedocs.org/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset