A Bash script for validating and checking Ansible playbooks, inventory files, and Jinja2 templates. This script helps ensure the correctness of your Ansible configurations by performing syntax checks, linting, and inventory validation.
Find a file
2025-10-31 23:01:52 +01:00
lib refactor TEMP_PLAYBOOK initialization and improve error messages in check_dependency function 2025-10-31 23:01:52 +01:00
.gitignore init 2025-05-13 17:45:37 +02:00
LICENSE init 2025-05-13 17:45:37 +02:00
README.md fix script name in README.md to validate_ansible.sh 2025-10-31 23:00:53 +01:00
validate_ansible.sh update collect_files function to handle null-delimited filenames and support both .yml and .yaml extensions 2025-10-31 23:01:09 +01:00

Ansible Validation Script

A Bash script for validating and checking Ansible playbooks, inventory files, and Jinja2 templates. This script helps ensure the correctness of your Ansible configurations by performing syntax checks, linting, and inventory validation.

Features

  • Syntax Validation:
    • Validates YAML syntax for playbooks, roles, and variable files.
    • Checks inventory files for correctness using ansible-inventory.
    • Validates Jinja2 templates for syntax errors.
  • Linting:
    • Runs ansible-lint for YAML files.
    • Uses yamllint for additional YAML checks.
  • Modes of Operation:
    • Default: Checks only files changed in the current Git repository.
    • --all: Checks all YAML, inventory, and template files in the directory.
    • Selective modes: Check specific file types with --playbooks, --inventory, or --template.

Installation

  1. Clone the repository or copy the script to your desired location.
  2. Ensure the following dependencies are installed:
    • ansible
    • ansible-lint
    • yamllint
    • python3 with the jinja2 module
  3. Make the script executable:
    chmod +x validate_ansible.sh
    

Usage

Run the script with the desired options:

./validate_ansible.sh [OPTIONS]

Options

  • --all: Check all file types (playbooks, inventory, and templates).
  • --playbooks: Check only playbook files.
  • --inventory: Check only inventory files.
  • --template: Check only Jinja2 template files.
  • --help: Display usage information.

If no options are provided, the script will check only files changed in the current Git repository.

Examples

  1. Check all files in the directory:

    ./validate_ansible.sh --all
    
  2. Check only playbook files:

    ./validate_ansible.sh --playbooks
    
  3. Validate files changed in the current Git repository:

    ./validate_ansible.sh
    

Dependencies

The script requires the following tools and libraries:

  • Ansible: For playbook and inventory validation.
  • ansible-lint: For linting YAML files.
  • yamllint: For additional YAML syntax checks.
  • Python 3 with the jinja2 module: For validating Jinja2 templates.

Install missing dependencies using your package manager or pip:

sudo apt install ansible ansible-lint yamllint python3
pip3 install jinja2

Contributing

Contributions are welcome! To contribute:

  1. Fork the repository.
  2. Create a new branch for your feature or bugfix.
  3. Submit a pull request with a clear description of your changes.

License

This project is licensed under the AGPL-3.0 License. See the LICENSE file for details.