--- - 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 ansible.builtin.set_fact: vm_net0: "{{ proxmox_vm_info.proxmox_vms[0].config.net0 }}" - name: Extract MAC address using regex ansible.builtin.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