Ansible on The Captain's Shackhttps://www.the-captains-shack.com/tags/ansible/index.xml
Recent content in Ansible on The Captain's ShackHugo -- gohugo.ioen-usSpring refactoring of the Captain's shackhttps://www.the-captains-shack.com/post/refactoringTheShack/
Sun, 02 Apr 2017 00:01:00 +0100https://www.the-captains-shack.com/post/refactoringTheShack/<hr />
<p><strong>[TL;DR]</strong></p>
<p><em>Ansible roles are powerful to turn features on or off. The site&rsquo;s monolithic master Docker-compose doesn&rsquo;t lend itself for that. This article describes the script&rsquo;s refactoring to solve this.</em></p>
<hr />
<p>The best way to prevent tampering on an Internet facing server is to rebuild it regularly. And the only efficient way to do it is the scripted way. From day one, the &ldquo;Captain&rsquo;s Shack&rdquo; server has been configured with Ansible so that the server can be regularly reinitialised.<br />
Each component, defined in an Ansible role, is deployed as a Docker container. To orchestrate these containers, I use a central &ldquo;docker-compose&rdquo; file.</p>
<p><img src="https://www.the-captains-shack.com/images/refactoringAnsible/Kimsufi.png" alt="kimsufi" /></p>
<p>Initially, the docker configuration (and the docker-compose) was managed via a single docker-compose file. This design did not allow to disable a service (like Nexus or Lime Survey) by just commenting out the Ansible role. It required adapting several Ansible scripts or configuration file. Very impractical.</p>
<p>When I wanted to turn off the &ldquo;Lime Survey&rdquo; service it made refactoring need obvious.</p>
<p>The main issue with enabling/disabling a service is the required change to the docker-compose file. And this in different sections of the file. Obviously the service needs to be updated but also the volume section and, more annoying, the &ldquo;web&rdquo; service (Nginx) one.</p>
<p>I initially tried to achieve this scripted per role Docker-compose update with the blockinfile Ansible module. It turned out to be clumsy. The module doesn&rsquo;t lend itself easily to properly handle yaml block insertions: the block is inserted at column 0. The workaround is to start the block with a dummy line that would set the correct offset of the rest of the block (as this line is inserted at column 0). That dummy line needs to be removed in the following step. Also annoying is that the module inserts each time a &ldquo;noisy&rdquo; comment before and after the inserted block. This makes the generated docker-compose file difficult to read for a human reader. Repeatedly manipulating individual line in the &ldquo;web&rdquo; service part is also very annoying and inefficient.</p>
<p>So I chose to go for the proverbial plan B.</p>
<p>The idea is to have several docker-compose.yml that will first be &ldquo;superposed&rdquo; before being processed by the docker-compose engine.</p>
<p><img src="https://www.the-captains-shack.com/images/refactoringAnsible/layers2.png" alt="kimsufi" /></p>
<p>Each compose file defines what is needed to activate a single service. It describes the service and volume part but also the required additional configuration to the &ldquo;web&rdquo; service (reverse proxy).</p>
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span>version: <span style="background-color: #fff0f0">&#39;2&#39;</span>
services:
web:
depends_on:
- nexus
nexus:
image: sonatype/nexus3
ports:
- <span style="background-color: #fff0f0">&quot;8081:8081&quot;</span>
volumes:
- nexus-data:/nexus-data
volumes:
nexus-data:
driver: local
</pre></div>
<p>These compose files can be assembled either with the <code>-f</code> switch of the docker-compose command line or by setting the COMPOSE_FILE environment variable with list of these files separated by a &ldquo;:&ldquo;. I chose the second strategy because it was easier to setup correctly with Ansible.</p>
<p>So each role was modified to have each its own docker-compose file fragment. It is stored in the <code>data/docker</code> where most of the docker stuff is stored.</p>
<p>A script file (<code>setComposeList.sh</code>) is added so that the required environment variable is correctly set up. Each role adds the name of its compose file to the variable definition.</p>
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #007020">export</span> <span style="color: #996633">COMPOSE_FILE</span><span style="color: #333333">=</span>nginx-compose.yml:gogs-compose.yml:hugo-compose.yml:jenkins-compose.yml:nexus-compose.yml
</pre></div>
<p>To package everything correctly, a set of scripts are also installed. I have a <code>start_web.sh</code>, a <code>stop_web.sh</code>, a <code>restart_web.sh</code> and a <code>build_and_start_web.sh</code> script. The latest is shown here after.</p>
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #888888">#!/usr/bin/env bash</span>
<span style="color: #007020">cd</span> /home/data/docker
. ./setComposeList.sh
docker-compose up --build -d
docker-compose ps
</pre></div>
<p>The ansible scripts discussed here are available on <a href="https://github.com/jmMeessen/the-captains-shack" target="_blank">my Github repo</a>.</p>