Professional Documents
Culture Documents
086-091 LV Ansible
086-091 LV Ansible
086-091 LV 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
ers that host your code and uses Ansible playbooks 58118E89F3A912897C070ADU
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
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
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.
ansible‑container run
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
02 gather_facts: false
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"
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"
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']
09 ‑ mysql‑server
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