Securing your website with an SSL certificate is essential for protecting data and building trust with visitors. Automating the deployment of SSL certificates can save time and reduce errors. In this article, we will explore how to automate SSL certificate deployment using Ansible and Let's Encrypt.

Understanding the Tools

Before diving into automation, it is important to understand the key tools involved:

  • Ansible: An open-source automation tool that manages configuration and deployment across multiple servers.
  • Let's Encrypt: A free, automated, and open certificate authority that provides SSL certificates.

Prerequisites

Ensure you have the following before starting:

  • Ansible installed on your control machine.
  • Access to the target server with SSH keys configured.
  • Domain names pointing to your server's IP address.
  • Basic knowledge of Ansible playbooks and roles.

Creating the Ansible Playbook

Start by creating an Ansible playbook that will handle the installation of Certbot, the Let's Encrypt client, and the configuration of SSL certificates.

Example playbook structure:

File: deploy_ssl.yml

```yaml

- hosts: webservers

become: yes

tasks:

- name: Install Certbot

apt:

name: certbot

state: present

- name: Obtain SSL Certificate

command: certbot certonly --webroot -w /var/www/html -d example.com --agree-tos --email [email protected] --non-interactive

- name: Configure web server to use SSL

# Additional tasks to enable SSL on your web server, e.g., Apache or Nginx

```

Automating Renewal

Let's Encrypt certificates are valid for 90 days. Automate renewal by adding a cron job that runs Certbot renewal command periodically.

Example cron task:

Run daily at midnight:

```bash 0 0 * * * /usr/bin/certbot renew --quiet --renew-hook "systemctl reload nginx" ```

Conclusion

Using Ansible with Let's Encrypt simplifies the process of deploying and maintaining SSL certificates. Automating these tasks ensures your website remains secure without manual intervention, saving time and reducing potential errors.