Repository: ansible-collections/community.general

Issue:
proxmox_kvm: only root can set 'args' config error even if args is null
##### SUMMARY
Set `proxmox_default_behavior: no_defaults`, do not set an args option (debug shows `"args": null,`)
It is not possible to create a kvm due to the following error: 500 error `only root can set 'args' config`

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
proxmox_kvm

##### ANSIBLE VERSION
```paste below
2.10.1
```

##### OS / ENVIRONMENT
Ubuntu 18.04.5 LTS


##### STEPS TO REPRODUCE
<!--- Describe exactly how to reproduce the problem, using a minimal test-case -->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: Create VM
  community.general.proxmox_kvm:
    api_host: xx
    api_user: xx
    api_token_id: xx
    api_token_secret: xx
    proxmox_default_behavior: no_defaults
    name: test
    sockets: 1
    cores: 1
    memory: 2048
    node: node1
    vmid: 100
delegate_to: localhost
```

##### EXPECTED RESULTS
VM should be created


##### ACTUAL RESULTS


<!--- Paste verbatim command output between quotes -->
```paste below
...

"invocation": {
        "module_args": {
            "acpi": null,
            "agent": null,
            "api_host": "xx",
            "api_password": null,
            "api_token_id": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "api_token_secret": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "api_user": "xx",
            "args": null,

...

"msg": "creation of qemu VM test with vmid 100 failed with exception=500 Internal Server Error: only root can set 'args' config - b'{\"data\":null}'"
```



Discussion:
Files identified in the description:
* [`plugins/modules/cloud/misc/proxmox_kvm.py`](https://github.com/ansible-collections/community.general/blob/master/plugins/modules/cloud/misc/proxmox_kvm.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @Aversiste @Thulium-Drake @helldorado @joshainglis @karmab
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Never mind, I updated the wrong issue.. Ignore the PR
Any chance you're using the api_user = root combined with some api_token? 
For whatever reason proxmox restricts a couple of settings to the root user. 
And it seems that having, like in my case, an api_token for the root@pam user is not equal to the "root" user. 
They probably want you to set those settings through the cli or whatever. 

Sadly the ansible module does not seem to take care of this specific scenario in lines 951 to 961... 

Say, if you had a user with a different name than "root@pam" which has the exact same permissions, 
then everything would work because the internal checks in the module will not add the 'args' paramter then.

I kinda helped myself by adding this to the proxmox_kvm.py in line 963:

```python
    if module.params['api_user'] != "root@pam" and module.params['skiplock'] is not None:
        module.fail_json(msg='skiplock parameter require root@pam user. ')

    # changed by almi
    if module.params['api_user'] == "root@pam" and module.params['api_token_id'] is not None:
        del kwargs['args']

    if update:
        if proxmox_node.qemu(vmid).config.set(name=name, memory=memory, cpu=cpu, cores=cores, sockets=sockets, **kwargs) is None:
            return True
        else:
            return False
```

Not yet friendly with coding in python so someone else might be better suited to put this into a stable pull request :)
But for now i can deploy vms with this...

I posted a request with the proxmox team to verify which params are affected by the 500 error if root account is used but authenticated by a token instead of a password: (thread is currently being reviewed before publicly available)
https://forum.proxmox.com/threads/root-pam-token-api-restricted.83866/

Once i get the confirmation i would try to provide a pull request to make the error message more descriptive... module.fail_json or something like that.

Resolving patch:
diff --git a/plugins/modules/cloud/misc/proxmox_kvm.py b/plugins/modules/cloud/misc/proxmox_kvm.py
--- a/plugins/modules/cloud/misc/proxmox_kvm.py
+++ b/plugins/modules/cloud/misc/proxmox_kvm.py
@@ -31,6 +31,9 @@
     description:
       - Pass arbitrary arguments to kvm.
       - This option is for experts only!
+      - If I(proxmox_default_behavior) is set to C(compatiblity) (the default value), this
+        option has a default of C(-serial unix:/var/run/qemu-server/<vmid>.serial,server,nowait).
+        Note that the default value of I(proxmox_default_behavior) changes in community.general 4.0.0.
     type: str
   autostart:
     description:
@@ -925,9 +928,9 @@ def create_vm(module, proxmox, vmid, newid, node, name, memory, cpu, cores, sock
         if searchdomains:
             kwargs['searchdomain'] = ' '.join(searchdomains)
 
-    # -args and skiplock require root@pam user
+    # -args and skiplock require root@pam user - but can not use api tokens
     if module.params['api_user'] == "root@pam" and module.params['args'] is None:
-        if not update:
+        if not update and module.params['proxmox_default_behavior'] == 'compatibility':
             kwargs['args'] = vm_args
     elif module.params['api_user'] == "root@pam" and module.params['args'] is not None:
         kwargs['args'] = module.params['args']
@@ -1118,7 +1121,6 @@ def main():
             cores=1,
             cpu='kvm64',
             cpuunits=1000,
-            force=False,
             format='qcow2',
             kvm=True,
             memory=512,


Test patch: