|
| 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