NGINX Unit
v. 1.23.0

Unit in Ansible§

The Ansible collection by XLAB Steampunk provides a number of Unit-related tasks that you can use with Ansible; some of them simplify installation and setup, while others provide common configuration steps.

Note

Ansible 2.9+ required; the collection relies on official packages and supports CentOS and Debian only.

A brief intro by the collection’s authors can be found here; a behind-the-scenes blog post is here.

First, install the collection:

$ ansible-galaxy collection install steampunk.unit

After installation, you can use it in a playbook. Consider this WSGI app:

def application(environ, start_response):
    start_response("200 OK", [("Content-Type", "text/plain")])
    return (b"Hello, Python on Unit!")

This playbook installs Unit with the Python language module, copies the app’s file, and runs the app:

---
- name: Install and run NGINX Unit
  hosts: unit_hosts
  become: true

  tasks:
    - name: Install Unit
      include_role:
        name: steampunk.unit.install

    - name: Create a directory for our application
      file:
        path: /var/www
        state: directory

    - name: Copy application
      copy:
        src: files/wsgi.py
        dest: /var/www/wsgi.py
        mode: "644"

    - name: Add application config to Unit
      steampunk.unit.python_app:
        name: sample
        module: wsgi
        path: /var/www

    - name: Expose application via port 3000
      steampunk.unit.listener:
        pattern: "*:3000"
        pass: applications/sample

The final preparation step is the host inventory that lists your managed hosts’ addresses:

all:
  children:
    unit_hosts:
      hosts:
        10.0.2.15:

With everything in place, start the playbook:

$ ansible-playbook -i inventory.yaml playbook.yaml

      PLAY [Install and run NGINX Unit] ***

      ...

      TASK [Expose application via port 3000] ***
      ok: [10.0.2.15]

      PLAY RECAP ********************************
      10.0.2.15                  : ok=15   changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

If it’s OK, try the app at the host address from the inventory and the port number set in the playbook:

$ curl 10.0.2.15:3000

      Hello, Python on Unit!