A simple 'dynamic dns'-client with Ansible and Cloudflare
- April 20, 2019 in
- ansible
- cloudflare
- dns
My ISP does not give me a static IP. It doesn’t change often, but in the rare case that it has changed I never discover it before I actually need it.
So. I wanted a very simple way of auto-updating the home.example.com
record. I didn’t want to bother with a thirdparty client from something like dyndns (and I didn’t want to pay for anything).
As I use Cloudflare as dns service for all domains I own I could just use their API and solve it with a oneliner, but doing it with Ansible is more readable
1---
2- hosts: localhost
3 connection: local
4
5 vars:
6 - cf_zone: 'example.com'
7 cf_record: 'home'
8 cf_email: 'email-for-cloudflare-login'
9 cf_api_token: 'your-api-token-from-cloudflare'
10
11 tasks:
12 - name: Get current public IP address
13 uri:
14 url: 'https://ipinfo.io/ip'
15 return_content: yes
16 register: public_ip
17
18 - name: Update DNS record
19 cloudflare_dns:
20 zone: '{{ cf_zone }}'
21 record: '{{ cf_record }}'
22 type: A
23 value: '{{ public_ip.content | trim }}'
24 state: present
25 account_email: '{{ cf_email }}'
26 account_api_token: '{{ cf_api_token }}'
27
28 - name: Create Cron job
29 cron:
30 name: Update DNS record
31 minute: '*/15'
32 job: 'ansible-playbook -i "localhost," /path/to/ansible/playbook.yml'