惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
Schneier on Security
Schneier on Security
H
Help Net Security
PCI Perspectives
PCI Perspectives
博客园 - 司徒正美
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
Last Week in AI
Last Week in AI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
LINUX DO - 最新话题
GbyAI
GbyAI
IT之家
IT之家
TaoSecurity Blog
TaoSecurity Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
J
Java Code Geeks
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
N
News and Events Feed by Topic
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
美团技术团队
T
Troy Hunt's Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Cloudbric
Cloudbric
A
About on SuperTechFans
Recorded Future
Recorded Future
Microsoft Security Blog
Microsoft Security Blog
阮一峰的网络日志
阮一峰的网络日志
H
Hacker News: Front Page
Forbes - Security
Forbes - Security
Webroot Blog
Webroot Blog
D
DataBreaches.Net
L
LangChain Blog
S
Schneier on Security
博客园_首页
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
N
News | PayPal Newsroom
Hacker News - Newest:
Hacker News - Newest: "LLM"
爱范儿
爱范儿
量子位
T
The Exploit Database - CXSecurity.com
博客园 - 【当耐特】
T
Threatpost
The Hacker News
The Hacker News
N
News and Events Feed by Topic
罗磊的独立博客
Spread Privacy
Spread Privacy
Hacker News: Ask HN
Hacker News: Ask HN

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
Automate Cloudflare DNS Failover with Ansible During Incidents
Oleksandr Kuryzhev · 2026-06-22 · via DEV Community

Oleksandr Kuryzhev

Originally published on kuryzhev.cloud


Your origin goes down at 2am — and your "automated" Cloudflare DNS failover is actually a Slack message asking someone to run a playbook manually. That someone is asleep. Their vault credentials are on a laptop that's also asleep. By the time the record gets swapped, you've lost 20 minutes of traffic and your SLO is in the floor. I've seen this exact scenario play out three times across different teams. The fix isn't complicated, but it requires understanding what Cloudflare DNS failover actually does before you write a single line of Ansible.

What Cloudflare DNS Failover Actually Does

Here's the first thing most tutorials get wrong: Cloudflare DNS failover is not automatic unless you're using their paid Load Balancer product. The $5/month-per-hostname Load Balancer gives you health checks, origin pools, and automatic record switching. What we're building here is something different — programmatic DNS record manipulation via the Cloudflare API, triggered by your monitoring stack. These are two completely different mechanisms, and conflating them is how teams end up with false confidence in their runbooks.

When you update an A record via the Cloudflare API on a proxied hostname, Cloudflare's edge reflects the change almost immediately — their internal propagation is fast. The real recovery window is determined by upstream resolvers that cached your previous record. Even with ttl: 1 (Cloudflare auto TTL), external resolvers may hold the old value for up to the TTL that was in effect when they last queried. This is why pre-staging your failover record matters enormously. If the backup A record has existed for weeks, resolvers have already cached it at TTL=1 and will pick up the swap quickly. If you create it during the incident, you're starting cold.

The Ansible approach here uses community.cloudflare collection (v1.0.0+) alongside direct ansible.builtin.uri calls to the Cloudflare REST API. Install the collection with:

ansible-galaxy collection install community.cloudflare:>=1.0.0
ansible-galaxy collection install community.dns:>=2.5.0
# Also required on the control node:
pip install "dnspython==2.4.2"
# Pin to 2.4.2 — version 2.5.0 introduced a breaking timeout regression
# that causes community.dns.lookup to hang without an explicit timeout param

One critical internal detail: Cloudflare's API always returns ttl: 1 for proxied records regardless of what value you set. Do not use the API-returned TTL as a health signal in your pre-flight checks. It will always be 1 and tells you nothing about propagation state.

How Teams Get This Wrong in Production

I've audited a lot of "DNS failover runbooks" and the same three mistakes appear almost universally.

Mistake 1: Manual trigger during incidents. The playbook exists, it works in staging, and it's documented in Confluence. But it requires a human to wake up, authenticate, find the runbook, and have working vault credentials. That human latency adds 10–25 minutes to your MTTR. A failover mechanism that depends on a person being awake and functional at 3am is not a failover mechanism — it's a recovery procedure. These are not the same thing.

Mistake 2: Creating the failover record during the incident. I stopped doing this after a particularly bad incident where we spent eight minutes troubleshooting why the new record wasn't propagating — it was because resolvers had zero TTL history for it. The standby A record should exist permanently, pointing at your standby IP, proxied, at all times. You're not hiding it. You're just not routing production traffic to it yet. When failover happens, you're changing the content of an existing record, not creating a new one. That distinction matters for propagation speed.

Mistake 3: Not pinning zone_id explicitly. The community.general.cloudflare_dns module does a zone name lookup on every run. Under normal conditions, that's fine. During a major incident when you're running multiple playbook tasks simultaneously, you can hit Cloudflare's rate limit of 1200 requests per 5 minutes per token. When that happens, the API returns Error 10000: Authentication error — which is completely misleading and gets misdiagnosed as a credentials problem every single time. Always pull zone_id from Ansible vault and pass it directly. Never let the module resolve it at runtime.

Watch out: Cloudflare occasionally returns HTTP 524 (origin timeout) on DNS write operations during their own platform incidents. Always wrap your update tasks with retries: 3 and delay: 10. Without this, a transient Cloudflare hiccup during your incident becomes a compounded failure.

The Correct Approach: Ansible Playbook Architecture

A production-safe failover playbook has three distinct plays, not one. Pre-flight validation, the record swap, and post-flight verification. Skipping the third play means you have no confirmation the change actually propagated — your playbook reports success while resolvers are still serving the dead IP.

Store your credentials in Ansible vault at group_vars/all/vault.yml with keys vault_cf_api_token and vault_cf_zone_id. Use an API token scoped to Zone > DNS > Edit for the specific zone only — never the Global API Key. The Global API Key cannot be scoped, cannot be rotated without breaking everything else, and is a security antipattern that I refuse to use in any production system.

Here is the full three-play failover playbook. Read the inline comments — several of them encode hard-won lessons:

# dns_failover.yml
# Cloudflare DNS failover playbook — swaps A record to standby IP
# Requires: community.cloudflare >= 1.0.0, community.dns >= 2.5.0
# Vault vars: vault_cf_api_token, vault_cf_zone_id
# Usage: ansible-playbook dns_failover.yml -e "target_hostname=app.example.com failover_ip=203.0.113.50"

---
- name: Pre-flight — validate current DNS state
  hosts: localhost
  gather_facts: false
  vars:
    cf_api_token: "{{ vault_cf_api_token }}"
    cf_zone_id: "{{ vault_cf_zone_id }}"

  tasks:
    - name: Resolve current A record for target hostname
      community.dns.lookup:
        name: "{{ target_hostname }}"
        type: A
        timeout: 5        # Required — dnspython 2.4.x hangs without this
      register: current_dns

    - name: Abort if already pointing to failover IP (idempotency guard)
      ansible.builtin.fail:
        msg: >
          DNS already points to {{ failover_ip }}.
          Failover may have already run. Aborting to prevent duplicate API calls.
      when: failover_ip in (current_dns.result | map(attribute='address') | list)

    - name: Record pre-failover state to local facts file
      ansible.builtin.copy:
        content: |
          {
            "hostname": "{{ target_hostname }}",
            "original_ip": "{{ current_dns.result[0].address }}",
            "failover_ip": "{{ failover_ip }}",
            "timestamp": "{{ ansible_date_time.iso8601 }}"
          }
        dest: /var/lib/ansible/dns_failover_state.json
        mode: '0600'      # Restrict — this file contains your original IP history

- name: Execute DNS record swap
  hosts: localhost
  gather_facts: false
  vars:
    cf_api_token: "{{ vault_cf_api_token }}"
    cf_zone_id: "{{ vault_cf_zone_id }}"

  tasks:
    - name: Fetch current DNS record ID dynamically (never use static record_id)
      ansible.builtin.uri:
        url: "https://api.cloudflare.com/client/v4/zones/{{ cf_zone_id }}/dns_records?type=A&name={{ target_hostname }}"
        method: GET
        headers:
          Authorization: "Bearer {{ cf_api_token }}"
          Content-Type: "application/json"
        status_code: 200
      register: cf_record_lookup
      retries: 3
      delay: 10           # Cloudflare record IDs change on delete+recreate — always look up dynamically

    - name: Extract record ID from API response
      ansible.builtin.set_fact:
        cf_record_id: "{{ cf_record_lookup.json.result[0].id }}"

    - name: Update A record to failover IP via Cloudflare API
      ansible.builtin.uri:
        url: "https://api.cloudflare.com/client/v4/zones/{{ cf_zone_id }}/dns_records/{{ cf_record_id }}"
        method: PUT
        headers:
          Authorization: "Bearer {{ cf_api_token }}"
          Content-Type: "application/json"
        body_format: json
        body:
          type: A
          name: "{{ target_hostname }}"
          content: "{{ failover_ip }}"
          ttl: 1          # Cloudflare auto TTL — effective for proxied records
          proxied: true   # CRITICAL: do NOT set to false during incidents — you lose DDoS protection
        status_code: 200
      register: cf_update_result
      retries: 3
      delay: 10
      failed_when: not cf_update_result.json.success

- name: Post-flight — verify DNS propagation
  hosts: localhost
  gather_facts: false

  tasks:
    - name: Wait for DNS to reflect failover IP (poll every 15s, max 2 min)
      community.dns.lookup:
        name: "{{ target_hostname }}"
        type: A
        timeout: 5
      register: post_failover_dns
      retries: 8
      delay: 15
      until: failover_ip in (post_failover_dns.result | map(attribute='address') | list)
      # Note: do NOT use a notify handler here — handlers run at end of play,
      # meaning your playbook would report success before DNS is confirmed

    - name: Confirm success and emit structured log
      ansible.builtin.debug:
        msg:
          event: dns_failover_complete
          hostname: "{{ target_hostname }}"
          new_ip: "{{ failover_ip }}"
          verified: true

Watch out: Never use a notify handler for the post-verification task. Handlers execute at the end of the play, which means your playbook would report success before DNS resolution is confirmed. Use a direct task with register and until as shown above.

Advanced Patterns: Event-Driven Failover and Rollback Safety

The playbook above is the mechanism. The architecture around it determines whether it's actually automated or just "automatable."

For event-driven triggering, I use Alertmanager webhook receivers pointed at AWX (Ansible Automation Platform) job templates. When Alertmanager fires an alert with severity=critical and service=origin, it sends a POST to the AWX webhook endpoint, which launches the failover playbook with extra_vars containing target_hostname and failover_ip. No human in the loop. The ansible-runner version matters here — use 2.3.x or higher. Older versions silently drop extra vars passed via webhook payload, which means your playbook runs with empty variables and fails in a confusing way. See the AWX webhook documentation for the full integration setup.

For rollback, the key principle is: never hardcode the original IP. You wrote that playbook six months ago. Your origin IP has changed twice since then. The rollback playbook must read the pre-failover state from the facts file written during the forward failover run. Here's the rollback playbook:

# dns_failover_rollback.yml
# Reads pre-failover state from facts file and restores original record
# Run after incident resolution — do NOT hardcode original IP

---
- name: Rollback DNS to pre-failover state
  hosts: localhost
  gather_facts: false
  vars:
    cf_api_token: "{{ vault_cf_api_token }}"
    cf_zone_id: "{{ vault_cf_zone_id }}"
    state_file: /var/lib/ansible/dns_failover_state.json  # Must be on persistent storage, not tmpfs

  tasks:
    - name: Load pre-failover state from facts file
      ansible.builtin.slurp:
        src: "{{ state_file }}"
      register: failover_state_raw

    - name: Parse state JSON
      ansible.builtin.set_fact:
        failover_state: "{{ failover_state_raw.content | b64decode | from_json }}"

    - name: Confirm rollback target with operator (interactive gate)
      ansible.builtin.pause:
        prompt: >
          Rolling back {{ failover_state.hostname }}
          from {{ failover_state.failover_ip }}
          to {{ failover_state.original_ip }}.
          Press ENTER to confirm or Ctrl+C to abort.

    - name: Fetch current record ID for rollback target
      ansible.builtin.uri:
        url: "https://api.cloudflare.com/client/v4/zones/{{ cf_zone_id }}/dns_records?type=A&name={{ failover_state.hostname }}"
        method: GET
        headers:
          Authorization: "Bearer {{ cf_api_token }}"
        status_code: 200
      register: rollback_record_lookup

    - name: Restore original A record
      ansible.builtin.uri:
        url: "https://api.cloudflare.com/client/v4/zones/{{ cf_zone_id }}/dns_records/{{ rollback_record_lookup.json.result[0].id }}"
        method: PUT
        headers:
          Authorization: "Bearer {{ cf_api_token }}"
          Content-Type: "application/json"
        body_format: json
        body:
          type: A
          name: "{{ failover_state.hostname }}"
          content: "{{ failover_state.original_ip }}"
          ttl: 1
          proxied: true
        status_code: 200
      failed_when: not (rollback_record_lookup.json.success | default(true))

    - name: Archive state file post-rollback (do not delete — keep for audit trail)
      ansible.builtin.command:
        cmd: "mv {{ state_file }} {{ state_file }}.{{ ansible_date_time.epoch }}.bak"

For multi-origin setups, extend the state storage to an S3 bucket using amazon.aws.s3_object. Write and read the dns_state.json atomically. This survives the Ansible control node crashing mid-incident — which happens more often than you'd think, because the control node is often under load during the same event that triggered failover.

Performance and Operational Cost Notes

Let's talk numbers, because SLOs need real data behind them.

The Cloudflare API p99 response time for a DNS record update sits around 180–400ms. A complete three-play playbook run — pre-flight, swap, and post-flight verification — completes in 8–15 seconds under normal conditions. That is your minimum achievable RTO using this method. Plan your SLOs accordingly. If you need sub-5-second failover, you need Cloudflare's Load Balancer or a BGP-based solution, not scripted DNS.

On TTL strategy: set proxied records to ttl: 1 permanently, not just during incidents. I've seen teams try to reactively lower TTL when they detect problems — that does absolutely nothing for resolvers that already cached the old value at a higher TTL hours earlier. The time to set a low TTL is before you need it, not during the incident.

Cloudflare's rate limit is 1200 API requests per 5 minutes per token. A single failover playbook run uses approximately 4–6 API calls (GET zone lookup if you don't pin zone_id, GET record, PUT update, optional verification calls). You have headroom for roughly 200 concurrent failover runs before hitting limits — more than enough for any reasonable fleet size.

Cost comparison: Cloudflare Load Balancer costs $5/month per hostname plus $0.50 per 500k health check queries. For teams with fewer than five critical hostnames and an acceptable RTO of 2–5 minutes, the Ansible API approach is a legitimate cost-optimized architecture, not a workaround. It's a deliberate trade-off between automation latency and infrastructure spend. Document that trade-off explicitly in your runbook so the next engineer doesn't "fix" it by adding Load Balancer without understanding why it wasn't there. You can find the full Cloudflare DNS API reference at the official Cloudflare developer documentation.

One final security note: the Ansible control node running this playbook has DNS-edit access to your entire zone. Treat it as a privileged host. Restrict SSH access, enable auditd, and rotate the API token every 90 days. The token scope is Zone > DNS > Edit for the specific zone only — scope it as narrowly as Cloudflare allows. For more patterns on securing your automation infrastructure, see the related posts at kuryzhev.cloud.

Related