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 templating support in some inventory field #463

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
nutanix-ncp-*
__pycache__/
13 changes: 13 additions & 0 deletions examples/inventory/templating_1_nutanix.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
plugin: nutanix.ncp.ntnx_prism_vm_inventory
nutanix_hostname: "{{ lookup('ansible.builtin.env', 'PC_1_HOSTNAME') }}"
nutanix_username: "{{ lookup('ansible.builtin.env', 'PC_1_USERNAME') }}"
nutanix_password: "{{ lookup('ansible.builtin.env', 'PC_1_PASSWORD') }}"
validate_certs: false
data: {"offset": 0, "length": 1000}
groups:
group_1: "'<name_prefix>' in name"
group_2: "'<vm_name>'==name"
keyed_groups:
- prefix: "host"
separator: ':'
key: "ansible_host"
13 changes: 13 additions & 0 deletions examples/inventory/templating_2_nutanix.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
plugin: nutanix.ncp.ntnx_prism_vm_inventory
nutanix_hostname: "{{ lookup('community.hashi_vault.hashi_vault', 'secret=path/to/pc/secret:hostname') }}"
nutanix_username: "{{ lookup('community.hashi_vault.hashi_vault', 'secret=path/to/pc/secret:username') }}"
nutanix_password: "{{ lookup('community.hashi_vault.hashi_vault', 'secret=path/to/pc/secret:password') }}"
validate_certs: false
data: {"offset": 0, "length": 1000}
groups:
group_1: "'<name_prefix>' in name"
group_2: "'<vm_name>'==name"
keyed_groups:
- prefix: "host"
separator: ':'
key: "ansible_host"
29 changes: 25 additions & 4 deletions plugins/inventory/ntnx_prism_vm_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,33 @@
required: true
choices: ['ntnx_prism_vm_inventory', 'nutanix.ncp.ntnx_prism_vm_inventory']
nutanix_hostname:
description: Prism central hostname or IP address
description:
- Prism central hostname or IP address
- Accepts Jinja to template the value
required: true
type: str
env:
- name: NUTANIX_HOSTNAME
nutanix_username:
description: Prism central username
description:
- Prism central username
- Accepts Jinja to template the value
required: true
type: str
env:
- name: NUTANIX_USERNAME
nutanix_password:
description: Prism central password
description:
- Prism central password
- Accepts Jinja to template the value
required: true
type: str
env:
- name: NUTANIX_PASSWORD
nutanix_port:
description: Prism central port
description:
- Prism central port
- Accepts Jinja to template the value
default: 9440
type: str
env:
Expand Down Expand Up @@ -122,6 +130,19 @@ def parse(self, inventory, loader, path, cache=True):
# Determines if composed variables or groups using nonexistent variables is an error
strict = self.get_option("strict")

if self.templar.is_template(self.nutanix_hostname):
self.nutanix_hostname=self.templar.template(variable=self.nutanix_hostname,disable_lookups=False)

if self.templar.is_template(self.nutanix_username):
self.nutanix_username=self.templar.template(variable=self.nutanix_username,disable_lookups=False)

if self.templar.is_template(self.nutanix_password):
self.nutanix_password=self.templar.template(variable=self.nutanix_password,disable_lookups=False)

if self.templar.is_template(self.nutanix_port):
self.nutanix_port=self.templar.template(variable=self.nutanix_port,disable_lookups=False)


module = Mock_Module(
self.nutanix_hostname,
self.nutanix_port,
Expand Down