Changed DNS record creation to Ansible

This commit is contained in:
2025-07-08 23:27:46 -05:00
parent 2bce9c3f29
commit 8212a69802
8 changed files with 150 additions and 92 deletions

View File

@@ -1,70 +0,0 @@
---
- name: Create VM ID from IP address for Proxmox hosts
hosts: all
gather_facts: false
tasks:
- name: Create Proxmox VMs
delegate_to: "localhost"
when: platform is defined and platform == "proxmox"
block:
- name: Create VM ID from IP address
ansible.builtin.set_fact:
vm_id: "{{ ip_address.split('.')[-2] }}{{ '%03d' % (ip_address.split('.')[-1] | int) }}"
- name: Clone VM with source vmid and target newid and raw format
community.proxmox.proxmox_kvm:
api_user: "{{ proxmox_user }}"
api_password: "{{ proxmox_password }}"
api_host: "{{ proxmox_host }}"
clone: "{{ vm_clone_source }}"
newid: "{{ vm_id }}"
name: "{{ inventory_hostname }}"
node: "{{ proxmox_clone_node }}"
storage: "{{ vm_storage }}"
format: raw
- name: Retrieve information about specific VM by name and get current configuration
community.proxmox.proxmox_vm_info:
api_user: "{{ proxmox_user }}"
api_password: "{{ proxmox_password }}"
api_host: "{{ proxmox_host }}"
name: "{{ inventory_hostname }}"
config: current
register: proxmox_vm_info
- name: Extract net0 information
set_fact:
vm_net0: "{{ proxmox_vm_info.proxmox_vms[0].config.net0 }}"
- name: Extract MAC address using regex
set_fact:
vm_mac_address: "{{ vm_net0 | regex_search('([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}') }}"
# Create DNS Entry
# Create DHCP Reservation
#
# - name: Start VM
# community.proxmox.proxmox_kvm:
# api_user: "{{ proxmox_user }}"
# api_password: "{{ proxmox_password }}"
# api_host: "{{ proxmox_host }}"
# name: "{{ inventory_hostname }}"
# node: "{{ proxmox_target_node }}"
# state: started
# - name: Add VM to HA group
# community.proxmox.proxmox_cluster_ha_resources:
# api_user: "{{ proxmox_user }}"
# api_password: "{{ proxmox_password }}"
# api_host: "{{ proxmox_host }}"
# name: vm:"{{ vm_id }}"
# state: "present"
# group: "{{ ha_group }}"
# max_relocate: 2
# max_restart: 2
# - name: Set hostname of cloned VM
# delegate_to: "{{ inventory_hostname }}"
# ansible.builtin.hostname:
# name: "{{ hostname }}"

View File

@@ -0,0 +1,4 @@
---
#freeipa_user: "admin"
ipaadmin_password: "{{ vault_freeipa_password }}"
ipaserver_domain: "int.mk-labs.cloud"

View File

@@ -15,6 +15,10 @@ freeipa:
hosts:
infra01:
ipaserver:
hosts:
infra01.int.mk-labs.cloud:
matchbox:
hosts:
infra01:

View File

@@ -0,0 +1,17 @@
---
- name: Add entry to DNS
hosts: all
gather_facts: false
tasks:
- name: Create DNS entry for {{ inventory_hostname }} #on {{ groups['ipa_servers'][0] }}
delegate_to: "{{ groups['ipaserver'][0] }}"
freeipa.ansible_freeipa.ipadnsrecord:
ipaapi_context: "server"
ipaadmin_password: "{{ vault_freeipa_password }}"
name: "{{ inventory_hostname }}"
zone_name: "{{ base_domain }}"
record_type: "A"
record_value: "{{ ip_address }}"
record_ttl: 60
#create_reverse: yes

View File

@@ -0,0 +1,14 @@
---
- name: Master playbook to create VM from a template
hosts: all
gather_facts: false
- name: DNS Playbook
ansible.builtin.import_playbook: add_dns_entry.yml
# Create DHCP Reservation
- name: Proxmox Clone VM Playbook
ansible.builtin.import_playbook: proxmox_clone_vm.yml
# - name: Set Hostname Playbook
# ansible.builtin.import_playbook: set_hostname.yml

View File

@@ -0,0 +1,16 @@
---
- name: Remove entry from DNS
hosts: all
gather_facts: false
tasks:
- name: Delete DNS entry for {{ inventory_hostname }} #on {{ groups['ipa_servers'][0] }}
delegate_to: "{{ groups['ipaserver'][0] }}"
freeipa.ansible_freeipa.ipadnsrecord:
ipaapi_context: "server"
ipaadmin_password: "{{ vault_freeipa_password }}"
zone_name: "{{ base_domain }}"
records:
- name: "{{ inventory_hostname }}"
del_all: yes
state: absent

View File

@@ -0,0 +1,63 @@
---
- name: Create VM ID from IP address for Proxmox hosts
hosts: all
gather_facts: false
tasks:
- name: Create Proxmox VMs
delegate_to: "localhost"
when: platform is defined and platform == "proxmox"
block:
- name: Create VM ID from IP address
ansible.builtin.set_fact:
vm_id: "{{ ip_address.split('.')[-2] }}{{ '%03d' % (ip_address.split('.')[-1] | int) }}"
- name: Clone VM with source vmid and target newid and raw format
community.proxmox.proxmox_kvm:
api_user: "{{ proxmox_user }}"
api_password: "{{ proxmox_password }}"
api_host: "{{ proxmox_host }}"
clone: "{{ vm_clone_source }}"
newid: "{{ vm_id }}"
name: "{{ inventory_hostname }}"
node: "{{ proxmox_clone_node }}"
storage: "{{ vm_storage }}"
format: raw
- name: Retrieve information about specific VM by name and get current configuration
community.proxmox.proxmox_vm_info:
api_user: "{{ proxmox_user }}"
api_password: "{{ proxmox_password }}"
api_host: "{{ proxmox_host }}"
name: "{{ inventory_hostname }}"
config: current
register: proxmox_vm_info
- name: Extract net0 information
set_fact:
vm_net0: "{{ proxmox_vm_info.proxmox_vms[0].config.net0 }}"
- name: Extract MAC address using regex
set_fact:
vm_mac_address: "{{ vm_net0 | regex_search('([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}') }}"
# - name: Start VM
# community.proxmox.proxmox_kvm:
# api_user: "{{ proxmox_user }}"
# api_password: "{{ proxmox_password }}"
# api_host: "{{ proxmox_host }}"
# name: "{{ inventory_hostname }}"
# node: "{{ proxmox_target_node }}"
# state: started
# - name: Add VM to HA group
# community.proxmox.proxmox_cluster_ha_resources:
# api_user: "{{ proxmox_user }}"
# api_password: "{{ proxmox_password }}"
# api_host: "{{ proxmox_host }}"
# name: vm:"{{ vm_id }}"
# state: "present"
# group: "{{ ha_group }}"
# max_relocate: 2
# max_restart: 2
#

View File

@@ -0,0 +1,10 @@
---
- name: Create VM ID from IP address for Proxmox hosts
hosts: all
gather_facts: false
tasks:
- name: Set hostname of VM
delegate_to: "{{ inventory_hostname }}"
ansible.builtin.hostname:
name: "{{ hostname }}"