Reputation: 35
How can I add an A record to an existing DigitalOcean DNS domain using Ansible?
I tried the following:
---
- hosts: localhost
connection: local
gather_facts: no
tasks:
- digital_ocean_domain:
state: present
name: my.example.com
- digital_ocean_domain:
state: present
name: test1.my.example.com
ip: 127.0.0.1
But the second task adds a full domain test1.my.example.com
(with NS records) along the A record for test1.my.example.com
.
I wanted to add just the A record for test1.my.example.com
to the domain created in first task.
For reasons quite obvious, I don't want a separate domain for every single host.
Upvotes: 1
Views: 532
Reputation: 4742
The community.digitalocean.digital_ocean_domain_record
has been added to the collection.
You can install it with:
ansible-galaxy collection install community.digitalocean
Given an existing domain (with NS & SOA records), you should be able to add a specific record like so:
- name: Create Droplet
community.digitalocean.droplet: {...}
register: dx
- name: Create DNS Entry for Droplet
community.digitalocean.digital_ocean_domain_record:
state: present
example: "{{ domain }}"
type: A
name: "www"
data: "{{ dx.data.ip_address }}"
This example registers the public IP address of a new droplet as www.{{ domain }}
, where domain=example.com
Upvotes: 1
Reputation: 33203
There is no such ansible module (although there was one making its way through PR, across three repo renames but it was closed by the ansible team), but there is an open issue you can track.
In the meantime, you can accomplish that via the Domain Record API, which you can package up into a "playbook local" module, if having this much yaml bothers you.
- uri:
url: "https://api.digitalocean.com/v2/domains/{{ dns_domain }}/records"
headers:
accept: application/json
authorization: Bearer {{ oauth2_token }}
return_content: yes
register: domain_records_resp
- when: domain_host not in (domain_records_resp.domain_records | map(attribute="name") | list)
uri:
method: POST
url: "https://api.digitalocean.com/v2/domains/{{ dns_domain }}/records"
headers:
authorization: Bearer {{ oauth2_token }}
body: '{{ create_record | to_json }}'
body_format: json
return_content: yes
vars:
create_record:
data: 127.0.0.1
flags: null
name: '{{ domain_host }}'
port: null
priority: null
tag: null
ttl: 1800
type: A
weight: null
Upvotes: 1