An Ansible inventory plugin for DigitalOcean does not yet exist. Fortunately, with some bash scripting and the help of jq, we can write a script which generates an inventory for us.
If we had the following set of servers:
Server Name | Public IPv4 | Private IPv4 | Tags |
---|---|---|---|
appserver-a | 100.65.89.4 | 10.100.101.90 | role-appserver |
appserver-b | 100.65.89.10 | 10.100.101.23 | role-appserver |
appserver-c | 100.65.89.24 | 10.100.101.99 | role-appserver |
database-a | 100.202.73.9 | 10.200.4.187 | role-database |
load-balancer | 100.200.33.196 | 10.200.4.136 | role-load-balancer |
A static Ansible inventory file could look like
[appservers]
100.65.89.4 internal_ip=10.100.101.90
100.65.89.10 internal_ip=10.100.101.23
100.65.89.24 internal_ip=10.100.101.99
[databases]
100.202.73.9 internal_ip=10.200.4.187
[load-balancer]
100.200.33.196 internal_ip=10.200.4.136
Servers come and go, and keeping track of them in this manner is no fun.
The script
#!/bin/bash
json=
appservers=""
databases=""
load_balancer=""
meta_hostvars=
To execute the script, first make sure the DO_TOKEN
variable is set with your personal access token:
The output should look similar to this:
To use it for a playbook, supplement the static inventory with a path to the script file:
How it works
json=
Fetch a list of all available droplets.
Pipe JSON data into a jq filter which filters droplets based on a role (role-appserver
, role-database
, role-load-balancer
) and returns a list of arrays in the format [ <public ip>, <private ip> ]
.
appservers=""
databases=""
load_balancer=""
Set server group data from results of the filter_droplets
function call.
meta_hostvars=
Setup the _meta
property by piping the joined lists of all the server groups into jq and using the slurp (-s
) option to generate valid JSON. The jq filter will create key/value pairs where the key is the public ip (.[0]
) and the value is an object containing the private ip ({"internal_ip": .[1]}
). add
merges the the array of key/value pairs into a single object.
Finally we generate the JSON output.
Once again slurp the list of IP arrays to create a valid JSON array of arrays, and for each sub-array, use the first item.