community.general icon indicating copy to clipboard operation
community.general copied to clipboard

Proxmox Inventory

Open Nouhailakermouss opened this issue 3 years ago • 15 comments

Summary

Proxmox inventory role have been created as the example in this documentation https://docs.ansible.com/ansible/latest/collections/community/general/proxmox_inventory.html my question is how could i filter groups and hosts in order to get only the hosts that contain a specific tag (i need to work only with some hosts that contains specific tags and i want the proxmox inventory to bring only this hosts in AWX) if there any possibility to add "filters" paramter as vmware inventory

Issue Type

Bug Report

Component Name

inventory

Ansible Version

ansible --version 2.11.2

Community.general Version

`2.9

Configuration

$ ansible-config dump --only-changed

OS / Environment

No response

Steps to Reproduce

plugin: community.general.proxmox validate_certs: no want_facts: yes keyed_groups:

key: ansible_controlled separator: "" prefix: ansible

Expected Results

i expect to have only the hosts with the specific tags with ansible-inventory

Actual Results

the inventory bring all groups and hosts

Code of Conduct

  • [X] I agree to follow the Ansible Code of Conduct

Nouhailakermouss avatar Oct 12 '21 16:10 Nouhailakermouss

Files identified in the description:

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

click here for bot help

ansibullbot avatar Oct 12 '21 16:10 ansibullbot

cc @Charliekenney23 @InTheCloudDan @LBGarber @decentral1se @displague @rmcintosh click here for bot help

ansibullbot avatar Oct 12 '21 16:10 ansibullbot

!component =plugins/inventory/proxmox.py

felixfontein avatar Oct 12 '21 16:10 felixfontein

Files identified in the description:

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

click here for bot help

ansibullbot avatar Oct 12 '21 16:10 ansibullbot

cc @Ajpantuso @Thulium-Drake @ilijamt @joshainglis @karmab @tleguern click here for bot help

ansibullbot avatar Oct 12 '21 16:10 ansibullbot

+1

ElZakariyae avatar Oct 12 '21 17:10 ElZakariyae

I'm thinking of adding a flag to the options and filter out the proxmox predefined groups. please let me know if we collaborate on this. I'll be running tests locally and then when the solution is tested and fully functional I'll send a PR

SabrineMihni avatar Oct 14 '21 14:10 SabrineMihni

Yes currently there is no filtering on the inventory. You can always run ansible with a limit on a tag after the inventory has been pulled in.

It's not just as simple as adding a filters' property. We will need to figure something out like

filters:
  # All instances with their `Environment` tag set to `dev`
  tag:Environment: dev
  # All dev and QA hosts
  tag:Environment:
    - dev
    - qa

ilijamt avatar Oct 14 '21 16:10 ilijamt

we are using awx so we can't really add an argument to filter out the groups, we're trying to automate as much as possible. let me know if you know how I can start or if I can be of any help to this. cheers

SabrineMihni avatar Oct 14 '21 17:10 SabrineMihni

I also use AWX, and we can do the filtering based on the tags in the inventory. I just make sure that my workflow/template has a limit set to the correct tag, and in Proxmox all my VM's should have the correct tag associated with them.

Here is a real example from my systems

Part of the ansible inventory:

  |--@k8s:
  |  |--k8s-n01.example.com
  |  |--k8s-n02.example.com
  |  |--k8s-n03.example.com
  |  |--k8s-n04.example.com
  |--@internal:
  |  |--k8s-n01.example.com
  |  |--k8s-n02.example.com
  |  |--k8s-n03.example.com
  |  |--k8s-n04.example.com
  |--@php74:
  |  |--www.example.com

And a qm config for one of the machines

$ qm config 10041002
agent: 1,type=virtio
balloon: 0
bootdisk: scsi0
cores: 4
ide2: none,media=cdrom
memory: 8192
name: k8s-n02.example.com
net0: virtio=ff:ff:ff:ff:ff:ff,bridge=vmbr0
net1: virtio=ff:ff:ff:ff:ff:ff,bridge=vmbr4000
numa: 0
onboot: 1
ostype: l26
scsi0: storage:vm-10041002-disk-0,size=10G
scsi1: storage:vm-10041002-disk-3,size=30G
scsi2: storage:vm-10041002-disk-2,size=10G
scsihw: virtio-scsi-pci
serial0: socket
smbios1: uuid=863b31c3-42ca-4a92-aed7-4111f342f70a
sockets: 1
tablet: 0
tags: k8s,internal
vmgenid: ddfb79b2-b484-4d66-88e7-6e76f2d1be77

ilijamt avatar Oct 14 '21 17:10 ilijamt

thank you so much for your quick feedback, truly appreciated. I'll check that out. cheers

SabrineMihni avatar Oct 14 '21 18:10 SabrineMihni

One thing I forgot if you want to add a tag to a VM you can do so by doing from the command line

qm set 10041002 -tags "k8s,internal"

ilijamt avatar Oct 14 '21 18:10 ilijamt

I think the vmware inventory can be used for inspiration. Their filter definition looks like this:

filters:
- runtime.powerState == "poweredOn"
- "tag_category.ansible is defined and 'managed' in tag_category.ansible"

Of course proxmox doesn't have (I think) tag categories but still composing filters like this would be awesome.

filviu avatar Nov 08 '21 10:11 filviu

@Nouhailakermouss After reading some issue's I came up with this working configuration:

...
filters:
  - proxmox_status == "running"
  - "proxmox_tags_parsed is defined and 'ansible' in proxmox_tags_parsed"
...

It only return qemu_vm and lxc ressource in running state which have the tag ansible (and of course further groups to be used with ansible itself)

meyayl avatar Jun 18 '22 22:06 meyayl

cc @krauthosting click here for bot help

ansibullbot avatar Feb 17 '24 12:02 ansibullbot