From 2def875433ed3d3c8f2073bf1c6df9917ee6dfc5 Mon Sep 17 00:00:00 2001 From: Josh Lay Date: Sat, 18 Dec 2021 21:39:48 -0600 Subject: [PATCH 01/10] reflect changes to domain management --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1741af9..b1302f4 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,11 @@ Sample Ansible role to create CloudFlare DNS records ## Requirements -1) See `roles/defaults/vars.yml` for sample DNS zone entry definitions -- change/remove these as desired +1) See `domains/sample.yml` for sample DNS zone entry definitions -- change these as desired + +`domains` is a symbolic link to `roles/create-records/vars/`, these are loaded based on the `domains` list in `play.yml` + +_TODO_: Load files created here dynamically 2) Your CloudFlare email address and API key must be stored in `~/.cloudflare.yml` like so for authentication: ``` From e538a528d1d0fbf3af251bab045b5304eb39c1a0 Mon Sep 17 00:00:00 2001 From: Josh Lay Date: Sat, 18 Dec 2021 21:44:35 -0600 Subject: [PATCH 02/10] include credentials only once --- play.yml | 3 +++ roles/create-records/tasks/main.yml | 4 ---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/play.yml b/play.yml index 89e1f37..3805d05 100644 --- a/play.yml +++ b/play.yml @@ -7,6 +7,9 @@ domains: - sampledomain.local tasks: + - name: import cloudflare creds + include_vars: + file: "~/.cloudflare.yml" - name: "enforce state for {{item}}" include_role: name: create-records diff --git a/roles/create-records/tasks/main.yml b/roles/create-records/tasks/main.yml index dbb97db..417598f 100644 --- a/roles/create-records/tasks/main.yml +++ b/roles/create-records/tasks/main.yml @@ -1,9 +1,5 @@ --- -- name: import cloudflare creds - include_vars: - file: "~/.cloudflare.yml" - - name: "create records ({{domain}})" cloudflare_dns: zone: "{{ domain }}" From 66e6c4d30a36512af6033da280c9b8ee0a975121 Mon Sep 17 00:00:00 2001 From: Josh Lay Date: Sat, 18 Dec 2021 21:47:30 -0600 Subject: [PATCH 03/10] fix name for create-records include_role --- play.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/play.yml b/play.yml index 3805d05..f856e26 100644 --- a/play.yml +++ b/play.yml @@ -10,7 +10,7 @@ - name: import cloudflare creds include_vars: file: "~/.cloudflare.yml" - - name: "enforce state for {{item}}" + - name: "create-records: include role" include_role: name: create-records vars_from: "{{item}}" From 70fd56adcd830147d7033fff9ff2d40ae2144391 Mon Sep 17 00:00:00 2001 From: Josh Lay Date: Sat, 18 Dec 2021 21:52:07 -0600 Subject: [PATCH 04/10] clarification of domain management --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b1302f4..0ff89ec 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,11 @@ Sample Ansible role to create CloudFlare DNS records ## Requirements -1) See `domains/sample.yml` for sample DNS zone entry definitions -- change these as desired +1) See `roles/create-records/vars/sample.yml` for sample DNS zone entry definitions -- change these as desired -`domains` is a symbolic link to `roles/create-records/vars/`, these are loaded based on the `domains` list in `play.yml` +To add a domain, define it in the `domains` list in `play.yml`. This will load the correlating file at `roles/create-records/vars/{{domain}}.yml`. + +The symbolic link `./domains` was provided to make these files more accessible, not used for function -- can be removed. _TODO_: Load files created here dynamically From 3dd6c77eaec95e5ab56cd2b9c9379e169a202fcf Mon Sep 17 00:00:00 2001 From: Josh Lay Date: Sat, 18 Dec 2021 21:52:33 -0600 Subject: [PATCH 05/10] formatting --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0ff89ec..58237d4 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,9 @@ Sample Ansible role to create CloudFlare DNS records 1) See `roles/create-records/vars/sample.yml` for sample DNS zone entry definitions -- change these as desired -To add a domain, define it in the `domains` list in `play.yml`. This will load the correlating file at `roles/create-records/vars/{{domain}}.yml`. +To add a domain, define it in the `domains` list in `play.yml`. + +This will load the correlating file at `roles/create-records/vars/{{domain}}.yml`. The symbolic link `./domains` was provided to make these files more accessible, not used for function -- can be removed. From b79fb76f250f44c4b48f4634a9dcab26be97d93b Mon Sep 17 00:00:00 2001 From: Josh Lay Date: Sat, 18 Dec 2021 21:57:36 -0600 Subject: [PATCH 06/10] disable localhost warning, that's our intention --- ansible.cfg | 1 + 1 file changed, 1 insertion(+) diff --git a/ansible.cfg b/ansible.cfg index 56bf283..b40b464 100644 --- a/ansible.cfg +++ b/ansible.cfg @@ -1,2 +1,3 @@ [defaults] nocows=True +localhost_warning=False From 0e6228b751a0ef7943f710ccfb7a5755a20193a1 Mon Sep 17 00:00:00 2001 From: Josh Lay Date: Wed, 20 Apr 2022 23:22:01 -0500 Subject: [PATCH 07/10] fix fact gathering, rename role --- ansible.cfg | 1 + play.yml | 5 ++--- roles/{create-records => manage-records}/tasks/main.yml | 0 roles/{create-records => manage-records}/vars/sample.yml | 0 4 files changed, 3 insertions(+), 3 deletions(-) rename roles/{create-records => manage-records}/tasks/main.yml (100%) rename roles/{create-records => manage-records}/vars/sample.yml (100%) diff --git a/ansible.cfg b/ansible.cfg index b40b464..07bb4c6 100644 --- a/ansible.cfg +++ b/ansible.cfg @@ -1,3 +1,4 @@ [defaults] nocows=True localhost_warning=False +gathering=smart \ No newline at end of file diff --git a/play.yml b/play.yml index f856e26..ce916f7 100644 --- a/play.yml +++ b/play.yml @@ -2,7 +2,6 @@ - name: create cloudflare dns records hosts: localhost connection: local - gather_facts: smart vars: domains: - sampledomain.local @@ -10,8 +9,8 @@ - name: import cloudflare creds include_vars: file: "~/.cloudflare.yml" - - name: "create-records: include role" + - name: "manage-records: include role" include_role: - name: create-records + name: manage-records vars_from: "{{item}}" with_items: "{{domains}}" diff --git a/roles/create-records/tasks/main.yml b/roles/manage-records/tasks/main.yml similarity index 100% rename from roles/create-records/tasks/main.yml rename to roles/manage-records/tasks/main.yml diff --git a/roles/create-records/vars/sample.yml b/roles/manage-records/vars/sample.yml similarity index 100% rename from roles/create-records/vars/sample.yml rename to roles/manage-records/vars/sample.yml From 2d4dc4ac4803db5db02a4c7b91aa5153c7e8fe68 Mon Sep 17 00:00:00 2001 From: Josh Lay Date: Wed, 20 Apr 2022 23:25:27 -0500 Subject: [PATCH 08/10] move record state to vars, support removal --- domains | 2 +- roles/manage-records/tasks/main.yml | 7 +++---- roles/manage-records/vars/sample.yml | 8 -------- roles/manage-records/vars/sampledomain.local.yml | 10 ++++++++++ 4 files changed, 14 insertions(+), 13 deletions(-) delete mode 100644 roles/manage-records/vars/sample.yml create mode 100644 roles/manage-records/vars/sampledomain.local.yml diff --git a/domains b/domains index 9d33abd..7fe77e7 120000 --- a/domains +++ b/domains @@ -1 +1 @@ -roles/create-records/vars \ No newline at end of file +roles/manage-records/vars \ No newline at end of file diff --git a/roles/manage-records/tasks/main.yml b/roles/manage-records/tasks/main.yml index 417598f..f43aaf5 100644 --- a/roles/manage-records/tasks/main.yml +++ b/roles/manage-records/tasks/main.yml @@ -1,16 +1,15 @@ --- -- name: "create records ({{domain}})" +- name: "manage records for ({{ domain }})" cloudflare_dns: zone: "{{ domain }}" record: "{{ record.name }}" value: "{{ record.value }}" type: "{{ record.type }}" - solo: yes - state: present + solo: "{{ 'yes' if record.state in ['present'] | default(false) else omit }}" + state: "{{ record.state }}" # don't assume / set a default, require explicit instruction account_email: "{{ cf_email }}" account_api_token: "{{ cf_token }}" loop: "{{ records }}" loop_control: loop_var: record - when: (item.state is undefined) or (not item.state == 'absent') diff --git a/roles/manage-records/vars/sample.yml b/roles/manage-records/vars/sample.yml deleted file mode 100644 index 9e406be..0000000 --- a/roles/manage-records/vars/sample.yml +++ /dev/null @@ -1,8 +0,0 @@ -domain: sampledomain.local -records: - - name: 'subdomain' - value: '127.0.0.1' - type: A - - name: 'subdomain' - value: '::1' - type: AAAA diff --git a/roles/manage-records/vars/sampledomain.local.yml b/roles/manage-records/vars/sampledomain.local.yml new file mode 100644 index 0000000..914816b --- /dev/null +++ b/roles/manage-records/vars/sampledomain.local.yml @@ -0,0 +1,10 @@ +domain: sampledomain.local +records: + - name: 'subdomain' # create a silly localhost subdomain.sampledomain.io A record (IPv4) + value: '127.0.0.1' + type: A + state: present + - name: 'subdomain' # remove equally silly subdomain.sampledomain.io AAAA record (IPv6 local) + value: '::1' + type: AAAA + state: absent From e87a0cf268bc2b00f76a1c2b68c670e0faea0cad Mon Sep 17 00:00:00 2001 From: Josh Lay Date: Wed, 20 Apr 2022 23:27:58 -0500 Subject: [PATCH 09/10] correct TLD in comment --- roles/manage-records/vars/sampledomain.local.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/roles/manage-records/vars/sampledomain.local.yml b/roles/manage-records/vars/sampledomain.local.yml index 914816b..eae1960 100644 --- a/roles/manage-records/vars/sampledomain.local.yml +++ b/roles/manage-records/vars/sampledomain.local.yml @@ -1,10 +1,10 @@ domain: sampledomain.local records: - - name: 'subdomain' # create a silly localhost subdomain.sampledomain.io A record (IPv4) + - name: 'subdomain' # create a silly localhost subdomain.sampledomain.local A record (IPv4) value: '127.0.0.1' type: A state: present - - name: 'subdomain' # remove equally silly subdomain.sampledomain.io AAAA record (IPv6 local) + - name: 'subdomain' # remove equally silly subdomain.sampledomain.local AAAA record (IPv6 local) value: '::1' type: AAAA state: absent From 8789c9d5941e7820735acbc3cb955f364eef1cdf Mon Sep 17 00:00:00 2001 From: Josh Lay Date: Wed, 20 Apr 2022 23:28:58 -0500 Subject: [PATCH 10/10] support record removal --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 58237d4..31bff9d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # cfdns -Sample Ansible role to create CloudFlare DNS records +Sample Ansible role to create/remove CloudFlare DNS records ## Requirements