ansible-openwisp2

repository·master·Indexed 19 days ago

https://github.com/openwisp/ansible-openwisp2

An Ansible role designed to automate the installation and configuration of the OpenWISP Server Application. It supports Debian (Trixie/Bookworm) and Ubuntu (26/24/22 LTS) with Ansible Core 2.13 or higher. The role includes capabilities for configuring Let's Encrypt SSL certificates via certbot, managing CORS headers using django-cors-headers, deploying custom static content, and setting up FreeRADIUS for WPA Enterprise (EAP-TTLS-PAP) authentication with multi-tenancy support.

Tokens
10.3K
Snippets
31
Records
42
Agent score
64%

What's inside ansible-openwisp2

  1. Deploy the OpenWISP Server Application with Ansible

    master

    The ansible-openwisp2 role is used to deploy the OpenWISP Server Application.

    Requirements and Compatibility:

    • Ansible Core: Minimum version 2.13 is recommended.
    • Supported Operating Systems:
      • Debian (Trixie/Bookworm)
      • Ubuntu (26/24/22 LTS)

    For detailed configuration, refer to the specific usage guides for system requirements, quickstart, SSL configuration, and role variables.

  2. Install Ansible and prerequisites on your local machine

    master

    Before deploying OpenWISP, you must install Ansible on your local machine (the machine from which you will launch the deployment). The production server is the remote machine where OpenWISP will be hosted.

    Requirements:

    • Ansible (minimum recommended version 2.13).
    • Jinja2 (version >= 2.11) in your Python environment.
    • sshpass (required if you use the -k flag for password-based SSH authentication).

    It is recommended to install Ansible within a Python virtual environment to avoid dependency conflicts.

    pip install Jinja2>=2.11
  3. Install the OpenWISP Ansible role and required collections

    master

    Install the openwisp.openwisp2 role and the necessary Ansible collections on your local machine using ansible-galaxy.

    ansible-galaxy install openwisp.openwisp2
    ansible-galaxy collection install "community.general:>=3.6.0"
    ansible-galaxy collection install "ansible.posix"
  4. Configure additional django-cors-headers settings

    master

    The openwisp.openwisp2 role provides a limited abstraction for django-cors-headers via the openwisp2_django_cors variable. To configure settings not covered by the standard abstraction (such as CORS_ALLOW_CREDENTIALS or CORS_ALLOW_ALL_ORIGINS), use the openwisp2_extra_django_settings_instructions variable.

    This variable accepts a list of strings, where each string contains valid Python code that will be used to set Django settings.

    - hosts: openwisp2
      become: "{{ become | default('yes') }}"
      roles:
        - openwisp.openwisp2
      vars:
        openwisp2_django_cors:
          enabled: true
          allowed_origins_list:
            - https://frontend.openwisp.org
            - https://logs.openwisp.org
        # Configuring additional settings for django-cors-headers
        openwisp2_extra_django_settings_instructions:
          - |
            CORS_ALLOW_CREDENTIALS = True
            CORS_ALLOW_ALL_ORIGINS = True
  5. Run the OpenWISP deployment playbook

    master

    Execute the deployment from your local machine using ansible-playbook.

    Command Syntax: ansible-playbook -i hosts playbook.yml -u <user> -k --become -K

    • -i hosts: Specifies the inventory file.
    • -u <user>: The username on your production server.
    • -k: Prompts for the SSH password (requires sshpass).
    • --become: Tells Ansible to use sudo to run commands.
    • -K: Prompts for the sudo password.

    Note: If you have your public SSH key installed on the server, you can omit -k, --become, and -K.

    Post-Deployment: Once finished, access the admin interface at https://<your-hostname>/admin using:

    • Username: admin
    • Password: admin

    Immediately change the admin password and update the Site object and Organization details.

    ansible-playbook -i hosts playbook.yml -u <user> -k --become -K
  6. Access the OpenWISP environment as the www-data user

    master

    All OpenWISP processes run as the www-data user. To edit files or perform tasks within the OpenWISP environment with the correct permissions and environment, switch to the www-data user and activate the virtual environment:

    sudo su www-data -s /bin/bash
    cd /opt/openwisp2
    source env/bin/activate
    sudo su www-data -s /bin/bash
    cd /opt/openwisp2
    source env/bin/activate
  7. Install OpenWISP for testing using Vagrant

    master

    The fastest way to set up an OpenWISP development environment is by using Vagrant. This method automates the installation process.

    To use this method:

    1. Clone the vagrant-openwisp2 repository: https://github.com/openwisp/vagrant-openwisp2.
    2. Follow the instructions provided in the repository's README.md to perform the automatic installation.
  8. Enable the RADIUS Module

    master

    To enable the RADIUS module, set openwisp2_radius to true.

    Key configuration options:

    • openwisp2_freeradius_install: Set to true to provide a basic FreeRADIUS configuration for OpenWISP including the radius user token mechanism. Set to false if you manage FreeRADIUS separately.
    • openwisp2_radius_urls: Set to true to register OpenWISP-RADIUS API endpoints. Set to false to prevent registration.
    - hosts: openwisp2
      become: "{{ become | default('yes') }}"
      roles:
        - openwisp.openwisp2
      vars:
        openwisp2_radius: true
        openwisp2_freeradius_install: true
        # set to false when you don't want to register openwisp-radius API endpoints.
        openwisp2_radius_urls: true
  9. Enable the Firmware Upgrader Module

    master

    To enable the Firmware Upgrader module, set openwisp2_firmware_upgrader to true in your playbook. You can also configure module-specific settings using openwisp2_extra_django_settings or openwisp2_extra_django_settings_instructions to pass custom Django configuration (e.g., custom OpenWrt images).

    - hosts: openwisp2
      become: "{{ become | default('yes') }}"
      roles:
        - openwisp.openwisp2
      vars:
        openwisp2_firmware_upgrader: true
        openwisp2_extra_django_settings_instructions:
          - |
            OPENWISP_CUSTOM_OPENWRT_IMAGES = (
                ('my-custom-image-squashfs-sysupgrade.bin', {
                    'label': 'My Custom Image',
                    'boards': ('MyCustomImage',)
                }),
            )
  10. Configure FreeRADIUS for WPA Enterprise (EAP-TTLS-PAP)

    master

    To enable WPA Enterprise (EAP-TTLS-PAP) authentication using OpenWISP RADIUS, you must configure the openwisp.openwisp2 Ansible role to create individual FreeRADIUS sites for each organization. This supports multi-tenancy by isolating authentication per organization.

    Key Requirements:

    • Set openwisp2_radius: true.
    • Set openwisp2_freeradius_install: true.
    • Define a list of organizations in the freeradius_eap_orgs variable.

    Port Isolation: Because the Ansible role creates a common FreeRADIUS site for all organizations (used for captive portals) listening on default ports (1812, 1813, 18120), you must provide unique ports for each organization in freeradius_eap_orgs to ensure proper isolation.

    - hosts: openwisp2
      become: "{{ become | default('yes') }}"
      roles:
        - openwisp.openwisp2
      vars:
        openwisp2_radius: true
        openwisp2_freeradius_install: true
        freeradius_eap_orgs:
          - name: openwisp
            uuid: 00000000-0000-0000-0000-000000000000
            radius_token: secret-radius-token
            auth_port: 1822
            acct_port: 1823
            inner_tunnel_auth_port: 18230
            cert: /etc/freeradius/certs/cert.pem
            private_key: /etc/freeradius/certs/key.pem
            ca: /etc/freeradius/certs/ca.crt
            dh: /etc/freeradius/certs/dh
            tls_config_extra: |
              private_key_password = whatever
              ecdh_curve = "prime256v1"
  11. Deploy the development version of OpenWISP

    master

    To use the latest unreleased features, you can deploy the master branch of the Ansible role. This requires a custom directory structure to manage local roles and collections.

    1. Setup Directory Structure

    Create a directory (e.g., ~/openwisp-dev) with roles and collections subdirectories.

    mkdir -p ~/openwisp-dev/roles
    mkdir -p ~/openwisp-dev/collections
    cd ~/openwisp-dev/
    touch ansible.cfg requirements.yml hosts playbook.yml

    2. Configure ansible.cfg

    Set the paths for roles and collections in your ansible.cfg:

    [defaults]
    roles_path=~/openwisp-dev/roles
    collections_paths=~/openwisp-dev/collections

    3. Define Requirements

    In requirements.yml, point to the OpenWISP Git repository and specify the master version:

    ---
    roles:
      - src: https://github.com/openwisp/ansible-openwisp2.git
        version: master
        name: openwisp.openwisp2-dev
    collections:
      - name: community.general
        version: ">=3.6.0"

    4. Install and Run

    Install the requirements and run your playbook:

    ansible-galaxy install -r requirements.yml
    ansible-playbook -i hosts playbook.yml

    Example Development Playbook:

    - hosts: openwisp2
      become: "{{ become | default('yes') }}"
      roles:
        - openwisp.openwisp2-dev
      vars:
        openwisp2_network_topology: true
        openwisp2_firmware_upgrader: true
        openwisp2_radius: true
        openwisp2_monitoring: true
    ansible-galaxy install -r requirements.yml
    ansible-playbook -i hosts playbook.yml
  12. Configure CORS Headers in OpenWISP

    master

    To resolve Cross-Origin Resource Sharing (CORS) issues when integrating OpenWISP with external services, use the openwisp2_django_cors variable within the openwisp.openwisp2 role. This role leverages the django-cors-headers package.

    Basic Configuration

    Create a playbook that defines the openwisp2_django_cors variable. At a minimum, you can enable CORS and specify a list of allowed origins using allowed_origins_list.

    Steps to Apply

    1. Install Ansible and the openwisp.openwisp2 role.
    2. Create an inventory file.
    3. Create a playbook (see example below).
    4. Run the playbook.
    5. Verify by logging into the OpenWISP admin panel (e.g., https://openwisp2.mydomain.com/admin).
    - hosts: openwisp2
      become: "{{ become | default('yes') }}"
      roles:
        - openwisp.openwisp2
      vars:
        # Cross-Origin Resource Sharing (CORS) settings
        openwisp2_django_cors:
          enabled: true
          allowed_origins_list:
            - https://frontend.openwisp.org
            - https://logs.openwisp.org