Use Ansible to Deploy Software from git

Imagine you work on an application on a development server for several months until it is time to deploy it to a production system for the first time. Chances are, there are several necessary configuration tasks just waiting to be forgotten: firewall permissions, specific software libraries, file permissions and so on.

Ansible offers a reproducible and automatable way to take care of these configurational changes for you – and the beauty is: it does not depend on a specific Linux flavour and it works both for single-machine deployments and distributed systems.

If you were never wondering why your application exits with an HTTP error until you have noticed that the cache folder did not have the correct permissions, stop reading; if you have never forgotten which libraries you had to apt-get install before the Makefile finally completed without errors, this is not the guide for you. Otherwise, see how a simple 50 line yml file can take care of your deployment challenges.

tl;dr – the Ansible Script

This script demonstrates how to fetch and build Kismet (as explained in Install Kismet on Ubuntu 19.04 from Source).

---
- name: Deploy
  hosts: localhost
  tasks:
          - name: Install necessary tools and libraries
            apt:
                name: "{{ packages }}"
            vars:
                packages:
                - git
                - build-essential
                - libmicrohttpd-dev
                - pkg-config
                - zlib1g-dev
                - libnl-3-dev
                - python-protobuf
                - python-requests
                - librtlsdr0
                - python-usb
                - python-paho-mqtt
                - libusb-1.0-0-dev
                - libsqlite3-dev
                - protobuf-compiler
                - protobuf-c-compiler
                - libsensors4-dev
                - python
                - python-setuptools
                - libnl-genl-3-dev
                - libcap-dev
                - libpcap-dev
                - libnm-dev
                - libdw-dev
                - libprotobuf-dev
                - libprotobuf-c-dev
                - net-tools
            become: yes
          - name: Download source files from github
            git:
                repo: https://github.com/kismetwireless/kismet.git
                dest: /tmp/kismet
          - name: Configure
            command: ./configure
            args:
                chdir: /tmp/kismet
          - name: Run make as root
            make:
                chdir: /tmp/kismet
            become: yes
          - name: Run make with target suidinstall
            make:
                chdir: /tmp/kismet
            become: yes
            target: suidinstall

Running it will generate the following output:

admin@instance-1:~$ ansible-playbook deploy.yml 
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

 [WARNING]: Ignoring invalid attribute: target


PLAY [Deploy] **************************************************************************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Install necessary tools and libraries] *******************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Download source files from github] ***********************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Configure] ***********************************************************************************************************************************************************************************************************************************************************************************************************************
changed: [localhost]

TASK [Run make as root] ****************************************************************************************************************************************************************************************************************************************************************************************************************
changed: [localhost]

TASK [Run make with target suidinstall] ************************************************************************************************************************************************************************************************************************************************************************************************
changed: [localhost]

PLAY RECAP *****************************************************************************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=6    changed=3    unreachable=0    failed=0

Explanation

There are five steps in the ansible playbook:

  1. Install the necessary tools and libraries for the build process
  2. Fetch the sources from git
  3. Run configure
  4. Run make
  5. Run make suidinstall

Since ansible is idempotent, the playbook can be run multiple times and each step will guarantee a valid outcome (e.g., if the libraries are already installed, nothing will happen).

Use this ansible script as a starting point to create your own deployment pipeline and create an application server from scratch with your exact specifications in a matter of minutes.

Bernhard Knasmüller on Software Development