import json
import os
from ansible_aap_api_client import (
InventoryBuilder,
InventoryRequestSchema,
InventoryHostRequestSchema,
InventoryGroupRequestSchema,
Organization,
)
from dotenv import load_dotenv
load_dotenv()
BASE_URL = os.getenv("BASE_URL")
USER_NAME = os.getenv("USER_NAME")
PASSWORD = os.getenv("PASSWORD")
HOME_INVENTORY_NAME = "my-inventory-with-custom-groups-and-hosts"
HOME_ORGANIZATION_NAME = "Default"
def to_json(data: dict) -> None:
print(json.dumps(data, indent=4))
inventory_host_request_schema = InventoryHostRequestSchema(
name="FAKE-DEVICE",
description="This is a test host",
)
custom_group_vars = {
"ansible_ssh_host": "something",
"ansible_ssh_user": "admin",
"ansible_ssh_pass": "admin",
"ansible_network_os": "moo",
}
custom_group = InventoryGroupRequestSchema(
name="CUSTOM-GROUP",
description="This is a test group",
variables=custom_group_vars,
)
custom_hosts = [
inventory_host_request_schema,
]
custom_groups = [
custom_group,
]
obj_organization = Organization(base_url=BASE_URL, username=USER_NAME, password=PASSWORD, ssl_verify=False)
inventory_request_schema = InventoryRequestSchema(
name=HOME_INVENTORY_NAME,
description="This is a test inventory",
organization=obj_organization.get_organization_id(HOME_ORGANIZATION_NAME),
)
obj_inventory = InventoryBuilder(
base_url=BASE_URL, username=USER_NAME, password=PASSWORD, ssl_verify=False, inventory=inventory_request_schema
)
obj_inventory.add_custom_groups_to_inventory(groups=custom_groups)
print(obj_inventory.custom_groups)
print(obj_inventory.custom_groups_data)
obj_inventory.add_hosts_to_custom_group_to_inventory(group_name="CUSTOM-GROUP", hosts=custom_hosts)
print(obj_inventory.custom_hosts)
obj_inventory.run()
print(obj_inventory.custom_groups_data)