Skip to content

Commit

Permalink
Add jinja2 template option to inventory plugin for auth_token (#1342)
Browse files Browse the repository at this point in the history
  • Loading branch information
erichoog authored Jul 15, 2024
1 parent dd26efa commit 9ac5130
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelogs/fragments/1342.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- zabbix_inventory Plugin - Add support for jinja2 templating for auth_token in zabbix_inventory.yml
12 changes: 11 additions & 1 deletion plugins/inventory/zabbix_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@
auth_token: 3bc3dc85e13e2431812e7a32fa8341cbcf378e5101356c015fdf2e35fd511b06
validate_certs: false
#Using jinga2 template for auth token instead of username/password.
plugin: community.zabbix.zabbix_inventory
server_url: https://zabbix.com
auth_token: "{{ lookup('ansible.builtin.env', 'ZABBIX_API_KEY') }}"
validate_certs: false
"""

from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable, to_safe_group_name
Expand Down Expand Up @@ -378,7 +383,7 @@ def logout_zabbix(self):
)

def login_zabbix(self):
auth_token = self.get_option('auth_token')
auth_token = self._template_option('auth_token')
if auth_token:
self.auth = auth_token
return
Expand Down Expand Up @@ -463,3 +468,8 @@ def parse(self, inventory, loader, path,
group_name = to_safe_group_name(group['name'])
self.inventory.add_group(group_name)
self.inventory.add_child(group_name, host_name)

def _template_option(self, option):
value = self.get_option(option)
self.templar.available_variables = {}
return self.templar.template(value)

0 comments on commit 9ac5130

Please sign in to comment.