A command line tool to create a graph representing your Ansible playbook tasks and roles
A command line tool to create a graph representing your Ansible playbook tasks and roles
ansible-playbook-grapher is a command line tool to create a graph representing your Ansible playbooks, plays, tasks and roles. The aim of this project is to have an overview of your playbooks that you can use as documentation.
Inspired by Ansible Inventory Grapher.
import_* and include_*.--collapsible-nodes).The following features are available when opening the SVGs in a browser (recommended) or a viewer that supports JavaScript:
--include-role-tasks is set.--open-protocol-handler vscode: it will open the folders when double-clicking on roles (not include_role) and
the files for the other nodes. The cursor will be at the task exact position in the file.--open-protocol-handler custom --open-protocol-custom-formats '{}'. See the help
and an example.
To not confuse
with Ansible Handlers.ansible package has been split).graphviz renderer.sudo apt-get install graphviz # or yum install or brew installI try to respect Red Hat Ansible Engine Life Cycle for the supported Ansible version.
uv tool install ansible-playbook-grapherpip install ansible-playbook-grapherYou can also install the unpublished version from GitHub directly. Examples:
# Using uv
uv tool install "ansible-playbook-grapher @ git+https://github.com/haidaraM/ansible-playbook-grapher"
# Using pip
pip install "ansible-playbook-grapher @ git+https://github.com/haidaraM/ansible-playbook-grapher"At the time of writing, two renderers are supported:
graphviz (default): Generate the graph in SVG. Has more features than the other renderers.mermaid-flowchart: Generate the graph in Mermaid format. You can
directly embed the graph in your Markdown and GitHub (
and other integrations) will render it.json: Generate a JSON representation of the graph. The corresponding JSON schema
is here. The JSON
output will give you more flexibility to create your own renderer outside the grapher.Comparison of the renderers:
graphviz (.svg) |
mermaid-flowchart (.mmd) |
json (.json) |
|
|---|---|---|---|
| Click on nodes to open the files (open protocol handler) | ✅ | ❌ | ✅: the file location is in the output |
| Highlight on hover | ✅ | ❌ | ❌: NA |
| Change graph orientation | ❌ | ✅ | ❌: NA |
| Group roles by name | ✅ | ✅ | ✅: the roles with the same names will have the same IDs. |
| View the output file in your the OS default viewer | ✅ | ✅ on https://mermaid.live/ | ✅ |
| Tests of the output | Automatic | Manual (need a parser) | Automatic |
If you are interested to support more renderers, feel free to create an issue or raise a PR based on the existing renderers.
ansible-playbook-grapher tests/fixtures/example.ymlansible-playbook-grapher --include-role-tasks tests/fixtures/with_roles.ymlansible-playbook-grapher tests/fixtures/with_block.ymlansible-playbook-grapher --include-role-tasks --renderer mermaid-flowchart tests/fixtures/multi-plays.yml…ansible-playbook-grapher --renderer json tests/fixtures/simple_playbook.ymlJson output
…Note on block: Since a block is a logical group of tasks, the conditional when is not displayed on the edges
pointing to them but on the tasks inside the block. This
mimics Ansible behavior
regarding the blocks.
The available options:
…The content of ansible.cfg is loaded automatically when running the grapher according to Ansible's behavior. The
corresponding environment variables are also loaded.
The values in the config file (and their corresponding environment variables) may affect the behavior of the grapher.
For example TAGS_RUN and TAGS_SKIP or vault configuration.
More information here.
ansible_os_family, ansible_system, etc.). The tasks inside any
import_* or include_* with some variables in their arguments may not appear in the graph.Contributions are welcome. Feel free to contribute by creating an issue or submitting a PR :smiley:
To set up a new local development environment:
uv sync --dev(cd tests/fixtures && ansible-galaxy install -r requirements.yml)The project contains some collections in collections. If you modify them, you need to do a force install with
ansible-galaxy install -r requirements.yml --force
Run the tests and open the generated files in your system's default viewer application:
export TEST_VIEW_GENERATED_FILE=1
make test # run all testsThe graphs are generated in the folders tests/generated-svgs, tests/generated-mermaids and tests/generated-jsons.
They are also generated as artifacts in GitHub Actions.
Feel free to look at them when submitting PRs.
The project uses ruff to format and lint the code. Run make lint to format and lint.
GNU General Public License v3.0 or later (Same as Ansible)
See LICENSE for the full text
No open issues yet, or sync has not completed.