joe-speedboat.fact_inventory

Role Name

joe-speedboat.fact_inventory

Description

This Ansible role helps you collect important information from different resources in your infrastructure. It simplifies tasks like deploying virtual machines, getting an overview of your resources, or visualizing network structures by gathering facts from various endpoints.

Features

  • Resource Compatibility: Works with many types of resources, including VMware vCenter, AWS, Azure, and others.
  • Easy Setup: Configure easily using YAML variables to select the resources for fact collection.
  • Organized Data: Gathers organized data like data centers, clusters, hosts, virtual machines, datastores, and networks.
  • JSON Output: Produces a clean JSON file with the collected facts, perfect for further analysis or visualization.

Requirements

  • Ansible version 2.11 or higher
  • Python version 3.6 or higher (on the target machine)
  • Required Python packages:
  • Ansible collection:
    • community.vmware

Role Variables

You can configure the following variables:

  • inventory_file: The path to the inventory file. Default is /tmp/fact_inventory.json.
  • debug_vars: A boolean to enable or disable debug mode. Default is True.
  • inventory_targets: A list of targets for fact gathering. Each target should contain:
    • resource_name: Name of the resource.
    • type: Type of resource (e.g., "vsphere").
    • validate_certs: A boolean to enable or disable certificate validation.
    • vcenter_api_url: URL for the vCenter API.
    • vcenter_username: Username for the vCenter API.
    • vcenter_password: Password for the vCenter API.
    • datacenter_regex: Regular expression to filter data centers.
    • cluster_regex: Regular expression to filter clusters.
    • host_regex: Regular expression to filter hosts.
    • datastore_regex: Regular expression to filter datastores.
    • network_regex: Regular expression to filter networks.
    • vm_folder_regex: Regular expression to filter VM folders.

Dependencies

None.

License

GPLv3

Usage

To use this role, specify the variables in the inventory_targets list in your playbook or the defaults/main.yml file. Each item represents a target for fact collection.

The variables datacenter_regex, cluster_regex, host_regex, datastore_regex, network_regex, and vm_folder_regex allow you to filter results from the vSphere API using regular expressions. For example, to gather facts only from data centers starting with "dc", set datacenter_regex to '^dc'.

Testing

Create a vars.yml file in the tests folder with your configurations by copying the vars_example.yml file:

cp tests/vars_example.yml tests/vars.yml

Fill the vars.yml file with your specific values, including regular expressions for filtering.

Run the tests with the following command:

ansible-playbook tests/test.yml -i tests/inventory

This runs the test.yml playbook using the inventory file found in the tests directory.

CGI Script for Rundeck Options

You can create interactive job menus with Rundeck and JSON. To do this, set up a CGI server first and use it with the JSON file created by this role.

Here's how to set up lighttpd with CGI on Rocky Linux 8:

dnf install lighttpd
systemctl enable lighttpd

cp -av /etc/lighttpd/modules.conf /etc/lighttpd/modules.conf.orig
cp -av /etc/lighttpd/lighttpd.conf /etc/lighttpd/lighttpd.conf.orig
cp -av /etc/lighttpd/conf.d/cgi.conf /etc/lighttpd/conf.d/cgi.conf.orig

sed -i 's@.*\(include.*conf.d/cgi.conf"\).*@\1@' /etc/lighttpd/modules.conf
sed -i 's@.*server\.use-ipv6.*@server\.use-ipv6 = "disable"@' /etc/lighttpd/lighttpd.conf
sed -i 's@.*server\.bind.*@server\.bind = "localhost"@' /etc/lighttpd/lighttpd.conf
sed -i 's@.*server\.port.*@server\.port = 8888@' /etc/lighttpd/lighttpd.conf
semanage port -a -t http_port_t -p tcp 8888
systemctl restart lighttpd
systemctl status lighttpd

lsof -i -P -n | grep light

test -f /usr/bin/python && ln -s /usr/bin/python3 /usr/bin/python

mkdir -p /var/www/lighttpd/cgi-bin

vi /var/www/lighttpd/cgi-bin/test.cgi
------------------
#!/usr/bin/env python3

print("Content-Type: text/html")  # HTML is following
print()  # blank line, end of headers
print("<html>")
print("<head><title>Hello CGI</title></head>")
print("<body>")
print("<h1>Hello World</h1>")
print("</body>")
print("</html>")
----------------------
chmod +x /var/www/lighttpd/cgi-bin/test.cgi

curl http://127.0.0.1:8888/cgi-bin/test.cgi

I also provided a CGI script for serving options in tests/cgi-bin/option.py. Copy it to the CGI-bin directory, update the JSON file location, and call it. Please note this CGI is a basic proof of concept.

cp tests/cgi-bin/option.py /var/www/lighttpd/cgi-bin/option.cgi
chmod +x /var/www/lighttpd/cgi-bin/option.cgi
vi /var/www/lighttpd/cgi-bin/option.cgi # update JSON file location

You can call it within Rundeck:

[ "Datacenter1" ]
[ "Cluster1" ]
[ "LUN_01_VM_Replica", "LUN_02_VM_Replicas" ]

This is quite amazing with relatively little effort.

Informazioni sul progetto

Create JSON Ressource file of various vSphere endpoints

Installa
ansible-galaxy install joe-speedboat.fact_inventory
Licenza
gpl-3.0
Download
4.5k
Proprietario
I don't do Linux for 20 years because I love Unix or consoles. OpenSource is what I love and want to do. moving together ... that fascinates me ...