086-091 LV Ansible

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

LINUX VOICE TUTORIALS – ANSIBLE

Ansible Container
Auto Deploy
Combine Ansible and Docker to streamline software deployment

C
BEN EVERARD omputing is bigger than it’s ever been. The install is a little involved and is different on
There are now more devices producing different distros. The basic process will be the
Why do this? and processing more data than last year, same on every distro, but the commands will be
• Quickly create and it’s only going to get bigger in coming years. different. The following works on Ubuntu 16.04. If
environments that For every device in the hands of a user, back-end you’re unsure what to do, the process is well doc-
are reproducible on services are needed to keep everything running umented for different distros [2] .
many machines and smoothly. The tools that have served administra- Docker maintains their own repositories with the
infrastructures. tors well for years now struggle to cope with latest software versions. With everything changing
• Automate your server farms packed to the rafters with comput- rapidly, it’s best to stay up to date or things may not
deployment for ease ers whirring away and needing maintenance. work, so let’s add these repositories to our system.
of reuse. Apt and Yum are great at installing software on You’ll need a couple of things to be able to add the
• Add the word DevOps one machine, but what if you need to update a Docker repository to APT. You can get these with:
to your CV and earn package on a hundred machines? What if a con-
more pounds, euros, figuration file needs to be updated but in a slightly sudo apt‑get install U
or dollars. different way on hundreds of machines? apt‑transport‑https ca‑certificates

Now imagine each of those machines is run-


ning a dozen containerized applications that can Cryptographic keys enable APT to verify that the
be launched or stopped depending on how much downloads are really coming from Docker. You
load is currently on the system. How do you man- can install them with:
age now? We’ll look at one option for keeping ev-
erything humming along smoothly – Ansible Con- sudo apt‑key adv ‑‑keyserverU

tainer [1]. hkp://p80.pool.sks‑keyservers.U

Ansible Container uses Docker to create contain- net:80 ‑‑recv‑keysU

ers that host your code and uses Ansible playbooks 58118E89F3A912897C070ADU

to set everything up inside these environments. BF76221572C52609D

The first thing you need is all the software you’ll


be using to manage the environments you’ll cre- Now you’re ready to add the Docker repository to
ate. Ansible Container is written in Python and is APT. Create the file /etc/apt/sources.list.d/
available through PIP, but at the time of writing, docker.list with your favorite text editor (you’ll
this was throwing errors on some systems, so I need sudo permissions) and add the following line:
opted to download straight from GitHub.
deb https://apt.dockerproject.U

git clone https://github.com/U org/repo ubuntu‑xenial main

ansible/ansible‑container.git

cd ansible‑container With everything set up, you can now grab the lat-
sudo python ./setup.py install est version of Docker from the repositories:

The second bit of software you’ll need is Docker, sudo apt‑get update

which will run behind the scenes and manage the sudo apt‑get install docker‑engine

containers themselves. You can run Ansible Con-


tainer with either Docker Engine (normal Docker) or The Docker service runs in the background con-
Docker Machine (which makes it easy to run con- trolling the containers that are running. The com-
tainers in virtual machines). I opted to go with mand-line tool sends instructions to this service
Docker Machine. Although it may seem a little over- about what you want to do, so you need to ensure
complicated to run containers on separate virtual that the background service is running before you
machines, it eases the set up for running locally. can do anything with the client:

86 DECEMBER 2016 ISSUE 193 LINUX-MAGAZINE.COM | LINUXPROMAGAZINE.COM


TUTORIALS – ANSIBLE LINUX VOICE

sudo service docker start

You can make sure that everything is running


properly with:

sudo docker run hello‑world

If everything has gone well, you should see a mes-


sage with the following text:

Hello from Docker!

This message shows that your installation

appears to be working correctly.

That’s Docker Engine set up. Now let’s move onto


Docker Machine, which is available as a binary
file. You can download it ready to run with:

sudo curl ‑L https://github.com/U

docker/machine/releases/download/U Figure 1: A good text editor


v0.7.0/docker‑machine‑`uname U This file defines the containers you want to run. In will have syntax highlighting
‑s`‑`uname ‑m` > /usr/local/bin/U this case, it’s a single one called web that’s based on for the YML language used
docker‑machine the Ubuntu Trusty image, has port 80 bound to loc- in Ansible playbooks.
alhost port 80, and runs Apache though dumb-init.
You’ll need to enable execute permissions before Now open main.yml and uncomment lines until
you can run this file. Add this with: it reads as shown in Listing 2.
This is an Ansible playbook that tells Ansible
sudo chmod +x /usr/local/bin/docker‑machine what to do to configure the container correctly.
Let’s look at the format of this file. It’s text based
There is one thing left to get, but don’t despair, using the YML markup language. In essence, it’s
this one’s easy to install. You should find Virtual- one big list with various items at various depths.
Box in your distro’s repositories. On Ubuntu, you A hyphen adds a new item to the list, and adding a
can install it with: two-spaces indent adds another level to the list. In
this case, the main list has two items (hosts: all
sudo apt install virtualbox and hosts: web). These items are plays, and the
hosts line tells Ansible which hosts to apply this
After all that installing, you’re now ready to get play to. In this project, there is only one host
into Ansible Container. (called web), but there
First, create a directory for your new container can be many. The
(we’ll call ours ansible‑test), then cd into it: hosts: all line tells An-
Listing 2: main.yml
sible to apply this play 01 ‑
 hosts: all
mkdir ansible‑test to every host that it 02  gather_facts: false
cd ansible‑test knows about. 03  tasks:

04 ‑
 raw: which python || apt‑get update
You’ll need a few files for your
project, and using the ansible‑
Listing 1: container.yml 05 ‑
 
raw: (which python && which aptitude) ||
apt‑get install ‑y python python‑app

container init command will 01 


version: "1"
aptitude

set everything up for you. 02 services:


06 ‑
 hosts: web

This will create a subdirec- 03 web:


07  tasks:

tory called ansible that con- 04  image: ubuntu:trusty


08  ‑ name: Upgrade APT

tains the critical files for your 05  ports:


09  apt: upgrade=yes

project, and they contain some 06  ‑ "80:80"


10  ‑ name: Install ca‑certificates

example code that’s com- 07  


command: ['/usr/bin/dumb‑init', 11  apt: name=ca‑certificates state=latest

mented out. The two most im- '/usr/sbin/apache2ctl', '‑D', 12  ‑ name: Install dumb‑init
'FOREGROUND']
portant files are container.yml 13  a
pt: deb=https://github.com/Yelp/
08  dev_overrides: dumb‑init/releases/download/v1.0.2/
and main.yml (Figure 1). Open dumb‑init_1.0.2_amd64.deb
09  environment:
up container.yml and uncom- 14  ‑ name: Install Apache
10  ‑ "DEBUG=1"
ment lines so that it looks like
11 
registries: {} 15  apt: name=apache2 state=latest
Listing 1.

LINUX-MAGAZINE.COM | LINUXPROMAGAZINE.COM ISSUE 193 DECEMBER 2016 87


LINUX VOICE TUTORIALS – ANSIBLE

ansible‑container run

This will spin up a new Docker container and bind


it to port 80 so you can access the web server.
Point your web browser to http://localhost and
you should see an Apache page.

Up to the Clouds
Now we’ll move on to a more complex example –
installing a Nextcloud server. This will have ex-
actly the same structure as before (with a play-
book in main.yml that tells Ansible how to config-
ure your machine), but there will be more to do to
get the server up and running.
To begin, create a new directory and run ansible‑
container init to set up the files needed. Again,
Figure 2: There are far more the majority of the configuration will be in the
Ansible modules than we Inside the play, there’s a sublist called tasks, main.yml file. This starts in the same way as in the
can cover here, but you can which is the sequence of things that you want An- previous example (Listing 3).
find the documentation on sible to do. Tasks typically have a name and an You’ve got dumb‑init installed now, which al-
the Ansible website. action (the name is optional but useful). The ac- lows you to run a single command as you start
tion has to link to an Ansible module that defines the machine. However, your container will need
what you want to do at each stage. In the play- to run two services (Apache and MySQL), so
book in Listing 2, there are two different types of you’ll need to create a script to run the init pro-
action: raw, which is just a command to be run via cess to start both of these (see Listing 4).
SSH, and apt, which uses APT to install or manipu- Two modules are used to create the init.sh file
late packages. There are many modules that en- and lineinfile, which you use to add the lines you
able us to do other things (Figure 2). need. lineinfile is far more powerful than this; it
You can now build the container by moving can be used to search and replace particular lines by
back to the directory in which you ran the first regular expressions. However, you don’t need this
ansible-container command and running: capability. You might have noticed that the init.sh
file is placed in the root and anyone can execute it.
ansible‑container build We’re side-stepping some issues by setting it up
this way. To deploy this service properly, you
This may take a little while to run, but it should wouldn’t want to set it up like this, but then you prob-
end by successfully building the machine. There ably wouldn’t want to run the database in the con-
will be some terminal output like: tainer as well (at least not without some clustering
setup to allow it to repopulate itself if the container
Exporting built containers as images... is recreated). This setup is designed to test out soft-
Committing image... ware before deploying it to a permanent home.
Exported web with image ID The set of tasks is to grab all the packages that
you’ll need (Listing 5).
Once this has completed, you can start the con- Here you can see how to iterate through a list of
tainer items with each option being inserted as {{ item }}
with: in the command.
Listing 3: More main.yml Config

Listing 4: Run and Start Apache and MySQL


01 
‑ hosts: all

02  gather_facts: false

03  tasks: 01  ‑ name: create init file

04  ‑ raw: which python || apt‑get update 02  file: path=/init.sh state=touch mode=a+rx

05  ‑ 
raw: (which python && which aptitude) || 03  ‑ name: copy over init file1
apt‑get install ‑y python python‑apt aptitude 04  l
ineinfile: dest=/init.sh line="#!/usr/bin/
06 
‑ hosts: web dumb‑init /bin/sh"

07  tasks: 05  ‑ name: copy over init file2

08  ‑ name: Install dumb init 06  l


ineinfile: dest=/init.sh
line="/usr/sbin/apache2ctl ‑D FOREGROUND &"
09  
get_url: dest=/usr/bin/dumb‑init
url=https://github.com/Yelp/dumb‑init/releases/ 07  ‑ name: copy over init file3

download/v1.0.2/dumb‑init_1.0.2_amd64 mode=0775 08  l
ineinfile: dest=/init.sh
validate_certs=no line="/usr/bin/mysqld_safe"

88 DECEMBER 2016 ISSUE 193 LINUX-MAGAZINE.COM | LINUXPROMAGAZINE.COM


LINUX VOICE TUTORIALS – ANSIBLE

Ansible Roles and Galaxy


In this tutorial, we’ve built a playbook that sets up the roles you want the server to perform. For
our server. It works, and we can use it. However, example, you might want a server to perform
it’s not the most reusable script. For example, if the role of a LAMP web server. Ansible allows
you now wanted to set up another server that you to encapsulate the tasks necessary for
used MySQL and Apache, you’d have to copy or this to work as a Role. These roles can then
rewrite the part of the playbook that installs and be added to hosts, and Ansible will work out
enables this. In this example, everything’s quite what needs to happen to the server to enable
simple, so it wouldn’t be much work to do this. In it to perform that role.
more involved installs, however, this becomes a n Galaxy. Well-defined roles are often not proj-
lot of duplication of effort and code. Ansible has ect-specific. For example, the LAMP web
a couple of ways of reducing the duplication that server will just need a few variable changes to
can make it much easier to manage large num- make it applicable to almost any project that
bers of different configurations.
needs a server like this. Ansible Galaxy [3] is a
n Tasklists. The simplest option for sharing website that enables Ansible users to share
work between projects is tasklists. These are roles so that they can be easily added to dif-
just YML files that contain tasks, and you ferent projects without everyone having to
can include them in playbooks with a line create them from scratch (Figure 4). If you
like: include: <tasklist‑file>. need to set up a piece of open source soft-
n Roles. Rather than think about the tasks you ware, there’s a good chance that someone
want to perform on a server, we can think of has already shared a role for this.

With everything set up, you only have to dumb‑init to run the init.sh script rather than the
download the latest version of Nextcloud (List- Apache start script:
ing 6).
This uses the unarchive command to pull the command: ['/usr/bin/dumb‑init', '/init.sh']

software from a remote repository straight into


the web root on the host. The copy=no option to Now you’re ready to launch our containers with
unarchive is needed because of a bug in Ansible the same commands as before:
when using remote_src on unarchive. The final
task here makes sure that files in the webroot are ansible‑container build

owned by the correct user. ansible‑container run

With main.yml set up, you just need to adjust


container.yml. The only difference between this If you point your web browser to localhost/
and the previous example is that you need to tell nextcloud, you’ll be able to enter the details
needed to finish the Nextcloud install. The data-
base login is root with no password. Again, this
Listing 5: Grab the Packages isn’t the most secure setup, but again, this da-
01  ‑ name: Upgrade APT tabase setup isn’t designed for live use. The
02  apt: upgrade=yes best setup depends a lot on your current sys-
03 
tem, whether you’ve already got a database
04  ‑ name: Install Everything
server you can use, and the amount of load
you’re expecting on the system.
05  apt: name={{item}} state=installed
When the containers are running, you might
06  with_items:
want to interact with them – maybe to just
07  ‑ apache2
have a poke around in the system or maybe to
08  ‑ git

09  ‑ mysql‑server

10  ‑ unzip Listing 6: Download Nextcloud


11  ‑ libapache2‑mod‑php5
01  ‑ name: download nextcloud
12  ‑ php5‑gd
02  u
narchive: copy=no remote_src=yes
13  ‑ php5‑json src=https://download.nextcloud.com/
server/releases/nextcloud‑10.0.0.zip
14  ‑ php5‑mysql
dest=/var/www/html/
15  ‑ php5‑curl
03 
16  ‑ php5‑intl
04  ‑ name: set permissions
17  ‑ php5‑mcrypt
05  f
ile: path=/var/www/htmlrecurse=yes
18  ‑ php5‑imagick owner=www‑datastate=directory

90 DECEMBER 2016 ISSUE 193 LINUX-MAGAZINE.COM | LINUXPROMAGAZINE.COM


TUTORIALS – ANSIBLE LINUX VOICE

Why Not Dockerfiles?


Docker does of course have its own provi-
sioning system – Dockerfiles – that can be
used to set up containers. However, the prob-
lem with Dockerfiles is that they can only be
used with Docker. Effectively, they bundle ev-
erything up into a single technology stack for
both your provisioning and hosting.
With Ansible Container, you separate this out.
Docker is used to run the images, but the pro-
visioning is done via Ansible playbooks. This
means you can use Ansible Container to cre-
ate your development environment. You can
also deploy with Ansible Container if you
wish; but, if this isn’t the right choice for you,
the same playbooks you used to build your Figure 3: Our Nextcloud
containers can be used to set up virtual serv- You can then attach a Bash session (as root) to server running in a Docker
ers or physical servers. the container using the ID listed in the first column container provisioned by
By decoupling the provisioning from the of the output of the previous command with: Ansible.
hosting, Ansible Containers give you far more
flexibility than Dockerfiles. sudo docker exec ‑i ‑t <id> /bin/bash

With this, you have a container up and running


add or remove something without having to re- Nextcloud, and you can connect to it via a termi-
build the container. Ansible Container creates nal to take care of any admin (Figure 3). By using
your containers, but they’re run through a combination of Ansible and Docker, you can
Docker, so this is the tool you’ll need to access easily customize your containers to perform al-
them (See the box entitled “Why Not Docker- most any function and share the configurations
files?”). You can see a list of currently running online using Ansible Galaxy (see boxout). You
Docker containers with: can then deploy your handiwork on almost any
infrastructure, giving you a great combination of
sudo docker ps ease, flexibility, and power. n n n

Info
[1] Ansible Container:
https://​­www.​
­ansible.​­com/​
­ansible‑container
[2] Docker Machine:
https://​­docs.​­docker.​
­com/​­machine/​
­install‑machine/
[3] Galaxy: https://​
Figure 4: Set up servers automatically by downloading Ansible roles from the Galaxy website. ­galaxy.​­ansible.​­com

LINUX-MAGAZINE.COM | LINUXPROMAGAZINE.COM ISSUE 193 DECEMBER 2016 91

You might also like