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
Thomas
1 Like
akira6592
(Akira Yokochi)
March 9, 2024, 2:31am
2
@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.
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…
akira6592
(Akira Yokochi)
March 9, 2024, 11:43am
4
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.
ansible-collections:main
← akira6592:add-proxmox-action-group
opened 04:03AM - 09 Mar 24 UTC
##### SUMMARY
<!--- Please do not forget to include a changelog fragmen… t:
https://docs.ansible.com/ansible/devel/community/collection_development_process.html#creating-changelog-fragments
No need to include one for docs-only or test-only PR, and for new plugin/module PRs.
Read about more details in CONTRIBUTING.md.
-->
Added proxmox module defoult group.
https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_module_defaults.html#module-defaults-groups
This eliminates the need to specify common parameters such as `api_user` for each task.
As below:
```yaml
- hosts: proxmox
module_defaults:
group/community.general.proxmox:
api_host: proxmoxhost
api_user: root@pam
api_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
```
##### ISSUE TYPE
<!--- Pick one or more below and delete the rest.
'Test Pull Request' is for PRs that add/extend tests without code changes. -->
- Feature Pull Request
##### COMPONENT NAME
##### ADDITIONAL INFORMATION
Relateted forum topic https://forum.ansible.com/t/import-role-and-module-defaults/4277
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
2 Likes
akira6592
(Akira Yokochi)
March 9, 2024, 11:50am
7
Due to the above reasons my PR will not be merged.
Wait for community.proxmox
collectionto be released
1 Like
ok, then I will wait for the release.
Thanks a lot!
akira6592
(Akira Yokochi)
March 9, 2024, 12:06pm
9
@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
akira6592
(Akira Yokochi)
March 9, 2024, 1:00pm
11
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.
api_host=dict(type='str',
required=True,
fallback=(env_fallback, ['PROXMOX_HOST'])
),
api_user=dict(type='str',
required=True,
fallback=(env_fallback, ['PROXMOX_USER'])
),
api_password=dict(type='str',
no_log=True,
fallback=(env_fallback, ['PROXMOX_PASSWORD'])
),
api_token_id=dict(type='str',
no_log=False
),
api_token_secret=dict(type='str',
no_log=True
),
validate_certs=dict(type='bool',
default=False
),
Let’s wait for the release of community.proxmox
, after all.
1 Like
system
(system)
Closed
April 8, 2024, 1:00pm
12
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.