+++ date = "2015-09-30" draft = false weight = 14 title = "Lab 14 - Ansible" +++
The objective of this lab is to introduce you to the basics of ansible. You will learn the syntax and mechanisms for one-off commands, inventories to reach multiple hosts, and playbooks to accomplish complex and multi-part tasks.
An appropriate ssh environment amiable to ansible has been setup for you inside this lab environment. This is usually a non-trivial amount of work which involves setting up passwordless sudoers and distributing public keys.
The final portion of this lab will focus on using ansible to fix a specific OpenStack configuration issue.
-
SSH to your controller and log in as root
-
De-elevate your privileges to the normal user
centos[root@controller ~]#su centos -
Ensure ansible is installed
[centos@controller ~]$sudo yum install ansible[centos@controller ~]$ansible -h | lessIt's always useful to check out the help file. Take a second and look for some key words which should be familiar from the lecture.
-
Create a simple
hostsfile with the following content:192.168.0.10 -
Use the ping ansible module to test connectivity to the controller
[centos@controller ~]$ansible all -i hosts -m pingRefer back to the help command and read what each of these flags represent.
-
Gather Facts about controller with ansible
[centos@controller ~]$ansible all -i hosts -m setup | lessSkim through these facts and look for interesting values. How many processor cores does this node have? Which Linux kernel? Which release of CentOS is it running? All of these variables are available to your playbooks, these can be very useful for eliminating many repetitive information gathering tasks in your environments!
-
Add our other
openstacknodes to thehostsfile:[openstack] controller ansible_ssh_host=192.168.0.10 neutron ansible_ssh_host=192.168.0.11 compute1 ansible_ssh_host=192.168.0.12 compute2 ansible_ssh_host=192.168.0.13 -
Ping all your hosts
[centos@controller ~]$ansible all -i hosts -m ping -
Try different
host-patterns and modules[centos@controller ~]$ansible openstack -i hosts -m command -a 'hostname'[centos@controller ~]$ansible controller -i hosts -m command -a 'whoami'[centos@controller ~]$ansible openstack -i hosts -m command -a '/usr/sbin/ip addr' -
Red ink - this command will fail
[centos@controller ~]$ansible openstack -i hosts -m yum -a 'name=htop state=installed'Read the help in order to find the argument to run this command with
sudo, turn this red ink to green -
Gather some information about our compute nodes
[centos@controller ~]$ansible compute1 -i hosts -m setup | grep vcpus[centos@controller ~]$ansible compute2 -i hosts -m setup | grep vcpus
At this point you now know how to run ansible one-off commands. These can be very useful for making changes and gathering information from your systems quickly and efficiently from a central location. Next we will use an ansible playbook to accomplish an ordered list of tasks.
Ansible playbooks are an efficient way to organize a list of ordered tasks. These tasks are grouped into a play which specify which hosts will be configured. In this section we will make a simple playbook that installs a new program to our controller.
-
Download the
sl.ymlplaybook, inspect it, and run it[centos@controller ~]$curl https://alta3.com/labs/files/sl.yml[centos@controller ~]$curl https://alta3.com/labs/files/sl.yml > sl.yml[centos@controller ~]$less sl.yml[centos@controller ~]$ansible-playbook -i hosts sl.ymlNote that we did not need to enumerate the host-pattern in this command! My fingers feel less tired already!
-
Run it again and see how the
PLAY RECAPis different[centos@controller ~]$ansible-playbook -i hosts sl.yml -
Use the new application (don't forget to read the man page!)
[centos@controller ~]$sl[centos@controller ~]$man sl
First we will see what is broken. There is a configuration issue with Nova VNC Proxy service. This service allows a user to view and interact with an instance console from the web browser, a super useful feature! In order to configure this service correctly the controller and the compute nodes need to know the external ip address of the controller in order to correctly direct the browser to the vnc proxy resource. Because your lab environment was stood up just for you, your external ip address is not predictable. As a result this service is incorrectly configured and pointing to an incorrect public ip address.
In this section we will read and run a playbook that fixes this issue.
-
Clone the
ansible-novncrepository[centos@controller ~]$git clone https://github.com/bryfry/ansible-novnc.git[centos@controller ~]$cd ansible-novnc -
Read the
novnc.ymland confguration template file[centos@controller ansible-novnc]$less novnc.yml[centos@controller ansible-novnc]$less files/nova-controller.conf.j2[centos@controller ansible-novnc]$less files/nova-compute.conf.j2 -
We actually care about the
vncrelated parameters, let's filter on that[centos@controller ansible-novnc]$grep vnc files/nova-controller.conf.j2[centos@controller ansible-novnc]$grep vnc files/nova-compute.conf.j2 -
Run the
novnc.ymlplaybook[centos@controller ~]$ansible-playbook -i hosts novnc.yml -
Match up each
PLAYandTASKwith the corresponding portion of the playbook below -
Let's go use the vnc service! Open the OpenStack Horizon interface
-
Launch an instance or navigate to a launched instance
-
Click the console tab
-
Open the console in a separate window
-
Accept the privacy exception, we're using a self signed certificate
-
View and interact with the console
-
Refresh the page the console tab page, it should also work now
This is only a very small subset of the use cases for ansible. Like any tool you can customize and utilize it to fit your environments needs. Especially reccomended is to use ansible in combination with a revision control system (like git). This allows for a succinct and manageable orchestration of your environments throughout time. The sky is definitely the limit!






