ansible-inventory-grapher icon indicating copy to clipboard operation
ansible-inventory-grapher copied to clipboard

different colours for custom groups

Open mihai-satmarean opened this issue 6 years ago • 3 comments

Hi, again we find it very useful to toy around, in order to see some production use we could have different groups represented with different colours. EG: Ansible reserved groups with grey and our defined groups with other colour, maybe even the children with specific colours. Thanks!

mihai-satmarean avatar Apr 10 '18 09:04 mihai-satmarean

I'd suggest using a custom template (using the -t option) to achieve this. Other than all, there's not really any such thing as reserved groups, as far as I'm aware.

willthames avatar Apr 10 '18 09:04 willthames

thanks! an example would be useful, I am not familiar with the format, just find it very nice how you do that

another question, dynamic inventory can be represented?

2018-04-10 11:58 GMT+02:00 Will Thames [email protected]:

I'd suggest using a custom template to achieve this. Other than all, there's not really any such thing as reserved groups, as far as I'm aware.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/willthames/ansible-inventory-grapher/issues/38#issuecomment-380044009, or mute the thread https://github.com/notifications/unsubscribe-auth/AEgqxvEhsdlaNCJYWYK-k4Z-ElK5gQfZks5tnIJYgaJpZM4TN4lG .

mihai-satmarean avatar Apr 10 '18 18:04 mihai-satmarean

{%- set group_colors = {
"group1": "green",
"group2": "blue",
"group3": "orange"
} -%}

{%- set fill_colors = {
"green": "aquamarine",
"blue":  "aqua",
"orange": "yellow"
} %}

{%- set narrow_target_styles = {
"group1": "dotted",
"group2":  "dotted",
"group3":        "dotted",
}%}

{%- set color_map = {} %}
{%- for edge in edges|sort(attribute='source') if edge.source != "all"%}
  {%- if edge.source in group_colors %}
      {%- set color_map=color_map.__setitem__(edge.source, group_colors[edge.source]) %}
  {%- endif %}
{%- endfor %}
{%- for edge in edges|sort(attribute='source') if edge.source != "all"%}
  {%- if edge.source in group_colors %}
      {%- set color_map=color_map.__setitem__(edge.target, group_colors[edge.source]) %}
  {%- endif %}
{%- endfor %}

# repeat step to paint more levels
{%- for edge in edges|sort(attribute='source') if edge.source != "all"%}
  {%- if edge.source in color_map %}
      {%- set color_map=color_map.__setitem__(edge.target, color_map[edge.source]) %}
  {%- endif %}
{%- endfor %}

digraph {{pattern|labelescape}} {
  {{ attributes }}

{% for node in nodes|sort(attribute='name') if node.name != "all" %}
  {{ node.name|labelescape }} [shape=record penwidth=2 {% if node.name in color_map %}color="{{ color_map[node.name] }}" fillcolor="{{ fill_colors[color_map[node.name]] }}" {% endif %}style="filled{% if node.leaf %},rounded{% endif %}" 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}}{% if var|is_visible %} =<b>{{node.vars[var]}}</b>{% endif %}<br/>{%endfor %}</font></td></tr>{% endif %}
</table>
>]
{% endfor %}

{% for edge in edges|sort(attribute='source') if edge.source != "all"%}
  {{ edge.source|labelescape }} -> {{ edge.target|labelescape }}[penwidth=3
  {%- if edge.target in narrow_target_styles %}
  style = "{{ narrow_target_styles[edge.target] }}"];
  {% else %}
    {% if edge.source in color_map %} color="{{ color_map[edge.source] }}"{% endif %}];
  {% endif %}
{% endfor %}
}

hryamzik avatar Sep 24 '23 08:09 hryamzik