diff --git a/plugins/module_utils/proxmox_node.py b/plugins/module_utils/proxmox_node.py new file mode 100644 index 0000000..4bda14d --- /dev/null +++ b/plugins/module_utils/proxmox_node.py @@ -0,0 +1,11 @@ +from ansible_collections.finallycoffee.proxmox.plugins.module_utils.common import _proxmox_request, ProxmoxAuthInfo + + +def get_nodes(auth_info: ProxmoxAuthInfo) -> [dict]: + node_answer = _proxmox_request('get', f"/nodes", auth_info).json() + return node_answer['data'] + + +def get_node(auth_info: ProxmoxAuthInfo, node: str) -> [dict]: + node_answer = _proxmox_request('get', f"/nodes/{node}", auth_info).json() + return node_answer['data'] diff --git a/plugins/modules/node_info.py b/plugins/modules/node_info.py new file mode 100644 index 0000000..20af2f4 --- /dev/null +++ b/plugins/modules/node_info.py @@ -0,0 +1,115 @@ +#!/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 import * + +DOCUMENTATION = r''' +--- +module: node_info +author: +- Johanna Dorothea Reichmann (transcaffeine@finally.coffee) +requirements: + - python >= 3.9 +short_description: Get information about all proxmox cluster nodes +description: + - "Equivalent to /api2/json/nodes, returns all available top-level information about all visible nodes" +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 +''' + +EXAMPLES = r''' +- name: Retrieve all nodes + finallycoffee.proxmox.node_info: + proxmox_instance: https://my.proxmox-node.local:8006 + promox_api_token_id: root@pam!token + proxmox_api_secret: supersecuretokencontent + +- name: Retrieve info about node called "my_pm_host" + finallycoffee.proxmox.node_info: + proxmox_instance: https://my.proxmox-node.local:8006 + promox_api_token_id: root@pam!token + proxmox_api_secret: supersecuretokencontent + node: my_pm_hst +''' + +RETURN = r''' +nodes: + description: All nodes present in the datacenter + returned: When nodes are visible to the API + type: list + elements: dict[str, str] +node: + description: Specific node information when queried for a single node + returned: When a single node was queried and found + type: 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=False, type='str'), + ), + supports_check_mode=True + ) + + result = _( + changed=False, + diff={}, + message='' + ) + + nodes = [] + try: + nodes = get_nodes(ProxmoxAuthInfo( + module.params['proxmox_instance'], + module.params['proxmox_api_token_id'], + module.params['proxmox_api_secret'], + module.params['proxmox_api_verify_cert'], + )) + except IOError as owie: + result['msg'] = owie + module.exit_json(**result) + + result['nodes'] = nodes + if module.params['node'] is not None: + result['node'] = list(filter(lambda n: n.name == module.params['node'], nodes))[0] + + module.exit_json(**result) + + +if __name__ == '__main__': + main()