SSL Certificate Management using Ansible and Let's Encrypt

Task Overview

This Task automates the process of obtaining, configuring, and renewing SSL certificates using Let's Encrypt with Ansible. The automation will ensure all web servers have up-to-date SSL certificates, reducing manual work and maintaining a secure environment.

Technology Stack

Task Architecture

  1. Ansible Playbook provisions and installs dependencies.
  2. Certbot is installed and used to generate SSL certificates.
  3. The certificate is deployed to Nginx or Apache.
  4. Automatic renewal is set up using a scheduled task.
  5. A monitoring mechanism is implemented to verify SSL expiration.

Implementation Steps

Step 1: Setting Up Ansible Environment

Step 2: Writing Ansible Playbook for SSL Management

Create a playbook ssl_certificate.yml to:

  1. Install required packages
  2. Obtain SSL certificate
  3. Deploy SSL to Nginx/Apache
  4. Automate renewal with cron/systemd
---
- name: SSL Certificate Management
  hosts: all
  become: yes
  tasks:
    - name: Install required packages
      apt:
        name: ["certbot", "python3-certbot-nginx"]
        state: present
        update_cache: yes
      when: ansible_os_family == "Debian"

    - name: Install required packages for RHEL
      yum:
        name: ["certbot", "python3-certbot-nginx"]
        state: present
      when: ansible_os_family == "RedHat"

    - name: Obtain SSL Certificate using Certbot
      command: certbot certonly --nginx --non-interactive --agree-tos --email admin@example.com -d example.com -d www.example.com
      args:
        creates: /etc/letsencrypt/live/example.com/fullchain.pem

    - name: Configure SSL in Nginx
      template:
        src: templates/nginx_ssl.j2
        dest: /etc/nginx/sites-available/example.com
      notify: Restart Nginx

    - name: Ensure SSL renewal via cron
      cron:
        name: "Renew Let's Encrypt Certificates"
        job: "certbot renew --quiet"
        minute: "0"
        hour: "0"

Step 3: Nginx Configuration Template

(templates/nginx_ssl.j2)

server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    location / {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Step 4: Running the Playbook

Execute the playbook to deploy SSL:

ansible-playbook -i inventory.yml ssl_certificate.yml

Step 5: Verifying SSL and Auto-Renewal

Automation & Monitoring

  1. Auto-renewal:
  2. Monitoring Expiry

Project Deliverables

Expected Outcomes

Next Steps

This project is ready for implementation and can be deployed across any cloud or on-prem infrastructure.