Using cloud-config to run the post-installation configuration

Cloud-config is a feature of cloud-init and is the simplest way to install packages via apt and configure our instances. With cloud-config, we can use a Yet Another Markup Language (YAML) file to describe how an instance is configured, which would require more effort if performed with shell scripts.

Getting ready

Ensure 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 you have set the following credentials (adjust the path to your certificates and key file to match your environment if 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...

In this section, we will demonstrate the configuration of the instance's hostname and installation of the Apache web server, as well as creation of groups and users. The steps are as follows:

  1. We first create the .yaml file describing this behavior. Create a file called webserver.yaml in the current directory with the following content:
    #cloud-config
    hostname: myWebserver
    fqdn: mywebserver.cook.book
    manage_etc_hosts: true
    groups:
     - developers
    users:
     - auser
       gecos: A User
       primary-group: users
       groups: users, developers
       passwd: $6r$j632wezy/grasdfds7/efew7fwq/fdfws.8ewfwefwe
    packages
     - apache2
  2. We can 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
        --user-data ./webserver.yaml
        myWebserver
    

    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.

  3. The output can be seen with the nova console-log command. Cloud-init will interpret this cloud-config data and as a result will output commands such as apt-get update and apt-get install. At the end of the cloud-config run, your web server will be accessible.

    Tip

    Note that the output of nova list, which shows the status of the instance, will show as Active despite the fact that cloud-init might not have completed yet. Be aware of this if you are relying on this status to check whether a service running on the instance is ready or not.

    Remember to open up the relevant security group ports for the services running on the instances too.

How it works...

Cloud-config is a feature of cloud-init and is a very simple method for the post-configuration tasks of our instances. A number of configuration options are available.

In the preceding example, we set the following line:

#cloud-config

Any file starting with this on the first line will be interpreted by cloud-init as cloud-config data.

The hostname parameter sets the short hostname of the instance.

The fqdn parameter sets the fully qualified domain name of the instance.

The manage_etc_hosts: true line allows us to modify the entries of /etc/hosts with the preceding information.

Adding users to the instance is simple when used with the groups and users statements. We simply list the groups to be added to the system and the users. Each user section begins with:

 - name: username

This line is followed by the system information you'd expect to see. The next user will start with the next section as follows:

 - name: anotheruser

Package installation starts with the packages and we simply list each package we want to install, as shown in the following code. Ensure there is an Internet connection available to your instances if you are accessing them outside your network:

packages
- apache2
- openssl 

If the outside world isn't available, you can configure apt to point to a repository internally:

apt_mirror: http://internal-apt.cook.book/ubuntu/
apt_mirror_search_dns: false

This method is great for installation of packages for instances that cannot reach the Internet.

There's more...

Cloud-config can do a lot more than just install packages. Cloud-config can be used to install and run Chef recipes and puppet manifests, allowing you to integrate your OpenStack instances into your favorite orchestration and configuration management system. More information on cloud-config can be found at http://cloud-init.readthedocs.org/.

Cloud-init is not just limited to Linux-based instances. Cloudbase-init from Cloudbase brings the same ability to Windows instances also. Visit http://www.cloudbase.it/cloud-init-for-windows-instances/ for more information on how to set up a cloud-init instance in Windows with Powershell.

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

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