|
1 | 1 | #!/usr/bin/env bash
|
2 | 2 |
|
| 3 | +# |
| 4 | +# This inventory script translates our data model to Ansible's data model. |
| 5 | +# We have one file per network location, and one or more hosts per location. |
| 6 | +# Ansible has groups and hosts, with one file per host, which wasn't practical. |
| 7 | +# |
| 8 | +# This script's output is a JSON object containing an array of all host names, |
| 9 | +# and an array with the initial hostvars for each host. See |
| 10 | +# https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html |
| 11 | +# for more general information on inventory construction. |
| 12 | +# |
| 13 | +# The queries and conversions are done with the help of the `jq` and `yq` tools. |
| 14 | +# |
| 15 | +# We first grab the JSON representation of every location YAML definition, |
| 16 | +# then read `.hosts[].hostname` for each location to create the list of all hosts. |
| 17 | +# Previously we also scanned the `host_vars/` directory for backward compatibility. |
| 18 | +# |
| 19 | +# Usually Ansible then calls this script with `--host <name>` for every host. |
| 20 | +# That would obviously get very slow with hundreds of hosts, |
| 21 | +# which is why Ansible lets us construct all hostvars objects in advance. |
| 22 | +# |
| 23 | +# To construct the hostvars object for a host, |
| 24 | +# we take as a base the full location object (without `.hosts`), |
| 25 | +# then merge the object from `.hosts[]` with a matching `hostname` value. |
| 26 | +# This way, host values overwrite location values. |
| 27 | +# (We actually merge host<-location<-host to preserve JSON ordering.) |
| 28 | +# |
| 29 | +# In total there are five stages to construct inventory and hostvars: |
| 30 | +# |
| 31 | +# 1. This inventory script |
| 32 | +# |
| 33 | +# 2. `host_vars/` |
| 34 | +# Old hosts in `host_vars/` don't need the conversion in this script, |
| 35 | +# they're still loaded by Ansible automatically. |
| 36 | +# |
| 37 | +# 3. Keyed groups |
| 38 | +# The next two stages after inventory construction handle keyed groups, |
| 39 | +# which load additional group data based on certain property values. |
| 40 | +# This is handled outside of this script by an Ansible plugin. |
| 41 | +# There are two stages so that new data from stage 1 can set properties |
| 42 | +# that result in more new data in stage 2 (e.g. `model` and OpenWrt version). |
| 43 | +# The first stage handles `location` (old), `target`, `model`, and `role`, |
| 44 | +# while the second handles `target` and `openwrt_version`. |
| 45 | +# |
| 46 | +# 4. Merge vars |
| 47 | +# The last stage is the `merge_vars` task. |
| 48 | +# All hostvars construction so far was only able to overwrite properties. |
| 49 | +# In some cases we need to merge with the existing property. |
| 50 | +# For specific properties, a "merge var" can be set: |
| 51 | +# `packages: ["some-pkg"]` and `xxx__packages__to_merge: ["another-pkg"]` |
| 52 | +# where `xxx` is an arbitrary name to allow for multiple merge vars. |
| 53 | +# Before any templates or tasks make use of hostvars, |
| 54 | +# these merge vars are merged together into one. |
| 55 | +# Applies to `ssh_keys`, `packages`, `sysctl`, `rclocal`, |
| 56 | +# `disabled_services`, `wireless_profiles`, `channel_assignments_*` |
| 57 | +# |
| 58 | + |
3 | 59 | set -e
|
4 | 60 | # set -x
|
5 | 61 |
|
6 | 62 | case "$1" in
|
7 | 63 | --host)
|
8 |
| - # No op - won't be called by Ansible anymore because --list contains all data. |
9 |
| - # See https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html#tuning-the-external-inventory-script |
| 64 | + # No op |
10 | 65 | echo "{}"
|
11 | 66 | exit 0
|
12 | 67 | ;;
|
|
0 commit comments