Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit 89ab1ed

Browse files
committed
Nginx-Proxy: Cloudflare Tasks.
- Moved CF tasks from Common role here. - Will now create a CF page rule for domain to allow for Lets Encrypt challenge behind CDN.
1 parent 5fd7a1f commit 89ab1ed

File tree

4 files changed

+78
-47
lines changed

4 files changed

+78
-47
lines changed

roles/common/tasks/cloudflare_cache_purge.yml

Lines changed: 0 additions & 39 deletions
This file was deleted.

roles/common/tasks/main.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,3 @@
208208
name: nethogs
209209
tags: nethogs
210210
ignore_errors: yes
211-
212-
- name: Cloudflare Cache Purge Task
213-
import_tasks: "cloudflare_cache_purge.yml"
214-
when: cloudflare_enabled
215-
tags: cloudflare-cache-purge
216-
ignore_errors: yes

roles/nginx-proxy/tasks/main.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#########################################################################
2-
# Title: Cloudbox: Nginx-Proxy Role #
3-
# Author(s): l3uddz #
2+
# Title: Cloudbox: Nginx-Proxy #
3+
# Author(s): l3uddz, desimaniac #
44
# URL: https://github.com/cloudbox/cloudbox #
55
# Docker Images: jwilder/nginx-proxy #
66
# jrcs/letsencrypt-nginx-proxy-companion #
@@ -74,6 +74,12 @@
7474
force: yes
7575
when: (not proxy_conf.stat.exists) or ((proxy_conf.stat.exists) and ((proxy_conf.stat.islnk) or (proxy_conf.stat.isdir)))
7676

77+
- name: Cloudflare Tasks
78+
import_tasks: "subtasks/cloudflare.yml"
79+
when: cloudflare_enabled
80+
tags: cloudflare-tasks
81+
ignore_errors: yes
82+
7783
- name: Create and start container
7884
docker_container:
7985
name: nginx-proxy
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#########################################################################
2+
# Title: Nginx-Proxy: Cloudflare Tasks #
3+
# Author(s): desimaniac #
4+
# URL: https://github.com/cloudbox/cloudbox #
5+
# -- #
6+
# Part of the Cloudbox project: https://cloudbox.works #
7+
#########################################################################
8+
# GNU General Public License v3.0 #
9+
#########################################################################
10+
---
11+
# https://github.com/advertine/ansible-cloudflare-purge
12+
- name: List Cloudflare Zones
13+
uri:
14+
url: 'https://api.cloudflare.com/client/v4/zones?name={{ user.domain }}'
15+
method: 'GET'
16+
headers:
17+
Content-Type: "application/json"
18+
X-Auth-Email: '{{ cloudflare.email }}'
19+
X-Auth-Key: '{{ cloudflare.api }}'
20+
return_content: yes
21+
register: cf_zones
22+
23+
- name: Set 'cloudflare_zone' Variable
24+
set_fact:
25+
cloudflare_zone: "{{ cf_zones.json.result[0].id }}"
26+
27+
- name: Purge Cloudflare Cache for '{{ user.domain }}'
28+
uri:
29+
url: 'https://api.cloudflare.com/client/v4/zones/{{ cloudflare_zone }}/purge_cache'
30+
method: 'POST'
31+
headers:
32+
X-Auth-Email: '{{ cloudflare.email }}'
33+
X-Auth-Key: '{{ cloudflare.api }}'
34+
body_format: json
35+
body: { purge_everything: true }
36+
status_code: 200
37+
register: cf_purge
38+
39+
- name: Print Result of Cloudflare Cache Purge
40+
debug:
41+
msg: "{{ ((cf_purge.json.success is defined) and (cf_purge.json.success))
42+
| ternary('Successfully','Unsuccessfully') }}
43+
purged Cloudflare cache for '{{ user.domain }}'."
44+
45+
- name: Create Cloudflare Page Rule for Lets Encrypt
46+
uri:
47+
url: 'https://api.cloudflare.com/client/v4/zones/{{ cloudflare_zone }}/pagerules'
48+
method: POST
49+
headers:
50+
X-Auth-Email: '{{ cloudflare.email }}'
51+
X-Auth-Key: '{{ cloudflare.api }}'
52+
body_format: json
53+
body:
54+
priority: 1
55+
status: active
56+
targets:
57+
- target: url
58+
constraint:
59+
operator: matches
60+
value: "*{{ user.domain }}/.well-known/*"
61+
actions:
62+
- id: ssl
63+
value: 'off'
64+
status_code: 200,400
65+
register: cf_page_rule
66+
67+
- name: Print result of Cloudflare Page Rule Creation
68+
debug:
69+
msg: "Successfully created Let's Encrypt Cloudflare page rule for '{{ user.domain }}'."
70+
when: (cf_page_rule.json.success is defined) and (cf_page_rule.json.success)

0 commit comments

Comments
 (0)