Ansible DNS optimizations

This commit is contained in:
2025-03-29 11:33:06 -05:00
parent 252581a74c
commit 28fc82623a
4 changed files with 54 additions and 48 deletions

View File

@@ -1,6 +1,20 @@
--- ---
# file: dns-entry.yml # file: dns-entry.yml
# This playbook is used to create a DNS entry for a given server if it's IP address is a host variable.
# It will create a Terraform file in the homelab/terraform/dns directory.
# It will then run the Terraform apply command to create the DNS entry.
# It will then notify the update-dns handler to update the DNS entry.
# Future work:
# - Add a check to see if the DNS entry already exists.
# - Add a check to see if the Terraform provider is already configured.
# - Add a check to see if the Terraform DNS entry is already created.
- name: Validate Terraform DNS provider
delegate_to: "{{ terraform_server }}"
run_once: true
block:
- name: Check if terraform directory exists - name: Check if terraform directory exists
ansible.builtin.stat: ansible.builtin.stat:
path: homelab/terraform/dns path: homelab/terraform/dns
@@ -19,36 +33,12 @@
dest: homelab/terraform/dns/provider.tf dest: homelab/terraform/dns/provider.tf
mode: '0644' mode: '0644'
- name: Loop through all hosts - name: Create Terraform files for DNS
vars: delegate_to: "{{ terraform_server }}"
my_hosts: "{{ query('inventory_hostnames', 'all') }}"
# ansible.builtin.debug:
# msg: "Host {{ item }} has IP address: {{ hostvars[my_hosts[my_idx]].ip_address | default('No IP address defined') }}"
ansible.builtin.template: ansible.builtin.template:
src: templates/dns.tf.j2 src: templates/dns.tf.j2
dest: homelab/terraform/dns/{{ item }}.tf dest: homelab/terraform/dns/{{ inventory_hostname }}.tf
mode: '0644' mode: '0644'
when: hostvars[my_hosts[my_idx]].ip_address is defined changed_when: true
loop: "{{ my_hosts }}" notify:
loop_control: - Update DNS
index_var: my_idx
# - name: Loop through all hosts
# block:
# - name: Create Terraform files for all eligible hosts
# when: "{{hostvars["{{ item }}"].ip_address}}" is defined
# ansible.builtin.template:
# src: templates/dns.tf.j2
# dest: homelab/terraform/dns/{{ item }}.tf
# mode: '0644'
# loop: "{{ query('inventory_hostnames', 'all') }}"
# - name: Update DNS entries via Terraform
# community.general.terraform:
# project_path: homelab/terraform/dns/
# variables:
# freeipa_server: "{{ dns_server }}"
# freeipa_admin: "{{ dns_admin }}"
# freeipa_password: "{{ vault_freeipa_password }}"
# state: present
# force_init: true

View File

@@ -5,3 +5,6 @@
dns_server: "infra01.int.mk-labs.cloud" dns_server: "infra01.int.mk-labs.cloud"
dns_admin: "admin" dns_admin: "admin"
base_domain: "int.mk-labs.cloud" base_domain: "int.mk-labs.cloud"
# Terraform variables
terraform_server: "infra01"

View File

@@ -7,9 +7,22 @@
tasks: tasks:
- name: Process DNS entries - name: Process DNS entries
when: inventory_hostname == "infra01" when: ip_address is defined
ansible.builtin.import_tasks: ansible.builtin.import_tasks:
file: dns-entry.yml file: dns-entry.yml
tags: tags:
- update-dns - update-dns
- never - never
handlers:
- name: Update DNS
run_once: true
delegate_to: "{{ terraform_server }}"
community.general.terraform:
project_path: homelab/terraform/dns/
variables:
freeipa_server: "{{ dns_server }}"
freeipa_admin: "{{ dns_admin }}"
freeipa_password: "{{ vault_freeipa_password }}"
state: present
force_init: true

View File

@@ -5,7 +5,7 @@ resource "freeipa_dns_record" "{{ inventory_hostname }}" {
name = "{{ inventory_hostname }}" name = "{{ inventory_hostname }}"
type = "A" type = "A"
records = [ records = [
"{{ hostvars[inventory_hostname].ip_address }}", "{{ ip_address }}",
] ]
ttl = 60 ttl = 60
} }