Import_role and module_defaults

Hello,
I currently learn ansible and write a few roles for handling a proxmox cluster.
Each role uses several proxmox modules and each module needs the login data.

therefore I tried to create a role “prox_common” with the module_defaults in it:

# prox_common/tasks/main.yml:

- name: Define proxmox module defaults
  meta: noop

  module_defaults:
    community.general.proxmox_vm_info:
      api_host:         "{{ hostvars['pve1']['ansible_host'] }}"
      api_user:         "{{ hostvars['pve1']['proxmox_api_user'] }}"
      api_token_id:     "{{ hostvars['pve1']['proxmox_api_token_id'] }}"
      api_token_secret: "{{ hostvars['pve1']['proxmox_api_token_secret'] }}"
      node:             "{{ hostvars['pve1']['inventory_hostname_short'] }}"

  community.general.proxmox_kvm:
      api_host:         "{{ hostvars['pve1']['ansible_host'] }}"
      api_user:         "{{ hostvars['pve1']['proxmox_api_user'] }}"
      api_token_id:     "{{ hostvars['pve1']['proxmox_api_token_id'] }}"
      api_token_secret: "{{ hostvars['pve1']['proxmox_api_token_secret'] }}"
      node:             "{{ hostvars['pve1']['inventory_hostname_short'] }}"

I import that role into another role, but unfortunately the default values are not recocnized:

# prox_create_vm/tasks/main.yml

- import_role:
    name: prox_common

- name: Check status of VM
  delegate_to: pve1
  community.general.proxmox_vm_info:
    vmid: '{{ vm_id }}'
    config: current
  register: proxmox_vms

I get the Error: “missing required arguments: api_host, api_user”

How can I achive that I have the logindata only at one place?

Thanks a lot :slight_smile:

Thomas

1 Like

@DengelFred Hi.

How about specifying module_defaults at play level not task level?

- hosts: pve1
  module_defaults:
    community.general.proxmox_vm_info:
      api_host:         "{{ ansible_host }}"
      api_user:         "{{ proxmox_api_user }}"
      api_token_id:     "{{ proxmox_api_token_id }}"
      api_token_secret: "{{ proxmox_api_token_secret }}"
      node:             "{{ inventory_hostname_short }}"

  tasks:
    - name: Import prox_create_vm
      ansible.builtin.import_role:
        name: prox_create_vm

… because I don’t want the same block in each playbook. :wink:
Unfortunately it’s not only a single module for handling a proxmox cluster, the functionality is distributed accross several modules and each module needs the same defaults…

Unfortunately perhaps the current version does not allow for it.

Instead, I submitted a pull request to allow using module default group for community.general.proxmox_* modules.

2 Likes

There’s currently a plan to move the proxmox modules from community.general to a separate collection (community.proxmox). That collection will also have a module default group that should solve this problem :slight_smile:

2 Likes

CC @proxmox-collection

Due to the above reasons my PR will not be merged.

Wait for community.proxmox collectionto be released :grinning:

1 Like

ok, then I will wait for the release.

Thanks a lot!

@DengelFred

Then, how about using environment variables?

- hosts: proxmox
  environment:
    PROXMOX_HOST: proxmoxhost
    PROXMOX_USER: user
    PROXMOX_PASSWORD: passowrd

  tasks:
    - name: Gather VM Info
      community.general.proxmox_vm_info:
        type: qemu
        node: node01

    - name: List existing nodes
      community.general.proxmox_node_info:
      register: proxmox_nodes
1 Like

Thanks for the hint, but it seems that not all proxmox modules support PROXMOX_TOKEN_ID and PROXMOX_TOKEN_SECRET

Oops, my bad. Forgot to mention that you are not using passwords.

Unfortunately, PROXMOX_TOKEN_ID and PROXMOX_TOKEN_SECRET are not supported as you have indicated.

Let’s wait for the release of community.proxmox, after all.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.