Generate Ansible Inventory Docs GitHub Action
This GitHub Action automatically generates documentation for the Ansible inventories whenever changes are pushed to the main branch or a pull request is created. It uses the Generate Inventory Documentation Script Python script to generate markdown files for each inventory, and maintains a global index of inventories, and commits the changes back to the repository.
π Workflow File
name: Generate Ansible Inventory Docs
on:
push:
branches:
- main
pull_request:
jobs:
generate-inventory-docs:
# Only run the job logic when the branch is main
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
# Checkout the repo
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # needed for committing back
# Set up Python
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
# Install dependencies (if any)
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
# Run the documentation generator
- name: Generate inventory docs
run: |
python ./scripts/generate_inventory_docs.py
- name: Commit and push changes
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git pull origin main
git add inventory/README.md docs/inventories/README.md docs/inventories/*.md
if ! git diff --cached --quiet; then
git commit -m "chore(docs): auto-generate inventory documentation"
git push origin main
else
echo "No documentation changes to commit."
fi
πΉ Workflow Details
Trigger
- push: Any push to the
mainbranch. - pull_request: Any new pull request.
Jobs
generate-inventory-docs
- Runs on:
ubuntu-latest -
Steps:
- Checkout repository
Uses
actions/checkout@v4withfetch-depth: 0to ensure the repository history is fully available for commits. - Set up Python
Installs Python 3.11 using
actions/setup-python@v5. - Install dependencies
Upgrades
pipand installs Python dependencies fromrequirements.txt. - Generate inventory docs
Runs the Python script
scripts/generate_inventory_docs.pyto produce markdown documentation for all inventories. - Commit and push changes Configures git user, stages updated markdown files, commits only if there are changes, and pushes back to the branch.
- Checkout repository
Uses
β‘ Summary
- Trigger: Runs on every push to
mainand on pull requests. - Checkout: Uses
actions/checkout@v4withfetch-depth: 0so commits can be pushed back. - Python Setup: Uses Python 3.11 (adjustable).
- Dependencies: Installs from
requirements.txt(currently onlyPyYAML). - Script Execution: Runs
scripts/generate_inventory_docs.pyto regenerate inventory documentation. - Generated Documentation: The following markdown files are created by the Python script:
inventory/README.mddocs/inventory/README.mddocs/inventory/<inventory folder name>.md
- Commit Logic:
- Stages only inventory markdowns and the central index.
- Commits only if changes exist (
git diff --cached --quietprevents empty commits). - Pushes back to the branch that triggered the workflow.
- Permissions: Requires repository Actions β Workflow permissions set to Read and write so the builtβin
GITHUB_TOKENcan push commits.