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)) |
PowerOn / PowerOff の VM をグループ化できるようにしてみた。
$ ansible-inventory -i ./ahv_inventory.py --graph
@all:
|--@poweroff_vms:
| |--dev-vm-01
| |--lab-oracle-01
| |--ol87-base-01
|--@poweron_vms:
| |--demo-ansible-01
| |--demo-ansible-02
| |--lab-ndb-01
| |--ndb-oracle-01
|--@ungrouped:
$ ansible -i ./ahv_inventory.py --list-hosts poweron_vms
hosts (4):
lab-ndb-01
demo-ansible-02
ndb-oracle-01
demo-ansible-01
$ansible-inventory -i ./ahv_inventory.py --host ndb-oracle-01
{
"ansible_host": "192.168.11.125",
"power_state": "on"
}
$ ansible-inventory -i ./ahv_inventory.py --host dev-vm-01
{
"ansible_host": "192.168.11.146",
"power_state": "off"
}
実行例はこちら。
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
The Script worked. Thank you.
Can you please help me to filter only power-on machines. How to apply the filters using ansible-playbook.
Using hosts as all is providing all the machines in nutanix environment. This is sample playbook example
hosts: all
become: yes
become_user: root
tasks:
shell:
"/usr/sbin/realm list|grep -i permitted"
register: result
Command executed is
#ansible-playbook -i nutanix_inventory.py realminstalledORnot.yml
Please help on this.
Thank you. Appreciate your help.