For those of you who may not be too familiar with Ansible: It’s a great tool to get started in the world of automation and makes your life in configuration management much easier.
The familiarization phase
In this tutorial, we will go through a basic playbook that you can use with OpenStack and look at using dynamic inventories. Dynamic inventories have one big advantage: you don’t have to manually update the file every time you set up a new server in a project. If Ansible is used with OpenStack, a dynamic inventory plugin can be used to create a list of servers running in the OpenStack environment.
The plugin connects to the OpenStack API and collects information about instances that meet certain criteria, such as those with a specific tag or running a specific image. By using a dynamic inventory, you can optimize the process of discovering and managing hosts in the network environment without having to manually update a static inventory file .
This is particularly useful in larger or more dynamic environments where the number of hosts is constantly changing. The only prerequisites for this tutorial are basic knowledge of Ansible and OpenStack. And even as an absolute beginner, you should still be able to get the gist of what we’re trying to accomplish. For demonstration purposes, we will use the servers of my project, but in reality it could be any number of servers. In this example, we are simply checking the uptime of all our servers.
So let’s start right away! The two most important prerequisites are (of course) Ansible and the OpenStack SDK to control the API. However, caution is advised: Depending on your system, you may need to use “pip3”, as “pip” refers to “pip2”, which is no longer supported. First, let’s install Ansible with pip from Python. Let’s check if pip is installed:
python3 -m pip -VIf you receive the message “No module named pip”, you can install it with these commands:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py --userAnd then you can also install Ansible:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py --userWith this command we can check whether it has been installed correctly:
ansible --versionThe first shared apartment
Now that we have Ansible installed, we can proceed with the OpenStack SDK. After we have made sure that Pip is definitely installed, we can use the following:
pip install openstackNow that we have installed both programs and the plugin, we can start writing a simple playbook. As already mentioned, this playbook is kept very simple and we will only check the uptime of the servers. Of course, this can also be much more complex, but the principle is always the same. We need the following files in our folder where we run the Ansible code: clouds.yaml This file contains all our credentials for OpenStack. Ansible needs these to gain access to our project. You can get the credentials by downloading the file from OpenStack. This is what mine looks like:
clouds:
openstack:
auth:
auth_url: https://cloud.netways.de:5000/v3/
username: "openstack-user"
password: "super-secret-password"
project_id: 12495myproject-id65684
project_name: "1234-openstack-15def5"
user_domain_name: "Default"
region_name: "HetznerNBG4"
interface: "public"
identity_api_version: 3ansible.cfg: Here I have added a server that acts as a jump host and a user that is required to connect to our servers.
[defaults]
ansible_ssh_common_args='-o ProxyCommand="ssh -W %h:%p user1@jumpserver"'
remote_user=ubuntuopenstack.yml: In this file, we specify which plugin we want to use and which variables we want to include. “private: false”, for example, ensures that we use public IP addresses. This will also link our cloud.yaml file so we don’t need to include it in our command. This is the file I used:
---
plugin: 'openstack.cloud.openstack'
expand_hostvars: yes
fail_on_errors: yes
all_projects: false
private: false
clouds_yaml_path: ["./clouds.yml"]playbook.yaml: This is the simple playbook I used to determine the uptime of all my servers:
---
- name: Print uptime of servers in OpenStack project
hosts: all
gather_facts: no
tasks:
- name: Print server uptime
command: uptime
register: uptime
- debug: msg="{{ uptime.stdout }}"Now that all the preparations have been made, it’s time to run our playbook and check the output:
ansible-playbook -i openstack.yml playbook.ymlHoneymoon
After the playbook has been fully executed, we can check that it has worked properly. We should see the results as printed above on the terminal. If the playbook is much more complex and fails, you can simply look at the error messages to see where the problem occurred. That’s the great thing about Ansible: you can run the playbook as many times as you like and get the same result. This is also known as idempotency .
Although this is a very basic tutorial on the possibilities of a dynamic inventory, it can just as easily be applied to much more complex playbooks. If the underlying project has 100 servers, this would be ideal as it eliminates the need to manually configure each server. However, this can also be helpful for smaller projects.
For example, you don’t have to re-enter the details of new servers if old servers need to be destroyed and new ones set up in the same way. If you want to learn more about IaaS or read more tutorials on various other Dev-Ops topics, check out our tutorials and the other articles on our website. Getting started with your own setup couldn’t be easier. One of our MyEngineers® will be happy to help you bring your project ideas to life.





0 Comments