Automated LetsEncrypt SSL installation with Ansible

simple SSL

Need a simple SSL which will work in all browsers?

LetsEncrypt SSL is your choice! Even more, you won't even need to get into multi-step slow domain ownership validation which includes waiting for emails and waiting for Certificate Authority (CA) to react to your requests.
Why and when would one need an SSL certificate in the first place? Well, if you got a WordPress site (replace this with your favorite CMS/framework) that has any kind of authorization form, then you need an SSL because you probably don't want usernames and passwords flying over the Internet in plain text.
Besides we should also make a remark, that for applications like online shops or pretty much any apps working with various sorts of confidential information, you might need a non-free SSL. There is a number of reasons for that. The main reasons are the following:
- those certificates are displayed as more secure in clients browsers
- you get much better warranty payments for a (very unlikely) case of SSL hack
The ones who probably benefit the most from LetsEncrypt fast validation are system administrators of various levels. Above all Let's Encrypt allows using CLI certbots like the officially recommended one at certbot.eff.org for obtaining and even installing certificates on some web servers. As a result, it's hard to overcome the temptation to automate the process, making it easily repeatable for any number of sites you need.

Automated setup of LetsEncrypt SSL on your website

In this demonstration, we will use Ansible as a configuration management tool. Likewise, for automation purposes, you can use many other similar tools like Chef, Puppet, and maybe even Bash :D We prefer to use Ansible, as it's very flexible in the borders of this task and doesn't require many additional configurations compared to other alternatives.

Requirements to begin

- DNS record pointing to the IP of the server with the domains, which you will install and configure SSL for
- Correct version of Ansible - it should be 2.9. To check it you can execute the following command in your CLI:

ansible --version

- OS: Ubuntu 22.04 installed on the target server
- SSH key uploaded to the target server
- Python-minimal installed on the target server
- Nginx web-server, working from www-data folder
- Catalogue structure on the target server should be as follows:

├── conf.d
├── fastcgi.conf
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── nginx.conf
├── proxy_params
├── scgi_params
├── sites-available
    └── default
├── sites-enabled
    └── default -> /etc/nginx/sites-available/default

Important files

- Conf.d - temporary configuration files for LetsEncrypt SSL are kept here
- Sites-available - configs of available websites
- Sites-enabled - connected websites
Also you should enable these strings in nginx.conf:

##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

Configuration

- Hosts file:

[all]
letsencrypt-test ansible_ssh_host=174.138.95.167 ansible_ssh_user=root
cat host_vars/letsencrypt-test
ssl_domains:
 - ssl-demo3.itsyndicate.org
 - ssl-demo4.itsyndicate.org
le_email: [email protected]

- ssl_domains - list of domains you want SSL to be installed and configured for
- le_email - e-mail address for LetsEncrypt SSL notifications

Playbook launch

ansible-playbook -i hosts example_le_ssl_playbook.yml

You can download the role here: https://gitlab.itsyndicate.org/public-area/ansible-letsencrypt
Note that any SSL won't fully protect your website from various attacks and possible hacks. There're other numerous security approaches and standards that you should review in order to be sure, that your website and server are protected. We're talking about this topic in one of our articles.