Overview
Ansible is a configuration management tool with built in OpenStack support. Using Ansible, you can deploy and manage instances on DreamCompute. This tutorial explains how to install Ansible and use it to launch an instance on DreamCompute.
Setup
This tutorial uses Ubuntu 22.04 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 22.04, you can read docs.ansible.com/ansible/latest/installation_guide 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 an instance
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.
- 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
- 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"
- 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
- 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:
- Add DreamCompute authentication so an instance 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.
# 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}
- Finally, define what the instance 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-22.04 key_name: myKey flavor: 50 network: public wait: yes
Breakdown the last few lines
state | The state of the instance, possible values are:
|
name | The name of the instance to create |
image | The image to boot the instance from, possible values are:
|
key_name | The public key to add to the instance once it is created. This can be any key you have added to DreamCompute. |
flavor | This defines how much RAM and CPU your instance will have. You can view all flavors when creating an instance in the dashboard or when running an OpenStack command. Some possible values are:
|
network | The network to put your instance on. In this case it is the “public” network, but if you have private networking enabled, it could be different
|
wait | Whether or not to wait for the instance to create before continuing. Possible values are:
|
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”