Using Ansible To Bootstrap Ubuntu

After creating a new VPS Ubuntu instance, one of the first things I do is lock down root access and create an administrator account.

I was using Fabric to push out these modifications. Recently, however, I decided to switch to Ansible instead.


Here are the tasks this playbook needs to perform:

  1. Install ansible requirements.
  2. Create administrator user account.
  3. Add administrator account to sudo group.
  4. Add public keys from GitHub to authorized keys.
  5. Add firewall with SSH hole
  6. Secure SSH:
    • Fail2Ban
    • Disable password auth
    • Disable root login


This is the resulting playbook:

Running it

An example of a playbook run:

$ git clone
Cloning into 'efed5a81b1622acfebf9'...
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
Checking connectivity... done.

$ cd efed5a81b1622acfebf9/

$ ansible-playbook -i ',' -e 'admin_user=admin' -e 'github_user=belminf' bootstrap.yaml

PLAY [all] ********************************************************************

TASK: [verify python installed] ***********************************************
The authenticity of host ' (' can't be established.
ECDSA key fingerprint is SHA256:Ih25OXlSJzQFkkug+2zzAP/IInaU0txqdhamMi7l2iM.
Are you sure you want to continue connecting (yes/no)? yes
failed: [] => {"rc": 1}

TASK: [install python] ********************************************************
ok: []

TASK: [sudoers for authkey-only auth] *****************************************
changed: []

TASK: [add admin user accounts] ***********************************************
changed: []

TASK: [add pubkeys from GitHub] ***********************************************
changed: []

TASK: [install fail2ban] ******************************************************
changed: []

TASK: [ensure fail2ban enabled] ***********************************************
ok: []

TASK: [add SSH ufw rule] ******************************************************
ok: []

TASK: [set default ufw policy] ************************************************
changed: []

TASK: [disable SSH password auth] *********************************************
changed: []

TASK: [disable SSH root logins] ***********************************************
changed: []

NOTIFIED: [restart fail2ban] **************************************************
changed: []

NOTIFIED: [restart SSH] *******************************************************
changed: []

PLAY RECAP ********************************************************************         : ok=13   changed=9    unreachable=0    failed=0


I kept this to the absolute minimuim required to lock down the root account and create an administrator account. Everything else I do with a more comprehensive playbook.

Tested successfully on Ubuntu 14.04 and 15.10 with Ansible 1.9.4.