diff --git a/.travis.yml b/.travis.yml index be4167c..47ab110 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,9 +4,12 @@ services: docker env: - distro: centos7 - distro: centos6 + playbook: test-source-install.yml - distro: ubuntu1604 - distro: ubuntu1404 - - distro: ubuntu1204 + playbook: test-source-install.yml + - distro: debian8 + playbook: test-source-install.yml script: # Download test shim. diff --git a/README.md b/README.md index 101c6f3..e05050e 100644 --- a/README.md +++ b/README.md @@ -2,31 +2,40 @@ [![Build Status](https://travis-ci.org/geerlingguy/ansible-role-certbot.svg?branch=master)](https://travis-ci.org/geerlingguy/ansible-role-certbot) -Installs Certbot (for Let's Encrypt) for RHEL/CentOS or Debian/Ubuntu. +Installs and configures Certbot (for Let's Encrypt). ## Requirements -Certbot requires Git to be installed. You can install Git using the `geerlingguy.git` role. +If one wants to install Certbot from upstream Git repository instead of distribution's package management, this role requires Git to be installed. You can install Git using the `geerlingguy.git` role. ## Role Variables +The variable `certbot_install_from_source` controls whether to install Certbot from Git or package management. The latter is the default, so the variable defaults to `no`. + + certbot_auto_renew: true + certbot_auto_renew_user: "{{ ansible_user }}" + certbot_auto_renew_hour: 3 + certbot_auto_renew_minute: 30 + +By default, this role configures a cron job to run under the provided user account at the given hour and minute, every day. The defaults run `certbot renew` (or `certbot-auto renew`) via cron every day at 03:30:00 by the user you use in your Ansible playbook. It's preferred that you set a custom user/hour/minute so the renewal is during a low-traffic period and done by a non-root user account. + +### Variables Relavant for Source Installation from Git + +Instead of installing Certbot from distribution's package management, installing from Git repository is also an option. This might be useful in several cases, but especially when older LTS distributions don't ship Certbot yet. These include CentOS < 7, Ubuntu < 16.10 and Debian < 8. Debian 8 includes Certbot package when packports repository is enabled. + +In case source installation from Git is intended, the following variables are relevant: + + certbot_install_from_source: yes certbot_repo: https://github.com/certbot/certbot.git certbot_version: master certbot_keep_updated: yes -Certbot code repository options. This role clones the agent from the configured repo, then makes the `certbot-auto` script executable. +Certbot Git repository options. This clones the configured `certbot_repo`, respecting the `certbot_version` setting. If `certbot_keep_updated` is set to `yes`, the repository is updated every time this role runs. certbot_dir: /opt/certbot The directory inside which Certbot will be cloned. - certbot_auto_renew: true - certbot_auto_renew_user: "{{ ansible_user }}" - certbot_auto_renew_hour: 3 - certbot_auto_renew_minute: 30 - -By default, this role configures a cron job to run under the provided user account at the given hour and minute, every day. The defaults run `certbot-auto renew` via cron every day at 03:30:00 by the user you use in your Ansible playbook. It's preferred that you set a custom user/hour/minute so the renewal is during a low-traffic period and done by a non-root user account. - ## Dependencies None. @@ -34,20 +43,20 @@ None. ## Example Playbook - hosts: servers - + vars: certbot_auto_renew_user: your_username_here certbot_auto_renew_minute: 20 certbot_auto_renew_hour: 5 - + roles: - geerlingguy.certbot -After installation, you can create certificates using the `certbot-auto` script, which by default is installed inside the configured `certbot_dir`, so by default, `/opt/certbot/certbot-auto`. Here are some example commands to configure certificates with Certbot: +After installation, you can create certificates using the `certbot` (or `certbot-auto`) script, which by default is installed inside the configured `certbot_dir` (when using Git). Here are some example commands to configure certificates with Certbot: # Automatically add certs for all Apache virtualhosts (use with caution!). /opt/certbot/certbot-auto --apache - + # Generate certs, but don't modify Apache configuration (safer). /opt/certbot/certbot-auto --apache certonly diff --git a/defaults/main.yml b/defaults/main.yml index 793362c..cf4bb2b 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,10 +1,11 @@ --- # Where to get Certbot. +certbot_install_from_source: no certbot_repo: https://github.com/certbot/certbot.git certbot_version: master certbot_keep_updated: yes -# Where to put Certbot. +# Where to put Certbot when cloning from Git. certbot_dir: /opt/certbot # How to keep Certbot certs up to date. diff --git a/meta/main.yml b/meta/main.yml index 2a9422b..aa00a88 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -3,21 +3,24 @@ dependencies: [] galaxy_info: author: geerlingguy - description: "Certbot (for Let's Encrypt) for RHEL/CentOS and Debian/Ubuntu." + description: "Installs and configures Certbot (for Let's Encrypt)." company: "Midwestern Mac, LLC" license: "license (BSD, MIT)" - min_ansible_version: 1.8 + min_ansible_version: 2.0 platforms: - name: EL versions: - 6 - 7 + - name: Fedora + versions: + - all - name: Ubuntu versions: - - all + - all - name: Debian versions: - - all + - all galaxy_tags: - networking - system diff --git a/tasks/include-vars.yml b/tasks/include-vars.yml new file mode 100644 index 0000000..0a70e50 --- /dev/null +++ b/tasks/include-vars.yml @@ -0,0 +1,8 @@ +--- +- name: Load a variable file based on the OS type, or a default if not found. + include_vars: "{{ item }}" + with_first_found: + - "{{ ansible_distribution }}-{{ ansible_distribution_version }}.yml" + - "{{ ansible_distribution }}.yml" + - "{{ ansible_os_family }}.yml" + - "default.yml" diff --git a/tasks/install-from-source.yml b/tasks/install-from-source.yml new file mode 100644 index 0000000..7d97f9b --- /dev/null +++ b/tasks/install-from-source.yml @@ -0,0 +1,17 @@ +--- +- name: Clone Certbot into configured directory. + git: + repo: "{{ certbot_repo }}" + dest: "{{ certbot_dir }}" + version: "{{ certbot_version }}" + update: "{{ certbot_keep_updated }}" + force: yes + +- name: Set Certbot script variable. + set_fact: + certbot_script: "{{ certbot_dir }}/certbot-auto" + +- name: Ensure certbot-auto is executable. + file: + path: "{{ certbot_script }}" + mode: 0755 diff --git a/tasks/install-with-package.yml b/tasks/install-with-package.yml new file mode 100644 index 0000000..10490ff --- /dev/null +++ b/tasks/install-with-package.yml @@ -0,0 +1,7 @@ +--- +- name: Install Certbot. + package: "name={{ certbot_package }} state=present" + +- name: Set Certbot script variable. + set_fact: + certbot_script: "{{ certbot_package }}" diff --git a/tasks/main.yml b/tasks/main.yml index 5e17fb6..5324ff9 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,22 +1,11 @@ --- -- name: Clone Certbot into configured directory. - git: - repo: "{{ certbot_repo }}" - dest: "{{ certbot_dir }}" - version: "{{ certbot_version }}" - update: "{{ certbot_keep_updated }}" - force: yes +- include: include-vars.yml -- name: Ensure certbot-auto is executable. - file: - path: "{{ certbot_dir }}/certbot-auto" - mode: 0755 +- include: install-with-package.yml + when: not certbot_install_from_source -- name: Add cron job for 'certbot-auto renew' (if configured). - cron: - name: Certbot automatic renewal. - job: "{{ certbot_dir }}/certbot-auto renew --quiet --no-self-upgrade" - minute: "{{ certbot_auto_renew_minute }}" - hour: "{{ certbot_auto_renew_hour }}" - user: "{{ certbot_auto_renew_user }}" +- include: install-from-source.yml + when: certbot_install_from_source + +- include: renew-cron.yml when: certbot_auto_renew diff --git a/tasks/renew-cron.yml b/tasks/renew-cron.yml new file mode 100644 index 0000000..7678a7c --- /dev/null +++ b/tasks/renew-cron.yml @@ -0,0 +1,8 @@ +--- +- name: Add cron job for certbot renewal (if configured). + cron: + name: Certbot automatic renewal. + job: "{{ certbot_script }} renew --quiet --no-self-upgrade" + minute: "{{ certbot_auto_renew_minute }}" + hour: "{{ certbot_auto_renew_hour }}" + user: "{{ certbot_auto_renew_user }}" diff --git a/tests/test-source-install.yml b/tests/test-source-install.yml new file mode 100644 index 0000000..d2a3d73 --- /dev/null +++ b/tests/test-source-install.yml @@ -0,0 +1,23 @@ +--- +- hosts: all + + vars: + certbot_install_from_source: yes + + pre_tasks: + - name: Update apt cache. + apt: update_cache=yes cache_valid_time=600 + when: ansible_os_family == 'Debian' + changed_when: false + + - name: Install cron (RedHat). + yum: name=cronie state=present + when: ansible_os_family == 'RedHat' + + - name: Install cron (Debian). + apt: name=cron state=present + when: ansible_os_family == 'Debian' + + roles: + - geerlingguy.git + - role_under_test diff --git a/tests/test.yml b/tests/test.yml index cf90f58..217fc45 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -16,5 +16,4 @@ when: ansible_os_family == 'Debian' roles: - - geerlingguy.git - role_under_test diff --git a/vars/Ubuntu-16.04.yml b/vars/Ubuntu-16.04.yml new file mode 100644 index 0000000..90e9138 --- /dev/null +++ b/vars/Ubuntu-16.04.yml @@ -0,0 +1 @@ +certbot_package: letsencrypt diff --git a/vars/default.yml b/vars/default.yml new file mode 100644 index 0000000..d88f2dc --- /dev/null +++ b/vars/default.yml @@ -0,0 +1,2 @@ +--- +certbot_package: certbot