如何通过Ansible playbook安装Certbot

155次阅读
没有评论

问题描述

想知道如何通过Ansible playbook安装Certbot。他已经在Bash脚本中找到了安装Certbot的方法,但想知道是否有更轻量级的Ansible任务来完成这个任务。

解决方案

请注意以下操作注意版本差异及修改前做好备份。

方案1

以下是将Bash安装脚本直接转换为Ansible任务的方法:

- apt_repository:
    repo: 'ppa:certbot/certbot'
- apt:
    name: "{{ item }}"
    update_cache: yes
  with_items:
    - nginx
    - python-certbox-nginx

在上面的示例中,我们使用apt_repository模块添加了Certbot的存储库,并使用apt模块安装了Nginx和python-certbox-nginx。这将直接在Ansible中安装Certbot。
请注意,根据用户的评论,如果您使用的是Ubuntu 18.04,添加存储库是不必要的。您可以直接使用become: yesapt模块来安装Certbot,如下所示:

- apt:
    name: python-certbot-nginx
    state: present
    become: yes

方案2

如果您使用的是Ubuntu 16.04,可以尝试以下Ansible任务:

- name: Install Certbot.
  package: "name=letsencrypt state=present"

您还可以使用Github上的一个角色来安装Certbot,具体方法如下:

- name: Include Certbot role
  include_role:
    name: geerlingguy.certbot

请注意,根据用户的评论,方案2中的任务可能不会直接安装Certbot。您可能需要根据您的具体情况进行调整。

方案3

以下是另一种安装Certbot的方法:

- name: Add certbot repository
  apt_repository:
    repo: 'ppa:certbot/certbot'
- name: Install Certbot's Apache package
  apt:
    name: python-certbot-apache
    state: present

方案4

以下是在Debian Stretch上安装Certbot的Ansible任务示例:

- name: Add "stretch-backports" to APT sources (required for Certbot)
  become: yes
  apt_repository:
    repo: deb http://httpredir.debian.org/debian stretch-backports main
    state: present
    update_cache: yes
- name: "Check LetsEncrypt/Certbot is installed"
  become: yes
  apt:
    name:
      - certbot
      - python-certbot-apache # https://github.com/certbot/certbot/issues/3854
    state: present
    default_release: stretch-backports
    cache_valid_time: 3600
    update_cache: yes

请注意,根据用户的评论,方案4中的任务可能需要根据您的具体情况进行调整。
以上是几种在Ansible playbook中安装Certbot的方法,您可以根据您的需求选择适合您的方法。

正文完