Jim Rigsbee
2019-07-22 0d0d0e99169d9285f46b3fd5fc425843c42fff89
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
---
## Request Let's Encrypt Certificates for a host
- name: Set Certbot directory
  set_fact:
    _certbot_dir: "{{ _certbot_remote_dir }}/certbot"
 
- name: Verify if AWS Credentials exist on the host
  when: _certbot_dns_provider is match('route53')
  stat:
    path: "/home/{{ ansible_user }}/.aws/credentials"
  register: aws_credentials_result
 
- name: Fail if AWS Credentials are not on the host
  fail:
    msg: AWS Credentials are required when requesting certificates for a wildcard domain
  when:
  - _certbot_dns_provider is match('route53')
  - aws_credentials_result.stat.exists == False
 
- name: Set _certbot_wildcard_certs fact
  set_fact:
    _certbot_wildcard_certs: "{{ (_certbot_wildcard_domain|length|int>0)|ternary('true','false') }}"
 
- name: Test if Let's Encrypt Certificates are already there
  stat:
    path: "{{ _certbot_install_dir }}/fullchain.pem"
  register: cacert
 
- name: No Certificates on host or _certbot_force_issue=true -> set up Let's Encrypt Certificates
  when:
    - cacert.stat.exists|bool == false or _certbot_force_issue|bool
  block:
    - name: Install certbot packages
      become: True
      yum:
        name:
        - certbot
        - "python2-certbot-dns-{{ _certbot_dns_provider }}"
        state: latest
 
    - name: Check if cached certificate archive exists
      stat:
        path: "{{ _certbot_cache_archive_file }}"
      delegate_to: localhost
      register: cache_archive_file
 
    - name: Restore entire certificate archive
      when:
      - _certbot_use_cache|bool
      - cache_archive_file.stat.exists|bool
      - not _certbot_force_issue|bool
      block:
      - name: Upload certificate archive
        unarchive:
          src: "{{ _certbot_cache_archive_file }}"
          dest: "{{ _certbot_remote_dir }}"
          owner: "{{ _certbot_install_dir_owner }}"
          keep_newer: yes
      - name: Set _certbot_setup_complete=true
        set_fact:
          _certbot_setup_complete: true
 
    - name: Ensure Certbot Directories are present
      file:
        name: "{{ item }}"
        state: directory
        owner: "{{ _certbot_remote_dir_owner }}"
        mode: 0775
      loop:
      - "{{ _certbot_dir }}"
      - "{{ _certbot_dir }}/config"
      - "{{ _certbot_dir }}/work"
      - "{{ _certbot_dir }}/logs"
      - "{{ _certbot_dir }}/renewal-hooks"
      - "{{ _certbot_dir }}/renewal-hooks/deploy"
 
    - name: Request Certificates from Let's Encrypt (force or no cache)
      when:
      - _certbot_force_issue|bool or not _certbot_setup_complete|bool
      block:
      # Get Intermediary CA Certificate.
      # This is also used in the SSO configuration!
      - name: Get Let's Encrypt Intermediary CA Certificate
        get_url:
          url: https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem.txt
          dest: "{{ _certbot_dir }}/lets-encrypt-x3-cross-signed.pem"
      - name: Print Shell Command
        debug:
          msg: >-
            About to request certificates using the following command:
            certbot certonly -n --agree-tos --email {{ _certbot_le_email }}
            -d {{ _certbot_domain }}
            {{ (_certbot_wildcard_domain|length>0)|ternary('-d','')}} {{ (_certbot_wildcard_domain|length>0)|ternary(_certbot_wildcard_domain,'')}}
            {{ (_certbot_production|bool)|ternary('','--test-cert') }}
            {{ _certbot_additional_args|d(_certbot_args)|d('') }}
            {{ (_certbot_wildcard_certs|bool)|ternary('--dns-'+_certbot_dns_provider, '') }}
            --config-dir={{ _certbot_dir }}/config
            --work-dir={{ _certbot_dir }}/work
            --logs-dir={{ _certbot_dir }}/logs
 
      - name: Request API and Wildcard Certificates
        become: False
        shell: >-
          certbot certonly -n --agree-tos --email {{ _certbot_le_email }}
          -d {{ _certbot_domain }}
          {{ (_certbot_wildcard_domain|length>0)|ternary('-d','')}} {{ (_certbot_wildcard_domain|length>0)|ternary(_certbot_wildcard_domain,'')}}
          {{ (_certbot_production|bool)|ternary('','--test-cert') }}
          {{ _certbot_additional_args|d(_certbot_args)|d('') }}
          {{ (_certbot_wildcard_certs|bool)|ternary('--dns-'+_certbot_dns_provider, '') }}
          --config-dir={{ _certbot_dir }}/config
          --work-dir={{ _certbot_dir }}/work
          --logs-dir={{ _certbot_dir }}/logs
        retries: 5
        delay: 30
        register: r_request_le
        until: r_request_le is succeeded
 
      - name: Save certificates to cache
        when:
        - _certbot_use_cache|bool
        - _certbot_cache_archive_file is defined
        - _certbot_cache_archive_file|trim != ""
        block:
        - name: Create archive of certbot directory for cache
          archive:
            path: "{{ _certbot_dir }}"
            dest: "/tmp/certbot.tgz"
        - name: Save certbot archive to cache
          fetch:
            src: "/tmp/certbot.tgz"
            dest: "{{ _certbot_cache_archive_file }}"
            flat: yes
        - name: Remove archive from server
          file:
            name: "/tmp/certbot.tgz"
            state: absent
 
- name: Install the certificates into {{ _certbot_install_dir }}
  block:
  - name: Ensure {{ _certbot_install_dir }} exists
    file:
      path: "{{ _certbot_install_dir }}"
      state: directory
      owner: "{{ _certbot_install_dir_owner }}"
      mode: 0775
  - name: Install certificates
    copy:
      src: "{{ _certbot_dir }}/config/live/{{ _certbot_domain }}/{{ item }}"
      dest: "{{ _certbot_install_dir }}/{{ item }}"
      remote_src: yes
    loop:
    - "cert.pem"
    - "fullchain.pem"
    - "chain.pem"
    - "privkey.pem"
 
- name: Install Automatic renewals of Certificates
  when:
  - _certbot_renew_automatically|bool
  block:
  - name: Install crontab to renew certificates when they expire
    become: False
    cron:
      name: "{{ _certbot_cron_job_name }}"
      special_time: daily
      job: "certbot renew {{ _certbot_additional_args|d(_certbot_args)|d('') }} --config-dir={{ _certbot_dir }}/config --work-dir={{ _certbot_dir }}/work --logs-dir={{ _certbot_dir }}/logs --quiet > /dev/null"