How to configure network settings using Ansible (community.general.proxmox_kvm module)?

samo.goljat

New Member
Oct 25, 2024
5
0
1
I have managed to connect with API-user and clone virtual machine. With:


tasks:
- name: Clone VM from template
community.general.proxmox_kvm:
api_user: "{{ api_user }}"
api_password: "{{ proxmox_password }}"
api_host: "{{ api_host }}"
clone: AlmaT01 # Source VM name (template)
name: "{{ vm_name }}" # New target VM name
node: "{{ node }}" # Proxmox node
storage: dpve_LDisk01 # Storage backend
format: qcow2 # Disk format
timeout: 500 # Timeout for the operation
register: clone_result


But when it comes to configuring network parameters I can't seem to change them with this approach:

- name: Configure network settings
community.general.proxmox_kvm:
api_user: "{{ api_user }}"
api_password: "{{ proxmox_password }}"
api_host: "{{ api_host }}"
vmid: "{{ new_vmid }}"
node: "{{ node }}"
net:
model: virtio
bridge: vmbr10
ip: "{{ ip_address }}/24"
gw: "{{ gateway }}"
nameservers: "{{ dns_servers }}"
searchdomains: "{{ search_domain }}"
update: yes

https://docs.ansible.com/ansible/latest/collections/community/general/proxmox_kvm_module.html

Am I doing it correctly (syntax-wise), or is there any other approach available for doing it.
The result I get from Ansible is:

TASK [Configure network settings] *****************************************************************************************************************************************************************************
changed: [localhost]

TASK [Verify network configuration via SSH (optional)] ********************************************************************************************************************************************************
fatal: [localhost -> 10.1.12.195]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 10.1.12.195 port 22: No route to host", "unreachable": true}

PLAY RECAP ****************************************************************************************************************************************************************************************************
localhost : ok=5 changed=3 unreachable=1 failed=0 skipped=1 rescued=0 ignored=0

So it seems it executes the task, but it does not assign properly IP address. (It uses DHCP).
I have also no clue, how to create so called Cloud-init image nor is in the options to use containers.
 
The real question would, if there exist a way to setup network parameters directly using ansible without cloud-init image/packet. For example we currently have effective 1-step process with:

https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware_guest_module.html

- name: Clone a virtual machine from Linux template and customize
community.vmware.vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ datacenter }}"
state: present
folder: /DC1/vm
template: "{{ template }}"
name: "{{ vm_name }}"
cluster: DC1_C1
networks:
- name: VM Network
ip: 192.168.10.11
netmask: 255.255.255.0
wait_for_ip_address: true
customization:
domain: "{{ guest_domain }}"
dns_servers:
- 8.9.9.9
- 7.8.8.9
dns_suffix:
- example.com
- example2.com
script_text: |
#!/bin/bash
touch /tmp/touch-from-playbook
delegate_to: localhost
 
f there exist a way to setup network parameters directly
I haven't done it with ansible, yet I did it via the PVE API directly. The implementation is the same as with VMware with the guest integration tools (here qemu-guest-agent), which let you query the VM and also setup thinks if the guest allows it.
 
I haven't done it with ansible, yet I did it via the PVE API directly. The implementation is the same as with VMware with the guest integration tools (here qemu-guest-agent), which let you query the VM and also setup thinks if the guest allows it.
Can you perhaps link me to the exact ansible module or possible implementation (example). I have been trying with some level of success with cloud-init image, but some things aren't working as you would expect them. So still looking for best possible solution.
 
Can you perhaps link me to the exact ansible module or possible implementation (example). I have been trying with some level of success with cloud-init image, but some things aren't working as you would expect them. So still looking for best possible solution.
The search terms for your google search will be "qemu-guest-agent run command" and yields a ton of examples, including examples on the forums.
 
I managed to configure cloud-init configuration with ansible, but there seems to be another problem. There must be setting in cloud-init configuration files that prevents virtual machine to join local domain:

Works:
[root@vm~]#realm discover <local.domain>
returns domain

Works only on classic Almalinux 9.5 installation
[root@vm ~]# realm join <local.domain> -U <user> <path>
realm: Cannot join this realm
Please check
https://red.ht/support_rhel_ad
to get help for common issues.


It works fine on classical Almalinux 9.5, but not on cloud-generic image which is also Almalinux 9.5
I am assuming that there might be some setting which cloud-init configures, that influence joining domain. But can't pinpoint exact parameter/script.
For example ssh_pwauth: yes inside /etc/cloud/cloud.cfg enables ssh authentication with password, which was another setting that i didn't want with end configuration (default it was no).
 
Last edited:
I managed to configure cloud-init configuration with ansible, but there seems to be another problem. There must be setting in cloud-init configuration files that prevents virtual machine to join local domain:

Works:
[root@vm~]#realm discover <local.domain>
returns domain

Works only on classic Almalinux 9.5 installation
[root@vm ~]# realm join <local.domain> -U <user> <path>
realm: Cannot join this realm
Please check
https://red.ht/support_rhel_ad
to get help for common issues.


It works fine on classical Almalinux 9.5, but not on cloud-generic image which is also Almalinux 9.5
I am assuming that there might be some setting which cloud-init configures, that influence joining domain. But can't pinpoint exact parameter/script.
For example ssh_pwauth: yes inside /etc/cloud/cloud.cfg enables ssh authentication with password, which was another setting that i didn't want with end configuration (default it was no).
The problem was in subnet parameter (completely elsewhere).