See also: Plugin
Juju can be easily extended by using plugins. There are many third-party plugins available to simplify certain tasks. However, you can easily create your own custom plugins for your specific needs. This tutorial will show you how to do this.
Let’s say we want to get the IP address for a machine. To do this, we can run
juju status, look at the table of units, find the row for the desired machine and read off the IP address. However:
- we cannot easily do this inside a Bash script
- this might get tedious if we have to do it repeatedly.
To solve this, we can write a plugin that, given the ID for a machine, will return the IP address. We want to be able to run it like this:
$ juju ip 0 10.50.10.50
Let’s get started. Create a file called
juju-ip with the following contents:
#!/bin/bash # First arg is machine ID MACHINE=$1 # The jq query we will use to find the IP address QUERY=$(printf '.machines."%d"."dns-name"' "$MACHINE") # Call juju status, use jq to filter output juju status --format=json | jq -r "$QUERY"
This is a simple Bash script that calls
juju status using JSON output, so that we can use
jq to filter the output and get the IP address. (You’ll need to install
jq for this to work.)
We’ve used Bash for this plugin, but you can use any language which allows command-line execution. Feel free to use Python, JS, Go, etc - whatever you are most familiar with.
To run this plugin, we just need to ensure that the
juju-ip file is placed somewhere on our
$PATH. In a terminal, run
echo $PATH | tr ':' '\n'
This will output all the directories on your
$PATH. Put the
juju-ip file in any one of these directories.
We also need to ensure our plugin is an executable file. In your terminal, run
sudo chmod +x [path/to/juju-ip]
[path/to/juju-ip] with the new location of the
Now, we are ready to run the plugin! Switch to a model with some machines deployed, and run
juju ip 0
When you run
juju [command] [args]
[command]is not a built-in Juju command, Juju will automatically search your
$PATHfor a file named
juju-[command], and attempt to execute it with the given
[args]. This mechanism is what makes plugins possible.
If you’ve written something that you think other Juju users might find useful, feel free to distribute it. We suggest: