AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

ansible-inventory-grapher: Visualize Ansible Inventory as Graph (Guide)

By Luca Berton · Published 2024-01-01 · Category: installation

How to use ansible-inventory-grapher to visualize your Ansible inventory structure as a graph diagram. Install, configure, and generate SVG/PNG inventory maps.

ansible-inventory-grapher: Visualize Ansible Inventory as Graph (Guide)

Introduction

As IT environments grow more complex, the ability to visualize infrastructure becomes increasingly valuable. This is where tools like ansible-inventory-grapher come into play, providing users with a means to graphically represent their Ansible inventories.

See also: Ansible troubleshooting - Error sanity

What is ansible-inventory-grapher?

ansible-inventory-grapher is a command-line tool designed to create visual graphs representing the connections and relationships between hosts and groups in an Ansible inventory. This can be especially useful when dealing with large inventories or when trying to understand the structure of an inherited project.

How Does it Work?

The tool takes an Ansible inventory file as input and generates a visual representation of all the hosts and groups, including variables assigned to each. By default, it creates a graph in DOT format, which can be rendered with graph visualization software like Graphviz.

See also: Understanding the ansible-console Command: Interactive Ansible REPL Tutorial

Key Features

Simplicity: With a simple command, users can generate a visual map of their inventory. • Clarity: It can clarify complex group relationships and inheritance. • Customization: Users can customize the output, highlighting specific parts of the inventory. • Integration: It works seamlessly with existing Ansible workflows.

Use Cases

Documentation: Automatically generate diagrams for documentation purposes. • Debugging: Identify misconfigurations in inventory hierarchies. • Onboarding: Help new team members understand the infrastructure quickly.

See also: Ansible Troubleshooting: Fix Jinja2 Syntax & Inventory Errors

Getting Started

To get started with ansible-inventory-grapher, you typically need to install the tool via pip, Ansible's package manager. Once installed, running it is as straightforward as pointing it to your inventory file and letting it process the data.

Conclusion

As infrastructure as code practices become standard, tools like ansible-inventory-grapher play a crucial role in maintaining clarity and understanding within IT teams. By visualizing Ansible inventories, teams can better communicate, troubleshoot, and document their environments, leading to more efficient and reliable operations.

Installation

pip install ansible-inventory-grapher

# Also need Graphviz for rendering # Ubuntu/Debian sudo apt install graphviz # RHEL/Fedora sudo dnf install graphviz # macOS brew install graphviz

Basic Usage

# Generate PNG
ansible-inventory-grapher -i inventory.yml all | dot -Tpng -o inventory.png

# Generate SVG (better for web) ansible-inventory-grapher -i inventory.yml all | dot -Tsvg -o inventory.svg

# Show specific group ansible-inventory-grapher -i inventory.yml webservers | dot -Tpng -o webservers.png

# Left-to-right layout ansible-inventory-grapher -i inventory.yml all -a "rankdir=LR;" | dot -Tsvg -o inventory.svg

Alternative: ansible-inventory --graph

Built-in text-based graph:

ansible-inventory -i inventory.yml --graph

Output:

@all:
  |--@webservers:
  |  |--web1
  |  |--web2
  |--@dbservers:
  |  |--db1
  |--@ungrouped:

With variables:

ansible-inventory -i inventory.yml --graph --vars

Alternative: ansible-cmdb

For HTML inventory reports:

pip install ansible-cmdb
ansible -m setup --tree out/ all
ansible-cmdb out/ > overview.html

CI/CD Integration

# .github/workflows/docs.yml
- name: Generate inventory diagram
  run: |
    pip install ansible-inventory-grapher
    sudo apt-get install -y graphviz
    ansible-inventory-grapher -i inventory/ all | dot -Tsvg -o docs/inventory.svg

FAQ

Why is my graph empty?

Validate your inventory first: ansible-inventory -i inventory.yml --list

Can I visualize dynamic inventory?

Yes: ansible-inventory-grapher -i aws_ec2.yml all | dot -Tpng -o aws.png

How do I generate a PDF?

ansible-inventory-grapher -i inventory.yml all | dot -Tpdf -o inventory.pdf

Related Articles

sudo and become in Ansible playbookshow Ansible inventory worksunderstanding Ansible roles

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home