Last active
May 7, 2023 06:19
-
-
Save gowatana/6ccf3e814eecbe82ce7e425e18c2d6eb to your computer and use it in GitHub Desktop.
ansible dynamic inventory script for Nutanix Prism
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
import json | |
import requests | |
# args = sys.argv | |
# conf_file = args[1] | |
conf_file = "/home/gowatana/prism-config.json" | |
def get_all_vm_inventory(): | |
with open(conf_file, "r") as file: | |
conf = file.read() | |
conf = json.loads(conf) | |
prism_addr = conf["prism_address"] | |
prism_user = conf["user_name"] | |
prism_pass = conf["password"] | |
base_url = 'https://' + prism_addr + ':9440/PrismGateway/services/rest/v2.0/' | |
insecure_warn = requests.packages.urllib3.exceptions.InsecureRequestWarning | |
requests.packages.urllib3.disable_warnings(insecure_warn) | |
s = requests.Session() | |
s.auth = (prism_user, prism_pass) | |
s.headers.update({'Content-Type': 'application/json; charset=utf-8'}) | |
# Get VM. | |
url = base_url + 'vms/?include_vm_nic_config=true' | |
vms = s.get(url, verify=False).json() | |
inventory = { | |
"all":{"hosts": []}, | |
"poweron_vms":{"hosts": []}, | |
"poweroff_vms":{"hosts": []}, | |
"_meta": { | |
"hostvars": {} | |
} | |
} | |
vm_entities = [] | |
hosts = {} | |
for vm in vms['entities']: | |
#print(json.dumps(vm, indent=2)) | |
vm_name = vm['name'] | |
power_state = vm['power_state'] | |
if(vm['power_state'] == 'on'): | |
inventory["poweron_vms"]["hosts"].append(vm_name) | |
else: | |
inventory["poweroff_vms"]["hosts"].append(vm_name) | |
inventory["all"]["hosts"].append(vm_name) | |
if(vm['vm_nics']): | |
vm_nic = vm['vm_nics'][0] | |
ip_address = vm_nic.get('ip_address') | |
requested_ip_address = vm_nic.get('requested_ip_address') | |
if (ip_address == None): | |
ip_address = vm_nic.get('requested_ip_address') | |
if ip_address: | |
inventory["_meta"]["hostvars"][vm_name] = {"ansible_host": ip_address, "power_state": power_state} | |
else: | |
inventory["_meta"]["hostvars"][vm_name] = {} | |
return(inventory) | |
if __name__ == '__main__': | |
hostname = None | |
inventory = get_all_vm_inventory() | |
if len(sys.argv) > 1: | |
if sys.argv[1] == "--host": | |
hostname = sys.argv[2] | |
if hostname: | |
if inventory["_meta"]["hostvars"][hostname]: | |
print(json.dumps(inventory["_meta"]["hostvars"][hostname])) | |
else: | |
print(json.dumps(inventory)) |
実行例はこちら。
Nutanix から仮想マシンを取得する Ansible Dynamic Inventory を作成してみる。(REST API v2)
https://blog.ntnx.jp/entry/2023/05/07/150924
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PowerOn / PowerOff の VM をグループ化できるようにしてみた。