znerol.certhub
Ansible角色:Certhub
在由systemd
控制的Linux服务器上安装certhub和git-gau
。
需求
- Git
- OpenSSL
- 如果目标机器作为SSL/TLS服务器运行,则需要Rsnic。
- 当目标机器作为控制器时,需要
certbot
、dehydrated
或lego
之一。
角色变量
此角色包含多个task
文件。main
任务文件仅导入其中一些:
- certhub-system-setup.yml:创建
certhub
用户和组,配置目录并准备主目录。 - certhub-software-setup.yml:获取并安装
certhub
和git-gau
。 - certhub-git-setup.yml:配置git的
user.name
和user.email
。 - certhub-repo-setup.yml:初始化本地
certs
git仓库。
以下任务文件包含其他配置步骤:
- certbot-noroot-config.yml:通过放置适当的
cli.ini
文件,简化以非特权用户certhub
运行certbot
。
以下任务文件包含设置证书获取和部署的步骤。
- systemd单元的环境:包含所有certhub systemd单元所需的环境文件路径的变量。
- repo-push-units.yml:设置负责在证书变化时将证书仓库推送到SSL/TLS服务器的
systemd
单元。 - certbot-run-units.yml:设置负责签发/续订证书并监控其过期的
systemd
单元(控制器)。 - lego-run-units.yml:设置负责签发/续订证书并监控其过期的
systemd
单元(控制器)。 - cert-export-units.yml:设置在SSL/TLS服务器上部署证书、重新加载相关服务和/或将证书发送到指定目的地的
systemd
单元。
certhub-system-setup.yml的变量
可用变量如下所列,并附有默认值(见defaults/main.yml
):
certhub_user_group: certhub
certhub_user_name: certhub
certhub_user_shell: /usr/bin/git-shell
certhub_home_dir_path: /var/lib/certhub
certhub_home_dir_mode: 0755
certhub_config_dir_path: /etc/certhub
certhub_config_dir_owner: root
certhub_config_dir_group: root
certhub_config_dir_mode: 0755
certhub_status_dir_path: "{{ certhub_home_dir_path }}/status"
certhub_status_dir_owner: "{{ certhub_user_name }}"
certhub_status_dir_group: "{{ certhub_user_group }}"
certhub_status_dir_mode: 0755
certhub_certs_dir_path: "{{ certhub_home_dir_path }}/certs"
certhub_certs_dir_owner: "{{ certhub_user_name }}"
certhub_certs_dir_group: "{{ certhub_user_group }}"
certhub_certs_dir_mode: 0755
certhub_private_dir_path: "{{ certhub_home_dir_path }}/private"
certhub_private_dir_owner: "{{ certhub_user_name }}"
certhub_private_dir_group: "{{ certhub_user_group }}"
certhub_private_dir_mode: 0700
certhub_repo_path: "{{ certhub_home_dir_path}}/certs.git"
certhub-software-setup.yml的变量
可用变量如下所列,并附有默认值(见defaults/main.yml
):
certhub_certhub_version: v1.0.0
certhub_certhub_checksum: "{{ certhub_certhub_releases[certhub_certhub_version]['checksum'] }}"
certhub_certhub_url: "{{ certhub_certhub_releases[certhub_certhub_version]['url'] }}"
certhub_certhub_prefix: /usr/local
certhub_certhub_archive_path: "{{ certhub_private_dir_path }}/certhub-dist-{{ certhub_certhub_version }}.tar.gz"
certhub_gitgau_version: v1.3.0
certhub_gitgau_checksum: "{{ certhub_gitgau_releases[certhub_gitgau_version]['checksum'] }}"
certhub_gitgau_url: "{{ certhub_gitgau_releases[certhub_gitgau_version]['url'] }}"
certhub_gitgau_prefix: /usr/local
certhub_gitgau_archive_path: "{{ certhub_private_dir_path }}/git-gau-dist-{{ certhub_gitgau_version }}.tar.gz"
变量certhub_certhub_releases
和certhub_gitgau_releases
包含所有公共发布的URL和校验和。设置certhub_certhub_version
和certhub_gitgau_version
以指定特定版本。
certhub-git-setup.yml的变量
可用变量如下所列,并附有默认值(见defaults/main.yml
):
certhub_git_user_name: "{{ certhub_user_name }}"
certhub_git_user_email: "{{ certhub_user_name }}@{{ ansible_fqdn }}"
certhub-repo-setup.yml的变量
可用变量如下所列,并附有默认值(见defaults/main.yml
):
certhub_repo_init_path: "{{ certhub_repo_path }}"
certhub_repo_init_message: Init
当certhub_repo_init_path
为空字符串时,将跳过git仓库的设置。
certbot-noroot-config.yml的变量
可用变量如下所列,并附有默认值(见defaults/main.yml
):
certhub_certbot_cli_ini_path: "{{ certhub_home_dir_path }}/.config/letsencrypt/cli.ini"
certhub_certbot_cli_ini_content: ""
certhub_certbot_work_dir: "{{ certhub_private_dir_path }}/certbot/work"
certhub_certbot_logs_dir: "{{ certhub_private_dir_path }}/certbot/logs"
certhub_certbot_config_dir: "{{ certhub_private_dir_path }}/certbot/config"
变量certhub_certbot_cli_ini_content
可用于提供额外的certbot配置。例如,staging=true
以在评估期间选择临时环境。
systemd单元的环境
可用变量如下所列,并附有默认值(见defaults/main.yml
):
# 所有实例和certhub服务共享的可选环境文件。
certhub_env_path: "{{ certhub_config_dir_path }}/env"
# 所有certhub服务共享的每个实例的可选环境文件。
certhub_cert_env_path: "{{ certhub_config_dir_path}}/{{ certhub_cert_slug }}.env"
# 所有certhub服务实例共享的每个服务的可选环境文件。
certhub_unit_env_path: "{{ certhub_config_dir_path}}/{{ certhub_unit_prefix }}.env"
# 每个实例和每个服务的可选环境文件。
certhub_cert_unit_env_path: "{{ certhub_config_dir_path}}/{{ certhub_cert_slug }}.{{ certhub_unit_prefix }}.env"
certhub_env_owner: root
certhub_env_group: "{{ certhub_user_group }}"
certhub_env_mode: 0640
在自定义copy
或template
任务中使用这些变量以填充certhub服务的env
文件。注意:certhub_unit_prefix
没有默认值。该变量需要设置为certhub服务单元前缀之一。以下值当前有效:
- certhub-cert-expiry
- certhub-cert-export
- certhub-cert-reload
- certhub-cert-send
- certhub-certbot-run
- certhub-dehydrated-run
- certhub-lego-run
repo-push-units.yml的变量
可用变量如下所列,并附有默认值(见defaults/main.yml
):
certhub_repo_push_user: "{{ certhub_user_name }}"
certhub_repo_push_host: "{{ inventory_hostname }}"
certhub_repo_push_path: "{{ certhub_repo_path }}"
certhub_repo_push_remote: "{{ '{:s}@{:s}:{:s}'.format(certhub_repo_push_user, certhub_repo_push_host, certhub_repo_push_path) }}"
certhub_repo_push_path_unit: "{{ lookup('pipe', 'systemd-escape [email protected] ' + certhub_repo_push_remote | quote) }}"
指定certhub_repo_push_remote
以完全覆盖远程位置。
certbot-run-units.yml的变量
可用变量如下所列,并附有默认值(见defaults/main.yml
):
certhub_cert_slug: "{{ inventory_hostname }}"
certhub_cert_expiry_timer_unit: "certhub-cert-expiry@{{ certhub_cert_slug }}.timer"
certhub_certbot_run_path_unit: "certhub-certbot-run@{{ certhub_cert_slug }}.path"
设置certhub_cert_slug
以指定证书实例。
lego-run-units.yml的变量
可用变量如下所列,并附有默认值(见defaults/main.yml
):
certhub_cert_slug: "{{ inventory_hostname }}"
certhub_cert_expiry_timer_unit: "certhub-cert-expiry@{{ certhub_cert_slug }}.timer"
certhub_lego_run_path_unit: "certhub-lego-run@{{ certhub_cert_slug }}.path"
设置certhub_cert_slug
以指定证书实例。
cert-export-units.yml的变量
可用变量如下所列,并附有默认值(见defaults/main.yml
):
certhub_cert_slug: "{{ inventory_hostname }}"
certhub_cert_export_path_unit: "certhub-cert-export@{{ certhub_cert_slug }}.path"
certhub_cert_services: []
certhub_cert_reload_config_path: "{{ certhub_config_dir_path }}/{{ certhub_cert_slug }}.services-reload.txt"
certhub_cert_reload_path_unit: "certhub-cert-reload@{{ certhub_cert_slug }}.path"
certhub_cert_destinations: []
certhub_cert_send_config_path: "{{ certhub_config_dir_path }}/{{ certhub_cert_slug }}.destinations-send.txt"
certhub_cert_send_path_unit: "certhub-cert-send@{{ certhub_cert_slug }}.path"
设置certhub_cert_slug
以指定证书实例。可以使用certhub_cert_services
指定在证书实例更改时应重新加载的systemd服务单元的列表。可以在certhub_cert_destinations
中指定证书应发送到的目的地列表。
依赖
无。
示例剧本
- name: Certhub控制器设置
hosts: certhub-controller
tasks:
- name: 确保Certhub依赖项存在
package:
name:
- certbot
- git
- lexicon
- openssl
state: present
- name: 确保Certhub已安装
import_role:
name: znerol.certhub
- name: TLS服务器设置
hosts: tls-server
tasks:
- name: 确保Certhub依赖项存在
package:
name:
- git
- openssl
state: present
- name: 确保Certhub已安装
import_role:
name: znerol.certhub
- name: 设置仓库推送单元
delegate_to: certhub-controller的名称
import_role:
name: znerol.certhub
tasks_from: repo-push-units.yml
- name: TLS证书配置
hosts: tls-server
tasks:
- vars:
certhub_cert_services:
- nginx
import_role:
name: znerol.certhub
tasks_from: cert-export-units.yml
- name: 设置Certbot运行单元
delegate_to: certhub-controller的名称
import_role:
name: znerol.certhub
tasks_from: certbot-run-units.yml
- name: 安装Certbot CSR
delegate_to: certhub-controller的名称
become: yes
become_user: root
copy:
dest: "{{ certhub_cert_csr_path }}"
owner: root
group: root
mode: 0644
content: |
-----BEGIN CERTIFICATE REQUEST-----
MIH6MIGhAgEAMBYxFDASBgNVBAMMC2V4YW1wbGUuY29tMFkwEwYHKoZIzj0CAQYI
KoZIzj0DAQcDQgAE1XubF1Uc4T595zSzikHAJTkXRlV5Fn5MhVUhr/18bm++5z2j
759NpRh/gbEieCT4sKZ0fUcPWBrLp5zf0CFaYqApMCcGCSqGSIb3DQEJDjEaMBgw
FgYDVR0RBA8wDYILZXhhbXBsZS5jb20wCgYIKoZIzj0EAwIDSAAwRQIhANwIuwCc
45XooSB4bleXURLDGuChxcdFYYSHnnQjArizAiBYefMa83Kk1AJUIArnJ0Rk162D
Fw3tPbiEVAmmvl0/5A==
-----END CERTIFICATE REQUEST-----
- name: 安装Certbot配置
delegate_to: certhub-controller的名称
become: yes
become_user: root
copy:
dest: "{{ certhub_certbot_config_path }}"
owner: root
group: root
mode: 0644
content: |
staging=true
agree-tos=true
register-unsafely-without-email=true
manual-public-ip-logging-ok=true
preferred-challenges=dns
manual=true
manual-auth-hook=/usr/local/lib/certhub/certbot-hooks/hook-lexicon-auth
manual-cleanup-hook=/usr/local/lib/certhub/certbot-hooks/hook-lexicon-cleanup
- name: 配置Certbot单元环境
delegate_to: certhub-controller的名称
become: yes
become_user: root
vars:
certhub_unit_prefix: certhub-certbot-run
copy:
dest: "{{ certhub_cert_unit_env_path }}"
owner: "{{ certhub_env_owner }}"
group: "{{ certhub_env_group }}"
mode: "{{ certhub_env_mode }}"
content: |
CERTHUB_LEXICON_PROVIDER=cloudflare
LEXICON_CLOUDFLARE_USERNAME="[email protected]"
LEXICON_CLOUDFLARE_TOKEN="cloudflare-api-token"
许可证
BSD