Ansible proxmox_kvm gettting MAC address for PXE

jdancer

Renowned Member
May 26, 2019
195
53
68
54
Anyone successfully used the Ansible module of proxmox_kvm to get a MAC address of a VM for a PXE install?

Here is the task code:

- name: Get Facts
proxmox_kvm:
api_user: "root@pam"
api_password: test
api_host: test
node: test
name: test
validate_certs: no
state: current
register: stuff

- debug: msg="{{ stuff }}"​

Playbook results:

PLAY [test]

TASK [Get Facts]

ok: [test]

TASK [debug]

ok: [test] => {
"msg": {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"failed": false,
"msg": "VM test with vmid = 100 is stopped",
"status": "stopped",
"warnings": [
"Platform ..."
]
}
}​

According to proxmox_kvm_module.html return-values at docs dot ansible dot com, it should return the MAC address of the VM?

So, has anyone able to get a MAC address of a Proxmox VM?
 
Thanks to morph027, the MAC address is returned upon VM creation. Here's my Ansible VM Create task:

Code:
 tasks:
  - name: VM create
    proxmox_kvm:
      api_user: "root@pam"
      api_password: "{{ api_password }}"
      api_host: "{{ api_host }}"
      node: "{{ node }}"
      name: "{{ inventory_hostname }}"
      net: "{{ net }}"
      scsihw: "{{ scsihw }}"
      virtio: "{{ virtio }}"
      cpu: "{{ cpu }}"
      cpuunits: "{{ cpuunits }}"
      cores: "{{ cores }}"
      memory: "{{ memory }}"
      balloon: "{{ memory }}"
      boot: n
      agent: 1
      description: 'Ansible Created'
      validate_certs: no
      state: present
    register: result

  - name: MAC get
    set_fact:
      macaddress: "{{ result.mac.net0 }}"

  - debug: var=macaddress
 
Hi,

seems also not to work with LXCs (anymore).

This is the output of my "result":
Code:
    "msg": {
        "changed": true,
        "failed": false,
        "msg": "Cloned VM 334 from 300"
    }

Is there a way, to get the mac or the ip after cloning / creating a vm/lxc?
Or what would be the best practice to go on configuring the new host with ansible?

(Using the hostname is no option, because i at first have to add the host to my local dns - this i want to do in the next task of my playbook)

Thanks, Alex
 
Last edited:
Is there a way, to get the mac or the ip after cloning / creating a vm/lxc?
QEMU guest agent is capable of doing this for QEMU/KVM VMs, but it needs to be installed in the template. LX(C) container should be easy, you can just inspect the running configuration, but if that is possible via the API ....
 
Hi and thanks for your reply.

I solved it with this task - not beautiful, but it works for me:
Code:
    - name: Get mac from PVE
      ansible.builtin.shell: set pipefail && awk -v IGNORECASE=1 '$1 == "net0:" { print }' /etc/pve/lxc/{{ containerid }}.conf | cut -d',' -f4 | cut -d'=' -f2
      register: result_mac
      retries: 10
      delay: 10
      delegate_to: pve1.domain.de
 
Hi and thanks for your reply.

I solved it with this task - not beautiful, but it works for me:
Code:
    - name: Get mac from PVE
      ansible.builtin.shell: set pipefail && awk -v IGNORECASE=1 '$1 == "net0:" { print }' /etc/pve/lxc/{{ containerid }}.conf | cut -d',' -f4 | cut -d'=' -f2
      register: result_mac
      retries: 10
      delay: 10
      delegate_to: pve1.domain.de
good idea, but keep in mind, that this will not work for a cluster. The files are in /etc/pve/nodes/<nodename>/lxc/<containerid>.conf.
 
  • Like
Reactions: e36Alex

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!