[SOLVED] Proxmox VE API: provision LXC with two network interfaces

jsabater

Member
Oct 25, 2021
110
11
23
48
Palma, Mallorca, Spain
Hello everyone!

I am using the community.general.proxmox Ansible module to provision LinuX Containers into my Proxmox 7 cluster. At the moment I am using this value for the netif option:

YAML:
netif: {'net0':'name={{ proxmox_net_iface }},bridge={{ proxmox_net_bridge }},firewall=1,ip={{ ansible_host }}/{{ proxmox_net_mask }},mtu={{ proxmox_net_mtu }}
'}

Later in the playbook, if the LXC has a secondary, public IP address assigned, I am adding it via shell execution of pvesh:

YAML:
- name: Add a public IP address to the container
  ansible.builtin.command:
    cmd: "pvesh set /nodes/{{ proxmox_node }}/lxc/{{ proxmox_ctid }}/config --net1 'name=eth1,bridge=vmbr4001,firewall=1,ip={{ proxmox_public_ipv4_cidr }},gw={{ pro
xmox_public_ipv4_gw }},mtu={{ proxmox_public_net_mtu }}' --quiet"
  when: proxmox_public_ipv4_cidr is defined

I have tried to merge these two tasks into one by passing a dictionary with two entries to the netif option of the first task:

Code:
{
    'net0': 'name={{ proxmox_net_iface }},
             bridge={{ proxmox_net_bridge }},
             firewall=1,
             ip={{ ansible_host }}/{{ proxmox_net_mask }},
             mtu={{ proxmox_net_mtu }}',
    'net1': 'name={{ proxmox_public_net_iface }},
             bridge={{ proxmox_public_net_bridge }},
             firewall=1,
             ip={{ proxmox_public_ipv4_cidr }},
             gw={{ proxmox_public_ipv4_gw }},
             mtu={{ proxmox_public_net_mtu }}'
  }

My question is: Does the API support this, i.e. provision the container with two interfaces with just one call? If so, how should I do it? I was trying to take the WebGUI as reference but, since you can only add one interface, I could not copy the format from there.

Thanks in advance.
 
I don't know if the ansible module can deal with multiple interfaces, but the API definitely can. For example, if I use pvesh to create a basic container:
Code:
pvesh create nodes/nola/lxc \
--ostemplate iso:vztmpl/alpine-3.16-default_20220622_amd64.tar.xz \
--vmid 1111111 \
--net0 name=eth0,firewall=1,bridge=vmbr0 \
--net1 name=eth1,firewall=0,bridge=vmbr10 \
--rootfs tankvms:2
I end up with two NICs right away.
 
Thank you very much, Aaron.

After further investigation, some more help and lots of testing, I found that the iface parametre of the community.general.proxmox module does support a dictionary with multiple keys indeed. So this is what I am using in the end (simplified version):

YAML:
# provision.yml snippet
netif:
  net0: "{{ lookup('template', 'netif0.j2') }}"
  net1: "{{ lookup('template', 'netif1.j2') if proxmox_public_ipv4_cidr is defined else omit }}"

# netif0.j2
name={{ proxmox_net_iface }},bridge={{ proxmox_net_bridge }},firewall=1,ip={{ ansible_host }}/{{ proxmox_net_mask }},mtu={{ proxmox_net_mtu }}

# netif1.j2
name={{ proxmox_public_net_iface }},bridge={{ proxmox_public_net_bridge }},firewall=1,ip={{ proxmox_public_ipv4_cidr }},gw={{ proxmox_public_ipv4_gw }},mtu={{ proxmox_public_net_mtu }}

I will mark the thread as solved so that other people can use it as reference.

Cheers!
 

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!