Skip to content

Autoconfig create inventory.yml using Python Script

Hamzaukw edited this page Sep 9, 2025 · 1 revision

Scripts/autoconfig-pipeline.py

This script automates the setup and processing of system configurations in an Ansible-based environment. Command to run: bnsc.exec This command is defined inside of luxnix/devenv/scripts as a executable script. direnv allow is prerequisite to run bnsc.exec short command.

Prerequisites

  1. Host Facts
    Ensure host facts are available at ansible/cmdb. If they are not available, generate them by running the script located at scripts/ansible-cmdb.sh using command run scripts/ansible-cmdb.sh.
  2. Ansible Inventory
    Verify that the inventory file is present at ansible/inventory.

1. main Function (scripts/autoconf-pipeline.py)

Purpose:

  • Acts as the entry point for the script when run directly.
  • Sets up paths:
    • ansible_root(/ansible): Path to Ansible resources (inventory and cmdb).
    • autoconf_out(/autoconf): Output directory for the ETL pipeline.
    • nix_out: Target directory for configuration files.
  • Call the pipe function with the above paths to start the process.

2. Top-Level pipe Function (lx_administration/autoconf/main.py)

Purpose:

  • Coordinates the ETL pipeline by calling two sub-pipelines:
    • etl_pipe:
      • Extracts inventory and host facts from Ansible resources.
      • Generates a merged inventory file (inventory.yml) in autoconf_out.
    • nix_pipe:
      • Processes Nix templates using data from autoconf_out.
      • Generates Nix configuration files in nix_out.
  • Parameters:
    • ansible_root: Directory containing Ansible inventory and facts.
    • autoconf_out: Directory where intermediate results are stored.
    • nix_out: Output directory for Nix configurations.
    • conf_parent: Directory containing Nix templates.
  • Logger: Logs the process for debugging and monitoring.

3. pipe Function in (lx_administration/autoconf/imports/main.py)

Purpose:

  • Runs ansible_etl to perform the ETL process.
  • Logs input paths and the subnet being processed.
  • Ensures the output directory (autoconf_out) exists.

4. ansible_etl Function (lx_administration/autoconf/imports/main.py)

Purpose:

  • Loads the Ansible inventory from hosts.ini.
  • Loads host facts from the cmdb directory.
  • Merges host facts with the inventory.
  • Saves the updated inventory as inventory.yml.
  • Exports merged variables for each host into the merged_vars directory.

Summary

Purpose:

This code processes Ansible configurations into merged YAML inventories and generates Nix configuration files.

Inputs:

  • ansible/inventory/hosts.ini: Ansible inventory file.
  • ansible/cmdb: Directory containing host facts.

Outputs:

  • autoconf/inventory.yml: Complete inventory with merged host facts.
  • autoconf/merged_vars: Per-host .yml files.
  • nix_out: Directory containing Nix configuration files.

Error Handling:

  • Logs errors for missing files or directories.
  • Warns about invalid host variables.

The script can run using command: python autoconf-pipeline.py in scripts/autoconf-pipeline.py

Clone this wiki locally