diff --git a/infra-config/create_vm_from_clone.yml b/infra-config/create_vm_from_clone.yml deleted file mode 100644 index 066aa82..0000000 --- a/infra-config/create_vm_from_clone.yml +++ /dev/null @@ -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 }}" diff --git a/infra-config/group_vars/ipaserver/vars b/infra-config/group_vars/ipaserver/vars new file mode 100644 index 0000000..d1bcfc4 --- /dev/null +++ b/infra-config/group_vars/ipaserver/vars @@ -0,0 +1,4 @@ +--- +#freeipa_user: "admin" +ipaadmin_password: "{{ vault_freeipa_password }}" +ipaserver_domain: "int.mk-labs.cloud" diff --git a/infra-config/inventory.yml b/infra-config/inventory.yml index 7c46ba2..68e24e2 100644 --- a/infra-config/inventory.yml +++ b/infra-config/inventory.yml @@ -1,44 +1,48 @@ # file: inventory.yml proxmox: - hosts: - pve0[1:3]: + hosts: + pve0[1:3]: unbound_servers: - hosts: - unbound02: + hosts: + unbound02: backup_servers: - hosts: - backup: + hosts: + backup: freeipa: - hosts: - infra01: + hosts: + infra01: + +ipaserver: + hosts: + infra01.int.mk-labs.cloud: matchbox: - hosts: - infra01: + hosts: + infra01: hub_cluster: - hosts: - ocp-hub: + hosts: + ocp-hub: sql_servers: - hosts: - sql01: + hosts: + sql01: n8n_servers: - hosts: - n8n: + hosts: + n8n: docker_servers: - hosts: - docker01: + hosts: + docker01: observer: - hosts: - prometheus: + hosts: + prometheus: target: - hosts: - prometheus: + hosts: + prometheus: diff --git a/infra-config/playbooks/add_dns_entry.yml b/infra-config/playbooks/add_dns_entry.yml new file mode 100644 index 0000000..a2e775a --- /dev/null +++ b/infra-config/playbooks/add_dns_entry.yml @@ -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 diff --git a/infra-config/playbooks/create_vm_from_clone.yml b/infra-config/playbooks/create_vm_from_clone.yml new file mode 100644 index 0000000..d06704a --- /dev/null +++ b/infra-config/playbooks/create_vm_from_clone.yml @@ -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 diff --git a/infra-config/playbooks/del_dns_entry.yml b/infra-config/playbooks/del_dns_entry.yml new file mode 100644 index 0000000..0805642 --- /dev/null +++ b/infra-config/playbooks/del_dns_entry.yml @@ -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 diff --git a/infra-config/playbooks/proxmox_clone_vm.yml b/infra-config/playbooks/proxmox_clone_vm.yml new file mode 100644 index 0000000..72f985f --- /dev/null +++ b/infra-config/playbooks/proxmox_clone_vm.yml @@ -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 + # \ No newline at end of file diff --git a/infra-config/playbooks/set_hostname.yml b/infra-config/playbooks/set_hostname.yml new file mode 100644 index 0000000..449ce9a --- /dev/null +++ b/infra-config/playbooks/set_hostname.yml @@ -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 }}"