Skip to content

Ansible inventory graph

Adam Spiers edited this page Dec 15, 2017 · 4 revisions

Graphing chunks of Ardana's Ansible inventory

Here is a very cryptic HOWTO:

zypper in graphviz-python
pip install ansible-inventory-grapher

Manually patch usage of ansible.module_utils.six out of /usr/bin/ansible-inventory-grapher

  • use basestring instead of string_types
  • This is maybe due to us having an old Ansible?
cat <<EOF >NOV.j2
digraph {{pattern|labelescape}} {
  {{ attributes }}

{% for node in nodes|sort(attribute='name') %}
{% if node.leaf %}
  {{ node.name|labelescape }} [shape=record style=rounded label=<
<table border="0" cellborder="0">
  <tr><td><b>
  <font face="Times New Roman, Bold" point-size="16">{{ node.name}}</font>
  </b></td></tr>
{% if node.vars and showvars %}<hr/><tr><td><font face="Times New Roman, Bold" point-size="14">{% for var in node.vars|sort %}{{var}}<br/>{%endfor %}</font></td></tr>{% endif %}
</table>
>]
{% elif 'NOV' in node.name %}
  {{ node.name|labelescape }} [shape=record label=<
<table border="0" cellborder="0">
  <tr><td><b>
  <font face="Times New Roman, Bold" point-size="16">{{ node.name}}</font>
  </b></td></tr>
{% if node.vars and showvars %}<hr/><tr><td><font face="Times New Roman, Bold" point-size="14">{% for var in node.vars|sort %}{{var}}<br/>{%endfor %}</font></td></tr>{% endif %}
</table>
>]
{% endif %}{% endfor %}

{% for edge in edges|sort(attribute='source') if "NOV" in edge.source or "NOV" in edge.target %}
  {{ edge.source|labelescape }} -> {{ edge.target|labelescape }};
{% endfor %}
}
EOF

ansible-inventory-grapher -i hosts/verb_hosts -t NOV.j2 deployerincloud-ccp-c0-m1 > NOV.dot

dot -Tpng -O NOV.dot
# or:
dot -Tsvg -O NOV.dot

Now view NOV.dot.png or NOV.dot.svg depending on which you generated.

Example output

See also