The ansible-inventory-grapher command

The ansible-inventory-grapher command by Will Thames uses the Graphviz library to visualize your host inventories. The first thing we need to do is install Graphviz. To install this on macOS using Homebrew, run the following command:

$ brew install graphviz

Or, to install Graphviz on Ubuntu, use:

$ sudo apt-get install graphviz

Once installed, you can install ansible-inventory-grapher using pip:

$ sudo install ansible-inventory-grapher

Now that we have everything installed, we can generate the graph using the hosts file we used earlier in the chapter:

ansible01 ansible_host=46.101.92.240
ansible02 ansible_host=159.65.63.218
ansible03 ansible_host=159.65.63.217
ansible04 ansible_host=138.68.145.116

[london]
ansible01
ansible02

[nyc]
ansible03
ansible04

[digitalocean:children]
london
nyc

[digitalocean:vars]
ansible_connection=ssh
ansible_user=root
ansible_private_key_file=~/.ssh/id_rsa
host_key_checking=False

We can run the following command to generate the raw graph file:

$ ansible-inventory-grapher -i hosts digitalocean

This will generate the following output:

digraph "digitalocean" {
rankdir=TB;

"all" [shape=record label=<
<table border="0" cellborder="0">
<tr><td><b><font face="Times New Roman, Bold" point-size="16">
all</font>
</b></td></tr>
</table>
>]
"ansible01" [shape=record style=rounded label=<
<table border="0" cellborder="0">
<tr><td><b><font face="Times New Roman, Bold" point-size="16">
ansible01</font>
</b></td></tr>
<hr/><tr><td><font face="Times New Roman, Bold"
point-size="14">ansible_connection<br/>ansible_host<br/>
ansible_private_key_file<br/>ansible_user<br/>
host_key_checking<br/></font></td></tr></table>

>]
"ansible02" [shape=record style=rounded label=<
<table border="0" cellborder="0">
<tr><td><b><font face="Times New Roman, Bold" point-size="16">
ansible02</font>
</b></td></tr>
<hr/><tr><td><font face="Times New Roman, Bold"
point-size="14">ansible_connection<br/>ansible_host<br/>
ansible_private_key_file<br/>ansible_user<br/>
host_key_checking<br/></font></td></tr></table>

>]
"ansible03" [shape=record style=rounded label=<
<table border="0" cellborder="0">
<tr><td><b><font face="Times New Roman, Bold" point-size="16">
ansible03</font>
</b></td></tr>
<hr/><tr><td><font face="Times New Roman, Bold"
point-size="14">ansible_connection<br/>ansible_host<br/>
ansible_private_key_file<br/>ansible_user<br/>
host_key_checking<br/></font></td></tr></table>

>]
"ansible04" [shape=record style=rounded label=<
<table border="0" cellborder="0">
<tr><td><b><font face="Times New Roman, Bold" point-size="16">
ansible04</font>
</b></td></tr>
<hr/><tr><td><font face="Times New Roman, Bold"
point-size="14">ansible_connection<br/>ansible_host<br/>
ansible_private_key_file<br/>ansible_user<br/>
host_key_checking<br/></font></td></tr></table>

>]
"digitalocean" [shape=record label=<
<table border="0" cellborder="0">
<tr><td><b><font face="Times New Roman, Bold" point-size="16">
digitalocean</font>
</b></td></tr>
</table>
>]
"london" [shape=record label=<
<table border="0" cellborder="0">
<tr><td><b><font face="Times New Roman, Bold" point-size="16">
london</font>
</b></td></tr>
</table>
>]
"nyc" [shape=record label=<
<table border="0" cellborder="0">
<tr><td><b><font face="Times New Roman, Bold" point-size="16">
nyc</font>
</b></td></tr>
</table>
>]

"all" -> "digitalocean";
"digitalocean" -> "london";
"digitalocean" -> "nyc";
"london" -> "ansible01";
"london" -> "ansible02";
"nyc" -> "ansible03";
"nyc" -> "ansible04";
}

This is the raw output of the graph. As you can see, it is similar to HTML. We can render this using the dot command, which ships as part of Graphviz. The dot command creates hierarchical drawings from graphs. To do this, run:

$ ansible-inventory-grapher -i hosts digitalocean | dot -Tpng > hosts.png

This will generate a PNG file called hosts.png that contains the visualization of the host inventory file you can see here:

We will be using this tool in later chapters to get an idea of what our inventory files look like as they are generated.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.214.32