Update rules script
This script is a lightweight example of how to update the same firewall rule on all your domains.
1import os
2
3import dotenv
4
5from cf_rules import Cloudflare
6
7dotenv.load_dotenv(".env")
8
9local_rule_file = "Bad IP.txt"
10# "Bad IP.txt" must exist in your expressions folder
11remote_rule_name = "Not allowed IP"
12
13cf = Cloudflare("expressions_main")
14cf.auth_key(os.environ.get("EMAIL"), os.environ.get("KEY"))
15
16# Export all rules from the main domain
17s = cf.export_rules("example.com")
18
19# TODO Edit your rules before updating them back to Cloudflare
20
21# Update your rule for all domains
22for domain in cf.domains:
23 s = cf.update_rule(domain.name, local_rule_file, remote_rule_name)
24 print(s)
Note
If you export the rules from a domain, you will not be able to update them again if the text file hasn’t changed. This is a Cloudflare limitation.