ansible/contrib/cache-prod-inventory.sh

34 lines
852 B
Bash
Executable File

#! /bin/sh
#
# cache-prod-inventory.sh
# Copyright (C) 2025 Jacob Babor <jacob@babor.tech>
#
# Distributed under terms of the MIT license.
#
set -e
proddir="inventories/production"
invdir="inventories/production-cache"
# Sanity check
[ -d "$invdir" ] || {
echo "Could not find $invdir; are you in the root of the repo?"
exit 1
}
# Get the new data
[ -e "$invdir"/hosts.yml.new ] && rm "$invdir"/hosts.yml.new
ansible-inventory -i "$proddir" --list -y > "$invdir"/hosts.yml.new || {
# And handle errors
echo "Failed to get inventory; see above and $invdir/hosts.yml.new for errors"
exit 2
}
# Shuffle shit around
[ -e "$invdir"/hosts.yml.old ] && rm "$invdir"/hosts.yml.old
[ -e "$invdir"/hosts.yml ] && mv "$invdir"/hosts.yml{,.old}
[ -e "$invdir"/hosts.yml.new ] && mv "$invdir"/hosts.yml{.new,}
echo "Inventory cached. Use -i \"$invdir\""