diff --git a/infra-config/ansible/set_sudoers.yml b/infra-config/ansible/set_sudoers.yml index 11f2c7d..541ae1a 100644 --- a/infra-config/ansible/set_sudoers.yml +++ b/infra-config/ansible/set_sudoers.yml @@ -9,6 +9,6 @@ ansible.builtin.lineinfile: path: /etc/sudoers state: present - regexp: '^%wheel' - line: '%wheel ALL=(ALL) NOPASSWD:ALL' - validate: 'visudo -cf %s' + regexp: "^%wheel" + line: "%wheel ALL=(ALL) NOPASSWD:ALL" + validate: "visudo -cf %s" diff --git a/infra-config/inventory.yml b/infra-config/inventory.yml index 68e24e2..3a0877d 100644 --- a/infra-config/inventory.yml +++ b/infra-config/inventory.yml @@ -3,6 +3,10 @@ proxmox: hosts: pve0[1:3]: +dhcp_server: + hosts: + matchbox: + unbound_servers: hosts: unbound02: @@ -19,9 +23,9 @@ ipaserver: hosts: infra01.int.mk-labs.cloud: -matchbox: - hosts: - infra01: +# matchbox: +# hosts: +# infra01: hub_cluster: hosts: diff --git a/infra-config/playbooks/add_dhcp_entry.yml b/infra-config/playbooks/add_dhcp_entry.yml new file mode 100644 index 0000000..68c8a2c --- /dev/null +++ b/infra-config/playbooks/add_dhcp_entry.yml @@ -0,0 +1,39 @@ +--- +- name: Add entry to DNSMasq + hosts: all + gather_facts: false + + tasks: + - name: Retrieve information about specific VM by name and get current configuration + delegate_to: "localhost" + 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: Add line to hosts file + delegate_to: "{{ groups['dhcp_server'][0] }}" + become: true + ansible.builtin.lineinfile: + path: /etc/dnsmasq.d/hosts.conf + regexp: "# {{ inventory_hostname }}$" + line: "dhcp-host={{ vm_mac_address|lower }},{{ ip_address }} # {{ inventory_hostname }}" + state: present + # restart dnsmasq service + - name: Restart service dnsmasq + delegate_to: "{{ groups['dhcp_server'][0] }}" + become: true + ansible.builtin.service: + name: dnsmasq + state: restarted diff --git a/infra-config/playbooks/create_vm_from_clone.yml b/infra-config/playbooks/create_vm_from_clone.yml index d06704a..e1001a7 100644 --- a/infra-config/playbooks/create_vm_from_clone.yml +++ b/infra-config/playbooks/create_vm_from_clone.yml @@ -3,12 +3,17 @@ hosts: all gather_facts: false +- name: Proxmox Clone VM Playbook + ansible.builtin.import_playbook: proxmox_clone_vm.yml + - name: DNS Playbook ansible.builtin.import_playbook: add_dns_entry.yml - # Create DHCP Reservation +- name: DHCP Playbook + ansible.builtin.import_playbook: add_dhcp_entry.yml -- name: Proxmox Clone VM Playbook - ansible.builtin.import_playbook: proxmox_clone_vm.yml -# - name: Set Hostname Playbook -# ansible.builtin.import_playbook: set_hostname.yml +- name: Start VM Playbook + ansible.builtin.import_playbook: proxmox_start_vm.yml + +- name: Set Hostname Playbook + ansible.builtin.import_playbook: set_hostname.yml diff --git a/infra-config/playbooks/proxmox_clone_vm.yml b/infra-config/playbooks/proxmox_clone_vm.yml index 72f985f..cef6a76 100644 --- a/infra-config/playbooks/proxmox_clone_vm.yml +++ b/infra-config/playbooks/proxmox_clone_vm.yml @@ -24,40 +24,13 @@ 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 + # - 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 diff --git a/infra-config/playbooks/proxmox_start_vm.yml b/infra-config/playbooks/proxmox_start_vm.yml new file mode 100644 index 0000000..18781ea --- /dev/null +++ b/infra-config/playbooks/proxmox_start_vm.yml @@ -0,0 +1,32 @@ +--- +- name: Create VM ID from IP address for Proxmox hosts + hosts: all + gather_facts: false + + tasks: + - name: Start Proxmox VM + delegate_to: "localhost" + when: platform is defined and platform == "proxmox" + community.proxmox.proxmox_kvm: + api_user: "{{ proxmox_user }}" + api_password: "{{ proxmox_password }}" + api_host: "{{ proxmox_host }}" + name: "{{ inventory_hostname }}" + node: "{{ proxmox_clone_node }}" + state: started + + # - name: Wait for the VM to start + # delegate_to: "localhost" + # wait_for: + # port: 22 + # host: "{{ inventory_hostname }}" + # search_regex: OpenSSH + # delay: 10 + # timeout: 60 + + # - name: Wait for the reboot to complete if there was a change. + # wait_for_connection: + # connect_timeout: 10 + # sleep: 5 + # delay: 5 + # timeout: 300 diff --git a/infra-config/playbooks/set_hostname.yml b/infra-config/playbooks/set_hostname.yml index 449ce9a..ede97e7 100644 --- a/infra-config/playbooks/set_hostname.yml +++ b/infra-config/playbooks/set_hostname.yml @@ -1,10 +1,12 @@ --- -- name: Create VM ID from IP address for Proxmox hosts +- name: Set hostname of VM hosts: all gather_facts: false tasks: - name: Set hostname of VM + become: true delegate_to: "{{ inventory_hostname }}" ansible.builtin.hostname: name: "{{ hostname }}" + #delay: 20