Unifi Controller Software with Ansible

I like the Unifi range of networking gear, but the two options to control it feel unnecessarily burdensome.

  • If you're feeling rich and wasteful you can always buy their cloudkey product.
  • If you have lot's of time on your hands and are not easily frustrated, you can use Unifi's controller software on your own host. I didnt feel rich so I went for option 2. But I really don't want to devote much time to maintaining the controller software.

It appears that the controller software you deploy will eventually find it's way on to most of Unifi's appliances which will run lightweight linux instances. So perhaps a simpler method of installing and updating will emerge.

There are some bash scripts which will help you deploy the software on an Ubuntu host. These are lovingly maintained by some dedicated folks. However, looking through the code I nearly had an aneurism when I saw it's complexity. As a result, upgrades to the operating system often cause the scripts to break until new code is added. I wanted a more reliable deployment and update method.

Below you will find a very simple Ansible script to deploy all the necessary dependencies and to install the controller software. Re-running the script will update the software if newer versions are available.

Unifi Controller Software with Ansible

This script was written for Ubuntu 20.04 (Focal Fossa), but may work on earlier releases as well.

 1---
 2  - name: Unifi Controller Deployment
 3    hosts: my_unifi_ctrl01
 4    become: yes
 5    vars:
 6      packages:
 7        - 'unifi'
 8      mongoDB_ver: "3.4"
 9      java_pkgs:
10        - openjdk-8-jre-headless
11      mongoDB_pkgs:
12        - mongodb-org-tools
13        - mongodb-org-mongos
14        - mongodb-org-server
15        - mongodb-org-shell
16        - mongodb-org
17      libssl_url: "http://security.ubuntu.com/ubuntu/pool/main/o/openssl1.0/libssl1.0.0_1.0.2n-1ubuntu5.3_amd64.deb"
18      libssl_tmp: '/tmp/libssl1.0.0.deb'
19    tasks:
20      - name: Install APT key for Unifi
21        apt_key:
22          url: https://dl.ui.com/unifi/unifi-repo.gpg
23
24      - name: Install repo for Unifi controller
25        apt_repository:
26          repo: deb https://www.ui.com/downloads/unifi/debian stable ubiquiti
27          state: present
28          filename: '100-ubnt-unifi'
29
30      - name: Install OpenJDK ver 8
31        apt:
32          name: "{{java_pkgs}}"
33          state: present
34
35      - name: Install latest MongoDB APT key.
36        apt_key:
37          url: "https://www.mongodb.org/static/pgp/server-{{ mongoDB_ver }}.asc"
38
39      - name: Install MongoDB Repo
40        apt_repository:
41          repo: "deb https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/{{mongoDB_ver}} multiverse"
42          state: present
43          filename: "mongodb-org-{{mongoDB_ver}}"
44
45      - name: Download libssl
46        get_url:
47          url: "{{libssl_url}}"
48          dest: "{{ libssl_tmp }}"
49
50      - name: install libssl
51        apt:
52          deb: "{{libssl_tmp}}"
53
54      - name: Install MongoDB packages
55        apt:
56          name: "{{ mongodb_pkgs }}"
57
58      - name: Install latest Unifi package
59        apt:
60          name: "{{packages}}"
61          state: latest
62        notify: Start Unifi Service
63
64      - name: Remove temporary libssl file
65        file:
66          path: "{{libssl_tmp}}"
67          state: absent
68
69    handlers:
70      - name: Start Unifi Service
71        systemd:
72          name: 'unifi'
73          state: started