How to launch a DreamCompute server with Ansible

Ansible is a configuration management tool with built in OpenStack support. Using Ansible, you can deploy and manage servers on DreamCompute. This tutorial explains how to install Ansible and use it to launch a server on DreamCompute.

Setup

This tutorial uses Ubuntu 16.04 (Xenial Xerus) as the host to run Ansible playbooks. First, install a few packages using the following command.

[user@localhost]$ sudo apt-get install -y ansible python2.7 python-virtualenv python-pip

If you are not using Ubuntu 16.04, you can read http://docs.ansible.com/ansible/intro_installation.html#latest-releases-via-pip to figure out how to install ansible on your system

Ansible depends on 'shade' which is a Python client library for interacting with OpenStack. Create a virtual environment and use pip to install 'shade'.

[user@localhost]$ virtualenv venv -p /usr/bin/python
[user@localhost]$ source venv/bin/activate
(venv)user@localhost]$ pip install shade

Now you have 'shade' installed and are ready to start writing Ansible playbooks.

What are Playbooks?

Playbooks are Ansible’s configuration, deployment, and orchestration language. At a basic level, playbooks can be used to manage configurations and deployments to remote machines. Playbooks are designed to be human-readable and are developed in a basic text language.

Writing a Ansible Playbook to launch a server

Create a file named launch-server.yaml and enter the following lines into it.

The following examples add on to the example before. You only need to update the code in green text each time.

Make sure to indent as described below. Incorrect spacing will cause the file to fail to run.

  1. The first part of your playbook is a list of hosts that your playbook will run on. In this case there is one, localhost.
    # hosts the playbook runs on
    - hosts: localhost
  2. Since 'shade' has been installed in the virtualenv, you must tell ansible to use this virtualenv. Move to the next line, indent 2 spaces, then add these lines.
    # hosts the playbook runs on
    - hosts: localhost
      vars:
        ansible_python_interpreter: "/usr/bin/env python"
  3. Define a list of tasks to perform in this playbook. Move to the next line, indent 2 spaces, then add these lines.
    # hosts the playbook runs on
    - hosts: localhost
      vars:
        ansible_python_interpreter: "/usr/bin/env python"
    
      # List of tasks
      tasks:
      - name: launch an Ubuntu server
  4. Use the 'os_server' module to create an OpenStack instance. Move to the next line, indent 2 spaces, then add this line.
    # hosts the playbook runs on
    - hosts: localhost
      vars:
        ansible_python_interpreter: "/usr/bin/env python"
    
      # List of tasks
      tasks:
      - name: launch an Ubuntu server
    
        # Define a server
        os_server:
  5. Add DreamCompute authentication so a server can be created. In the following example, substitute the following:
    • {username} — your DreamCompute dashboard username.
    • {password} — your DreamCompute dashboard password.
    • {project} — your DreamCompute project. This can be found in the dashboard on the top left.
    Move to the next line, indent 2 spaces, then add these lines.
    # hosts the playbook runs on
    - hosts: localhost
      vars:
        ansible_python_interpreter: "/usr/bin/env python"
    
      # List of tasks
      tasks:
      - name: launch an Ubuntu server
    
        # Define a server
        os_server:
    
        # Cloud authentication information
        auth: 
          auth_url: https://iad2.dream.io:5000 
          username: {username} 
          password: {password} 
          project_name: {project}
  6. Finally, define what the server should look like.
    # hosts the playbook runs on
    - hosts: localhost
      vars:
        ansible_python_interpreter: "/usr/bin/env python"
    
      # List of tasks
      tasks:
      - name: launch an Ubuntu server
    
        # Define a server
        os_server:
    
          # Cloud authentication information
          auth:
            auth_url: https://iad2.dream.io:5000
            username: {username} 
            password: {password} 
            project_name: {project}
    
          # VM details
          state: present
          name: ansible-vm1
          image: Ubuntu-16.04
          key_name: myKey
          flavor: 50
          network: public
          wait: yes
    

Breakdown the last few lines

state The state of the server, possible values are:
  • present
  • absent
name The name of the server to create
image The image to boot the server from, possible values are:
key_name The public key to add to the server once it is created. This can be any key you have added to DreamCompute.
flavor This defines how much RAM and CPU your server will have. You can view all flavors when creating a server in the dashboard or when running an OpenStack command. Some possible values are:
  • Name of a flavor: gp1.semisonic, gp1.supersonic, etc
  • ID of a flavor: 50, 100, 200, etc
network The network to put your server on. In this case it is the “public” network, but if you have private networking enabled, it could be different
  • Name of a network: public
  • ID of a network: e098d02f-bb35-4085-ae12-664aad3d9c52
wait Whether or not to wait for the server to create before continuing. Possible values are:
  • yes
  • no

Running the Ansible Playbook

The Ansible playbook can be run with the following command:

[user@localhost]$ ansible-playbook launch-server.yaml

You should see output like

PLAY [localhost]
***************************************************************************

TASK [Gathering Facts]
***************************************************************************
ok: [localhost]

TASK [launch an Ubuntu server]
***************************************************************************
changed: [localhost]

PLAY RECAP
***************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0

Now if you check the DreamCompute dashboard you will see the new instance named “ansible-vm1”

Did this article answer your questions?

Article last updated .