zabbix / ansible-collection

Zabbix Ansible Collection
14 stars 5 forks source link

extra vars not working with zabbix_inventory plugin for tags filtering #12

Open charlyforot opened 3 weeks ago

charlyforot commented 3 weeks ago

Hello,

The plugin inventory "zabbix_inventory" has an issue with the extra vars when using "filter" as parameter. Extra vars are not detected and used by the plugin.

Example with extra var:

use_extra_vars: true
plugin: zabbix.zabbix.zabbix_inventory
zabbix_api_url: "https://myzabbix.com"
validate_certs: true
query:
  selectInventory: ["model"]
filter:
  tags:
    - tag: Team
      value: "{{ team }}"
$ ansible-inventory --extra-vars team=test -i zabbix_inventory.yml --list
{
    "_meta": {
        "hostvars": {}
    },
    "all": {
        "children": [
            "ungrouped"
        ]
    }
}

Example without extra_var:

use_extra_vars: true
plugin: zabbix.zabbix.zabbix_inventory
zabbix_api_url: "https://myzabbix.com"
validate_certs: true
query:
  selectInventory: ["model"]
filter:
  tags:
    - tag: Team
      value: test
$ ansible-inventory -i zabbix_inventory.yml --list
{
    "_meta": {
        "hostvars": {
            "device1": {
                "team": "test",
                "zabbix_auto_compress": "1",
                "zabbix_custom_interfaces": "0",
[...]

I have tested extra vars into the query parameter and that works fine.

AleksandrKotsegubov commented 2 weeks ago

Hi, @charlyforot

Thank you for your attention to our collection!

In accordance with the issue, this functionality was added to the inventory plugin in this commit. 29b774c

Please note that this improvement is currently available only on Github. In Ansible Galaxy, this improvement will become available only after the next release of our collection, which will include this and other new features/improvements.

After updating the plugin, to use "extra-vars" you will need to meet 3 conditions:

  1. Add use_extra_vars: true to the inventory file or specify the use of extra-vars in the ansible configuration file.
  2. Specify a variable in the inventory file in 'Jinja' format. (e.g., zabbix_api_url: 'http://{{ url }}').
  3. Add --extra-vars or -e with the value in the command line. (e.g., --extra-vars url="your-zabbix.com").

Only if these 3 conditions are met, extra-vars will be expanded to their values.

In the inventory file you need to specify the variable in the format '{{ var_name }}', as you correctly did in the description. As short example:

---
plugin: "zabbix.zabbix.zabbix_inventory"

# Set the using of extra-vars
use_extra_vars: true

# Set credentials
zabbix_api_url: 'http://{{ url }}'
zabbix_user: Admin
zabbix_password: zabbix

And command for ansible-inventory: ansible-inventory --extra-vars url="your-zabbix.com" -i zabbix_inventory.yml --list

An example of using extra-vars can be found in more detail in the README for the inventory plugin.