#!/usr/bin/python # coding: utf-8 # (c) 2022, Johanna Dorothea Reichmann __metaclass__ = type import json from ansible.module_utils.basic import AnsibleModule from ansible_collections.finallycoffee.proxmox.plugins.module_utils.common import * from ansible_collections.finallycoffee.proxmox.plugins.module_utils.proxmox_node_iface import * DOCUMENTATION = r''' --- module: node_interface author: - Johanna Dorothea Reichmann (transcaffeine@finally.coffee) requirements: - python >= 3.9 short_description: Configures an authentication realm in a proxmox node description: - "Configue realm in a proxmox instance" options: proxmox_instance: description: Location of the proxmox API with scheme, domain name/ip and port, e.g. https://localhost:8006 type: str required: true proxmox_api_token_id: description: The token ID containing username, realm and token name (format: user@realm!name) type: str required: true proxmox_api_secret: description: The secret type: str required: true proxmox_api_verify_cert: description: If the certificate presented for `proxmox_instance_url` should be verified type: bool required: false default: true node: description: Node to retrieve information about. type: str required: true name: description: Interface to retrieve information about. If left omitted, return all realms type: str required: false config: description: Configuration of the interface, see https://pve.proxmox.com/pve-docs/api-viewer/index.html#/nodes/{node}/network/{iface} for parameters type: dict[str, str] required: false state: description: If the realm should be present or absent type: str choices: [present, absent] default: present required: false ''' EXAMPLES = r''' - name: Add and configure a new interface of type 'bond' on node 'hostname' finallycoffee.proxmox.node_interface: proxmox_instance: https://my.proxmox-node.local:8006 promox_api_token_id: root@pam!token proxmox_api_secret: supersecuretokencontent node: hostname name: my_bond_iface type: bond config: bond_mode: balance-rr bond-primary: enp0s1 config: slaves: enp0s1 enp0s2 enp0s3 - name: Change CIDR range on interface 'vmbr0' on 'other_pm_host' finallycoffee.proxmox.node_interface: proxmox_instance: https://my.proxmox-node.local:8006 promox_api_token_id: root@pam!token proxmox_api_secret: supersecuretokencontent node: other_pm_host name: vmbr0 type: bridge config: cidr: "1.2.3.4/8" ''' RETURN = r''' interfaces: description: The retrieved interfaces on the specified nodes returned: When interfaces are present on a node type: list elements: dict[str, str] ''' def main(): _ = dict module = AnsibleModule( argument_spec=_( proxmox_instance=_(required=True, type='str'), proxmox_api_token_id=_(required=True, type='str'), proxmox_api_secret=_(type='str', required=True, no_log=True), proxmox_api_verify_cert=_(type='bool', required=False, default=True), node=_(required=True, type='str'), name=_(required_if=['state', 'present'], type='str'), type=_(type='str', choices=PM_ACCEPTED_IFACE_TYPES), config=_(required_if=['state', 'present'], type='dict'), state=_(type='str', required=False, default='present', choices=['present', 'absent']) ), supports_check_mode=True ) result = _( changed=False, diff={}, message='' ) realms = [] try: interfaces = set_interface_config(ProxmoxAuthInfo( module.params['proxmox_instance'], module.params['proxmox_api_token_id'], module.params['proxmox_api_secret'], module.params['proxmox_api_verify_cert'], ), module.params['node'], module.params['name'], module.params['type'], module.params['config'], module.check_mode, module.params['state']) except IOError as owie: result['msg'] = owie module.exit_json(**result) result['interfaces'] = interfaces diff_rendered = _render_diff(interfaces[0], interfaces[1]) result['diff'] = { 'before_header': _render_diff_header(interfaces[0], module), 'after_header': _render_diff_header(interfaces[1], module), 'before': diff_rendered[0], 'after': diff_rendered[1], } result['changed'] = interfaces[0] != interfaces[1] module.exit_json(**result) def _render_diff_header(iface: dict[str, str], module: AnsibleModule) -> str: return (f"{iface.get('name', module.params['name'])}@{iface.get('node', module.params['node'])}" if iface else '') def _render_diff(before: dict[str, str] | None, after: dict[str, str] | None) -> (str, str): is_update: bool = (before and after) after = after or {} before = before or {} diff_excluded_keys = {'name', 'digest', 'node', 'iface'} after_filtered = {k: v for k, v in after.items() if k not in diff_excluded_keys} before_filtered = {k: v for k, v in before.items() if k not in diff_excluded_keys} before_clean = {k: v for k, v in before_filtered.items() if (k in after_filtered.keys() if is_update else True)} before_str = "\n".join([f"{k}: {v}" for k, v in sorted(before_clean.items(), key=lambda e: e[0])]) after_str = "\n".join([f"{k}: {v}" for k, v in sorted(after_filtered.items(), key=lambda e: e[0])]) return before_str + ("\n" if before_str else ''), after_str + ("\n" if after_str else '') if __name__ == '__main__': main()