Created
August 15, 2014 12:05
-
-
Save czunker/d6ae14c1c324e6c26cb8 to your computer and use it in GitHub Desktop.
Create Ansible dynamic inventory from Puppet CA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
/usr/bin/puppet cert list --all > /dev/null 2>&1 | |
if [ $? -ne 0 ] | |
then | |
echo "Puppet error" | |
exit 1 | |
fi | |
HOSTS="{\n\t\"all\":\n\t\t[\n" | |
FIRST_HOST="true" | |
while read hostname | |
do | |
# this way i don't get a trailing comma | |
if [ "${FIRST_HOST}" == "true" ] | |
then | |
HOSTS="${HOSTS}\t\t\t\"${hostname}\"" | |
unset FIRST_HOST | |
else | |
HOSTS="${HOSTS}, \"${hostname}\"" | |
fi | |
# can't use command | while because of subshell and loss of variable | |
done < <(/usr/bin/puppet cert list --all | egrep "^\+.*" | awk -F"\"" '{ print $2 }' | grep "example.com") | |
HOSTS="${HOSTS}\n\t\t]\n}" | |
echo -e "${HOSTS}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment