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
 4from cf_rules import Cloudflare
 5
 6dotenv.load_dotenv(".env")
 7
 8local_rule_file = "Bad IP.txt"
 9# "Bad IP.txt" must exist in your expressions folder
10remote_rule_name = "Not allowed IP"
11
12cf = Cloudflare("expressions_main")
13cf.auth_key(os.environ.get("EMAIL"), os.environ.get("KEY"))
14
15# Export all rules from the main domain
16s = cf.export_rules("example.com")
17
18# TODO Edit your rules before updating them back to Cloudflare
19
20# Update your rule for all domains
21for domain in cf.domains:
22    s = cf.update_rule(domain.name, local_rule_file, remote_rule_name)
23    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.