Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add jinja2 template option to inventory plugin for auth_token #1342

Merged
merged 3 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading