Using Ansible

My 9 year old home desktop has been slowly croaking it for weeks now. I got a new desktop from PB Tech (a very pleasurable experience ) and did a fresh install of Ubuntu 16.04 LTS Xenial yesterday. I want to set up my dev tools and other software, but want to control it and ensure it is repeatable, and the only way to do that is with code. Enter Ansible which I became enamoured with over a year ago. It may be over the top to set up your new development/workstation PC with a runnable script, but it should be fun.

PROCESS

All management and configuration of new PC, apart from setting up the initial Ansible install, will be source controlled code and be repeatable.

This is being done on a fresh Ubuntu 16.04 LTS Xenial Desktop install.

OUTPUT

A playbook in git that anyone will be able to run on a Ubuntu flavoured fresh Linux install to get a PC up and running with a similar development environment. A good record of this documented below for mere mortals.

Initial Manual Installs

We need to install Ansible and anything else it needs to get up and going.

byron@barret:~/$ sudo apt-get install software-properties-common
byron@barret:~/$ sudo apt-add-repository ppa:ansible/ansible
byron@barret:~/$ sudo apt-get update
byron@barret:~/$ sudo apt-get install ansible

Surprisingly ssh is not installed by default for Ubunutu desktop

byron@barret:~/$ sudo apt-get install openssh-server

Setting up Ansible locally and connecting to new Sandbox

To get us up and going, we will do simple Ansible set up. Using vi as editor but you can use a graphical editor as the cut down vi installed is a pain.

Make a working directory for Ansible work.

byron@barret:~/$ mkdir ansible
byron@barret:~/$ cd ansible

We will need an Inventory file for our development environment. For now this will contain just our sandbox, later it could contain cloud servers.

byron@barret:~/ansible$ vi development.ini
[workstations]
barret # name of you workstation/sandbox

You can test your Ansible install it’s connection to your new sandbox by issuing an ad-hoc ping command. The ask-pass switch allows you to enter password for ssh connection as Ansible by default assumes you are using SSH keys which will be set up later.

byron@barret:~/ansible$ ansible all -i development.ini -m ping --ask-pass
SSH password: 
barret | SUCCESS => {
 "changed": false, 
 "ping": "pong"
}

Something like the above indicates success and we are ready to make a playbook to install some software.

Installing a Text Editor

Until we install it, we keep using vi. For yml files watch the formatting as it is very important.

byron@barret:~/ansible$ vi workstations.yml
---
- hosts: workstations
  remote_user: byron
  become: yes
  become_method: sudo
  tasks:
  - name: Add atom ppa repository
    apt_repository:
    repo: 'ppa:webupd8team/atom'
    codename: 'xenial'
    state: present

  - name: Install atom
    apt:
      name: atom
      state: present

  - name: Install git
    apt: 
      name: git
      state: present

We have also added git so we check this work in at the end. To run we need to also enter the sudo password with–ask-become-pass as well as the ssh password.

byron@barret:~/ansible$ ansible-playbook -i development.ini workstations.yml --ask-pass --ask-become-pass
SSH password: 
SUDO password[defaults to SSH password]:

PLAY [workstations] ************************************************************

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

TASK [Add atom ppa repository] *************************************************
changed: [barret]

TASK [Install atom] ************************************************************
changed: [barret]

TASK [Install git] *************************************************************
ok: [barret]

PLAY RECAP *********************************************************************
barret : ok=4 changed=3 unreachable=0 failed=0

Now we Should have Atom installed and we can continue without using vi. Yahh! Last thing to do is to check in work.

Git repository for this will be at https://github.com/mabznz/dev-env

Installing MySQL

This could be a long and laborious task, especially if you are not a MySQL expert or DBA. Thankfully with Ansible there is a huge resource of playbooks that people have already written that can massively help.

The only problem I had was that MariaDB is recommended for Ubuntu now (and really for all open source work) after Oracle acquired MySQL. I did not know it even existed or what the story was but if you are keen for a good read, 10 reasons to migrate to MariaDB. Some from now on it is MariaDB.

I downloaded from Github a MySQL role authored by an Ansible guru. I did not clone it as I am not up to the pace on git yet. Using this MySQL role is a good gentle introduction to the power of roles, how they can be used to organise your Ansible work, and how easy it is to reuse other peoples work.

cd ~/ansible
wget https://github.com/geerlingguy/ansible-role-mysql/archive/master.zip
unzip master.zip
mkdir roles/mariadb
mv ansible-role-mysql-master/* roles/mariadb/

I did have to make some small changes to the files to get a later version of MariaDB installed. That’s all included in my repo. The other changes you need to make is to the defaults/main.yml file around users and passwords. This is all in the original ansible-role-mysql documentation.

Once you have made those file changes, all you need to do is include two new lines in your main playbook and run it to install and configure a very secure database server.

vi workstations.yml
...
---
- hosts: workstations
  roles:
  - mariadb
  remote_user: byron
  become: yes
...

WARNING: The only problem I had was around a peculiar error “MariaDB Plugin ‘unix_socket’ is not loaded” that has stumped many around it seems. The fix is here.

Installing NodeJS

This was easy, but along with a lot of software, you don’t want to just install the version out of the Ubuntu standard repository, you want the latest stable release.

For NodeJS that involved installing curl and getting the binary distribution via a shell command. This will help when you need to update the version and is the recommended method for installing NodeJS on Ubuntu distributions.

- name: Install curl
  apt:
  name: curl
  state: present

- name: Get nodejs from Ubuntu binary distributions repository
  shell: curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -

- name: Install nodejs
  apt:
  name: nodejs
  state: present
Advertisements