2021-05-13 21:48:49 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
2022-02-07 06:21:24 +01:00
|
|
|
#
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2021, Lammert Hellinga (@Kogelvis) <lammert@hellinga.it>
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2021-05-13 21:48:49 +02:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = r'''
|
|
|
|
---
|
|
|
|
module: proxmox_nic
|
2022-11-09 14:11:41 +01:00
|
|
|
short_description: Management of a NIC of a Qemu(KVM) VM in a Proxmox VE cluster
|
2021-05-13 21:48:49 +02:00
|
|
|
version_added: 3.1.0
|
|
|
|
description:
|
|
|
|
- Allows you to create/update/delete a NIC on Qemu(KVM) Virtual Machines in a Proxmox VE cluster.
|
|
|
|
author: "Lammert Hellinga (@Kogelvis) <lammert@hellinga.it>"
|
|
|
|
options:
|
|
|
|
bridge:
|
|
|
|
description:
|
|
|
|
- Add this interface to the specified bridge device. The Proxmox VE default bridge is called C(vmbr0).
|
|
|
|
type: str
|
|
|
|
firewall:
|
|
|
|
description:
|
|
|
|
- Whether this interface should be protected by the firewall.
|
|
|
|
type: bool
|
|
|
|
default: false
|
|
|
|
interface:
|
|
|
|
description:
|
|
|
|
- Name of the interface, should be C(net[n]) where C(1 ≤ n ≤ 31).
|
|
|
|
type: str
|
|
|
|
required: true
|
|
|
|
link_down:
|
|
|
|
description:
|
|
|
|
- Whether this interface should be disconnected (like pulling the plug).
|
|
|
|
type: bool
|
|
|
|
default: false
|
|
|
|
mac:
|
|
|
|
description:
|
|
|
|
- C(XX:XX:XX:XX:XX:XX) should be a unique MAC address. This is automatically generated if not specified.
|
|
|
|
- When not specified this module will keep the MAC address the same when changing an existing interface.
|
|
|
|
type: str
|
|
|
|
model:
|
|
|
|
description:
|
|
|
|
- The NIC emulator model.
|
|
|
|
type: str
|
|
|
|
choices: ['e1000', 'e1000-82540em', 'e1000-82544gc', 'e1000-82545em', 'i82551', 'i82557b', 'i82559er', 'ne2k_isa', 'ne2k_pci', 'pcnet',
|
|
|
|
'rtl8139', 'virtio', 'vmxnet3']
|
|
|
|
default: virtio
|
|
|
|
mtu:
|
|
|
|
description:
|
|
|
|
- Force MTU, for C(virtio) model only, setting will be ignored otherwise.
|
|
|
|
- Set to C(1) to use the bridge MTU.
|
|
|
|
- Value should be C(1 ≤ n ≤ 65520).
|
|
|
|
type: int
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Specifies the VM name. Only used on the configuration web interface.
|
|
|
|
- Required only for I(state=present).
|
|
|
|
type: str
|
|
|
|
queues:
|
|
|
|
description:
|
|
|
|
- Number of packet queues to be used on the device.
|
|
|
|
- Value should be C(0 ≤ n ≤ 16).
|
|
|
|
type: int
|
|
|
|
rate:
|
|
|
|
description:
|
|
|
|
- Rate limit in MBps (MegaBytes per second) as floating point number.
|
|
|
|
type: float
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Indicates desired state of the NIC.
|
|
|
|
type: str
|
|
|
|
choices: ['present', 'absent']
|
|
|
|
default: present
|
|
|
|
tag:
|
|
|
|
description:
|
|
|
|
- VLAN tag to apply to packets on this interface.
|
|
|
|
- Value should be C(1 ≤ n ≤ 4094).
|
|
|
|
type: int
|
|
|
|
trunks:
|
|
|
|
description:
|
|
|
|
- List of VLAN trunks to pass through this interface.
|
|
|
|
type: list
|
|
|
|
elements: int
|
|
|
|
vmid:
|
|
|
|
description:
|
|
|
|
- Specifies the instance ID.
|
|
|
|
type: int
|
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.proxmox.documentation
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Create NIC net0 targeting the vm by name
|
|
|
|
community.general.proxmox_nic:
|
|
|
|
api_user: root@pam
|
|
|
|
api_password: secret
|
|
|
|
api_host: proxmoxhost
|
|
|
|
name: my_vm
|
|
|
|
interface: net0
|
|
|
|
bridge: vmbr0
|
|
|
|
tag: 3
|
|
|
|
|
|
|
|
- name: Create NIC net0 targeting the vm by id
|
|
|
|
community.general.proxmox_nic:
|
|
|
|
api_user: root@pam
|
|
|
|
api_password: secret
|
|
|
|
api_host: proxmoxhost
|
|
|
|
vmid: 103
|
|
|
|
interface: net0
|
|
|
|
bridge: vmbr0
|
|
|
|
mac: "12:34:56:C0:FF:EE"
|
|
|
|
firewall: true
|
|
|
|
|
|
|
|
- name: Delete NIC net0 targeting the vm by name
|
|
|
|
community.general.proxmox_nic:
|
|
|
|
api_user: root@pam
|
|
|
|
api_password: secret
|
|
|
|
api_host: proxmoxhost
|
|
|
|
name: my_vm
|
|
|
|
interface: net0
|
|
|
|
state: absent
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
vmid:
|
|
|
|
description: The VM vmid.
|
|
|
|
returned: success
|
|
|
|
type: int
|
|
|
|
sample: 115
|
|
|
|
msg:
|
|
|
|
description: A short message
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: "Nic net0 unchanged on VM with vmid 103"
|
|
|
|
'''
|
|
|
|
|
2022-09-09 22:23:48 +02:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2022-02-07 06:21:24 +01:00
|
|
|
from ansible_collections.community.general.plugins.module_utils.proxmox import (proxmox_auth_argument_spec, ProxmoxAnsible)
|
2021-05-13 21:48:49 +02:00
|
|
|
|
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
class ProxmoxNicAnsible(ProxmoxAnsible):
|
|
|
|
def update_nic(self, vmid, interface, model, **kwargs):
|
|
|
|
vm = self.get_vm(vmid)
|
2021-05-13 21:48:49 +02:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
try:
|
|
|
|
vminfo = self.proxmox_api.nodes(vm['node']).qemu(vmid).config.get()
|
|
|
|
except Exception as e:
|
|
|
|
self.module.fail_json(msg='Getting information for VM with vmid = %s failed with exception: %s' % (vmid, e))
|
2021-05-13 21:48:49 +02:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
if interface in vminfo:
|
|
|
|
# Convert the current config to a dictionary
|
|
|
|
config = vminfo[interface].split(',')
|
|
|
|
config.sort()
|
2021-05-13 21:48:49 +02:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
config_current = {}
|
2021-05-13 21:48:49 +02:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
for i in config:
|
|
|
|
kv = i.split('=')
|
|
|
|
try:
|
|
|
|
config_current[kv[0]] = kv[1]
|
|
|
|
except IndexError:
|
|
|
|
config_current[kv[0]] = ''
|
2021-05-13 21:48:49 +02:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
# determine the current model nic and mac-address
|
|
|
|
models = ['e1000', 'e1000-82540em', 'e1000-82544gc', 'e1000-82545em', 'i82551', 'i82557b',
|
|
|
|
'i82559er', 'ne2k_isa', 'ne2k_pci', 'pcnet', 'rtl8139', 'virtio', 'vmxnet3']
|
|
|
|
current_model = set(models) & set(config_current.keys())
|
|
|
|
current_model = current_model.pop()
|
|
|
|
current_mac = config_current[current_model]
|
2021-05-13 21:48:49 +02:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
# build nic config string
|
|
|
|
config_provided = "{0}={1}".format(model, current_mac)
|
|
|
|
else:
|
|
|
|
config_provided = model
|
2021-05-13 21:48:49 +02:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
if kwargs['mac']:
|
|
|
|
config_provided = "{0}={1}".format(model, kwargs['mac'])
|
2021-05-13 21:48:49 +02:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
if kwargs['bridge']:
|
|
|
|
config_provided += ",bridge={0}".format(kwargs['bridge'])
|
2021-05-13 21:48:49 +02:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
if kwargs['firewall']:
|
|
|
|
config_provided += ",firewall=1"
|
2021-05-13 21:48:49 +02:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
if kwargs['link_down']:
|
|
|
|
config_provided += ',link_down=1'
|
2021-05-13 21:48:49 +02:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
if kwargs['mtu']:
|
|
|
|
config_provided += ",mtu={0}".format(kwargs['mtu'])
|
|
|
|
if model != 'virtio':
|
|
|
|
self.module.warn(
|
|
|
|
'Ignoring MTU for nic {0} on VM with vmid {1}, '
|
|
|
|
'model should be set to \'virtio\': '.format(interface, vmid))
|
2021-05-13 21:48:49 +02:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
if kwargs['queues']:
|
|
|
|
config_provided += ",queues={0}".format(kwargs['queues'])
|
2021-05-13 21:48:49 +02:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
if kwargs['rate']:
|
|
|
|
config_provided += ",rate={0}".format(kwargs['rate'])
|
2021-05-13 21:48:49 +02:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
if kwargs['tag']:
|
|
|
|
config_provided += ",tag={0}".format(kwargs['tag'])
|
2021-05-13 21:48:49 +02:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
if kwargs['trunks']:
|
|
|
|
config_provided += ",trunks={0}".format(';'.join(str(x) for x in kwargs['trunks']))
|
2021-05-13 21:48:49 +02:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
net = {interface: config_provided}
|
|
|
|
vm = self.get_vm(vmid)
|
2021-05-13 21:48:49 +02:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
if ((interface not in vminfo) or (vminfo[interface] != config_provided)):
|
|
|
|
if not self.module.check_mode:
|
|
|
|
self.proxmox_api.nodes(vm['node']).qemu(vmid).config.set(**net)
|
|
|
|
return True
|
2021-05-13 21:48:49 +02:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
return False
|
2021-05-13 21:48:49 +02:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
def delete_nic(self, vmid, interface):
|
|
|
|
vm = self.get_vm(vmid)
|
|
|
|
vminfo = self.proxmox_api.nodes(vm['node']).qemu(vmid).config.get()
|
2021-05-13 21:48:49 +02:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
if interface in vminfo:
|
|
|
|
if not self.module.check_mode:
|
|
|
|
self.proxmox_api.nodes(vm['node']).qemu(vmid).config.set(vmid=vmid, delete=interface)
|
|
|
|
return True
|
2021-05-13 21:48:49 +02:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
return False
|
2021-05-13 21:48:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module_args = proxmox_auth_argument_spec()
|
|
|
|
nic_args = dict(
|
|
|
|
bridge=dict(type='str'),
|
|
|
|
firewall=dict(type='bool', default=False),
|
|
|
|
interface=dict(type='str', required=True),
|
|
|
|
link_down=dict(type='bool', default=False),
|
|
|
|
mac=dict(type='str'),
|
|
|
|
model=dict(choices=['e1000', 'e1000-82540em', 'e1000-82544gc', 'e1000-82545em',
|
|
|
|
'i82551', 'i82557b', 'i82559er', 'ne2k_isa', 'ne2k_pci', 'pcnet',
|
|
|
|
'rtl8139', 'virtio', 'vmxnet3'], default='virtio'),
|
|
|
|
mtu=dict(type='int'),
|
|
|
|
name=dict(type='str'),
|
|
|
|
queues=dict(type='int'),
|
|
|
|
rate=dict(type='float'),
|
|
|
|
state=dict(default='present', choices=['present', 'absent']),
|
|
|
|
tag=dict(type='int'),
|
|
|
|
trunks=dict(type='list', elements='int'),
|
|
|
|
vmid=dict(type='int'),
|
|
|
|
)
|
|
|
|
module_args.update(nic_args)
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=module_args,
|
|
|
|
required_together=[('api_token_id', 'api_token_secret')],
|
|
|
|
required_one_of=[('name', 'vmid'), ('api_password', 'api_token_id')],
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
proxmox = ProxmoxNicAnsible(module)
|
2021-05-13 21:48:49 +02:00
|
|
|
|
|
|
|
interface = module.params['interface']
|
|
|
|
model = module.params['model']
|
|
|
|
name = module.params['name']
|
|
|
|
state = module.params['state']
|
|
|
|
vmid = module.params['vmid']
|
|
|
|
|
|
|
|
# If vmid is not defined then retrieve its value from the vm name,
|
|
|
|
if not vmid:
|
2022-02-07 06:21:24 +01:00
|
|
|
vmid = proxmox.get_vmid(name)
|
2021-05-13 21:48:49 +02:00
|
|
|
|
|
|
|
# Ensure VM id exists
|
2022-02-07 06:21:24 +01:00
|
|
|
proxmox.get_vm(vmid)
|
2021-05-13 21:48:49 +02:00
|
|
|
|
|
|
|
if state == 'present':
|
|
|
|
try:
|
2022-02-07 06:21:24 +01:00
|
|
|
if proxmox.update_nic(vmid, interface, model,
|
|
|
|
bridge=module.params['bridge'],
|
|
|
|
firewall=module.params['firewall'],
|
|
|
|
link_down=module.params['link_down'],
|
|
|
|
mac=module.params['mac'],
|
|
|
|
mtu=module.params['mtu'],
|
|
|
|
queues=module.params['queues'],
|
|
|
|
rate=module.params['rate'],
|
|
|
|
tag=module.params['tag'],
|
|
|
|
trunks=module.params['trunks']):
|
2021-05-13 21:48:49 +02:00
|
|
|
module.exit_json(changed=True, vmid=vmid, msg="Nic {0} updated on VM with vmid {1}".format(interface, vmid))
|
|
|
|
else:
|
|
|
|
module.exit_json(vmid=vmid, msg="Nic {0} unchanged on VM with vmid {1}".format(interface, vmid))
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(vmid=vmid, msg='Unable to change nic {0} on VM with vmid {1}: '.format(interface, vmid) + str(e))
|
|
|
|
|
|
|
|
elif state == 'absent':
|
|
|
|
try:
|
2022-02-07 06:21:24 +01:00
|
|
|
if proxmox.delete_nic(vmid, interface):
|
2021-05-13 21:48:49 +02:00
|
|
|
module.exit_json(changed=True, vmid=vmid, msg="Nic {0} deleted on VM with vmid {1}".format(interface, vmid))
|
|
|
|
else:
|
|
|
|
module.exit_json(vmid=vmid, msg="Nic {0} does not exist on VM with vmid {1}".format(interface, vmid))
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(vmid=vmid, msg='Unable to delete nic {0} on VM with vmid {1}: '.format(interface, vmid) + str(e))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|