Trellis Documentation

repository·master·Indexed 25 days ago

https://github.com/roots/trellis

An Ansible-powered LEMP stack automation tool for WordPress. Trellis provides infrastructure automation for local development using Lima VMs and high-performance production servers with zero-downtime deployments for Bedrock-based sites. It includes specialized Ansible roles for Fail2ban, firewall management via ansible-ferm, Let's Encrypt SSL certificates via acme-tiny, and SSH configuration.

Tokens
4.3K
Snippets
10
Records
25
Agent score
82%

What's inside Trellis

  1. What is ansible-ferm?

    master
    ansible-ferm is an Ansible role designed to manage iptables using the ferm tool. Instead of executing individual allow/deny commands (like the ufw module), it uses a template-based approach to manage firewall rules, which promotes a more idempotent setup.
  2. What is Trellis?

    master

    Trellis is a collection of Ansible playbooks designed to set up a LEMP (Linux, Nginx, MySQL, PHP) stack specifically optimized for WordPress. It provides infrastructure automation for various environments, including:

    • Local development: Using Lima VMs.
    • Production: High-performance server configurations.
    • Deployment: Zero-downtime deployments for WordPress sites built on Bedrock.

    For easier management of these tasks, it is recommended to use trellis-cli.

  3. View active SSH configurations

    master

    The sshd role omits options from the configuration files if the system defaults are secure. To see the full, active configuration currently in effect on your server, use the following commands:

    • SSH Server (sshd_config): sshd -T
    • SSH Client (ssh_config): ssh -G example.com
  4. Create custom Fail2ban filters

    master

    To use a custom filter with a service, you must place a .conf.j2 file in the directory specified by fail2ban_filter_templates_path (defaults to fail2ban_filters located next to your group_vars folder).

    If you are using filters provided by plugins that end in .conf, you can simply append .j2 to the filename to make it compatible with Trellis.

    For logic implementation, refer to the official Fail2ban documentation on Developing Filters.

  5. Get started with Trellis installation

    master

    To begin using Trellis, you must follow the official installation documentation. The setup process involves configuring Ansible to provision your LEMP stack environments.

    See the [Trellis installation documentation](https://roots.io/trellis/docs/installation/).
  6. Add custom services to monitor with Fail2ban

    master

    By default, Trellis only monitors SSH connections. To monitor additional services, define a list in fail2ban_services_custom within your group_vars (e.g., group_vars/all/security.yml).

    Each service in the list requires a name, port, filter, and logpath. You can optionally override protocol, maxretry, action, and banaction for specific services.

    Note: enabled must be a string (e.g., "true").

    fail2ban_services_custom:
      - name: wordpress
        filter: wordpress
        logpath: /var/log/auth.log
        maxretry: 2
  7. Customize SSH settings via variables

    master

    You can override default SSH settings by redefining variables in your Ansible group variables (e.g., group_vars/all/main.yml).

    Variable Naming

    • Most settings are defined in templates/sshd_config.j2 or templates/ssh_config.j2.
    • For the SSH client, many ssh_<varname> variables default to the value of their sshd_<varname> counterpart to avoid duplication.

    Example: Overriding AcceptEnv

    To allow specific environment variables in the SSH server, redefine sshd_accept_env as a list:

    # group_vars/all/main.yml
    sshd_accept_env:
      - LANG
      - LC_*
  8. Prevent lockout when modifying SSH settings

    master

    When modifying SSH configurations, you risk blocking future access. Before running the sshd role, open a backup SSH connection in a separate terminal. Use the ServerAliveInterval option to prevent the connection from being pruned by the server or NAT routers.

    If using PuTTY or WinSCP, set "Seconds between keepalives" to 60.

    ssh -o ServerAliveInterval=60 root@12.34.56.78
  9. Customize SSH via child templates

    master

    If a setting is not exposed via a variable, you can create a child Jinja2 template to override the default behavior.

    1. Designate child templates

    In your group_vars/all/main.yml, point the sshd_config and ssh_config variables to your new template paths (relative to the playbook directory):

    # group_vars/all/main.yml
    sshd_config: "{{ playbook_dir }}/templates/sshd_config.j2"
    ssh_config: "{{ playbook_dir }}/templates/ssh_config.j2"

    2. Create the child template

    Your template must {% extends %} the base role template and use {% block %} to inject content. Use {{ super() }} to include the original content from the base template.

    Example: Adding SFTP settings to sshd_config

    {% extends 'roles/sshd/templates/sshd_config.j2' %}
    
    {% block main %}
    {{ super() }}
    Match Group sftponly
    AllowAgentForwarding no
    ChrootDirectory /home/%u
    ForceCommand internal-sftp
    PermitRootLogin no
    {%- endblock %}

    Example: Adding host-specific options to the start of ssh_config

    {% extends 'roles/sshd/templates/ssh_config.j2' %}
    
    {% block main %}
    # Host-specific configuration
    Host example.com example2.com
    	Port 2222
    	ForwardAgent yes
    
    # Global defaults for all Hosts
    {{ super() }}
    {%- endblock %}
  10. Configure Fail2ban global settings

    master

    Use the following variables to configure the global behavior of Fail2ban, such as logging, banning duration, and default actions.

    VariableDescriptionDefault
    fail2ban_loglevelLog level (CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG)INFO
    fail2ban_logtargetDestination for log outputs (SYSLOG, STDERR, STDOUT, or a file path)/var/log/fail2ban.log
    fail2ban_socketPath where the socket should be created/var/run/fail2ban/fail2ban.sock
    fail2ban_ignoreipIP address, CIDR mark, or DNS host to ignore127.0.0.1/8
    fail2ban_bantimeBan duration in seconds600
    fail2ban_maxretryNumber of failed attempts before a ban6
    fail2ban_backendMethod to detect file changes (gamin, polling, auto)polling
    fail2ban_destemailEmail address for reportsroot@localhost
    fail2ban_banactionHow the ban is applied (e.g., iptables, iptables-new, iptables-multiport)iptables-multiport
    fail2ban_mtaMail transfer agent to use (sendmail or mail)sendmail
    fail2ban_protocolDefault protocol (tcp or udp)tcp
    fail2ban_chainThe iptables chain where JUMPs are addedINPUT
    fail2ban_actionThe default ban action (action_, action_mw, action_mwl)action_
  11. Use the `vars` Ansible callback plugin

    master

    The vars callback plugin is a Trellis-specific Ansible plugin designed to modify play and host variables during execution. It performs two primary functions:

    1. Variable Wrapping: It identifies variables that match patterns defined in a raw_vars list and wraps them using wrap_var. This is used to handle specific raw variable processing.
    2. CLI Option Injection: It automatically injects CLI-related variables into the host context, allowing playbooks to access the connection parameters and flags used during the Ansible run.

    To use this, ensure the vars callback plugin is enabled in your ansible.cfg.