Closed geerlingguy closed 1 year ago
Well this shouldn't be too hard, actually...
---
- hosts: mm
become: true
gather_facts: false
vars:
# API token should be created with cache_purge permissions.
cloudflare_purge_zone: [get from cloudflare dashboard]
cloudflare_purge_token: [create an API token with cache_purge permissions]
cloudflare_purge_body:
files:
- https://www.jeffgeerling.com
- https://www.jeffgeerling.com/blog
- https://www.jeffgeerling.com/blog.xml
- [other files to be purged here]
tasks:
- name: Empty out the nginx cache dir.
shell: rm -f /var/cache/nginx/*
- name: Restart nginx.
ansible.builtin.service:
name: nginx
state: restarted
- name: Purge CloudFlare caches
uri:
body: '{{ cloudflare_purge_body | to_json }}'
url: "https://api.cloudflare.com/client/v4/zones/{{ cloudflare_purge_zone }}/purge_cache"
method: DELETE
body_format: json
headers:
Content-Type: "application/json"
Authorization: "Bearer {{ cloudflare_purge_token }}"
I created an API token with the Zone.Cache Purge permission in Cloudflare:
You have to set an API start and end date, so I set mine to be valid until the end of the year. I'll have to update the token next year.
I got the API Zone ID from Cloudflare's UI. I logged in, went to Websites, clicked on my jeffgeerling.com website, then copied out the Zone ID from the right hand column:
(It's greyed out here, for obvious reasons.)
I would like to be able to run a quick Ansible play that both purges the proxy cache in Nginx, and purges relevant URLs in Cloudflare.
Right now I do that manually when I want to make sure a new article or changes post quickly to end users (otherwise it can be anywhere from 10-30 minutes before changes on the site are reflected in the real world).
The playbook should:
/var/cache/nginx
nginx