Last active
June 9, 2021 16:59
-
-
Save carlessanagustin/ddc06e1b4df1a175cbd3 to your computer and use it in GitHub Desktop.
Quick Bash script for running Ansible Playbooks
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
#!/usr/bin/env bash | |
########################## REQUIREMENTS | |
# sudo apt-get install ansible unzip | |
########################## VARIBLES | |
PLAYBOOK_GZ="playbook.zip" | |
PLAYBOOK_FILE="update.yml" | |
INVENTORY_FILE="all" | |
EXTRA_ARGS="--list-tasks --list-hosts" | |
SOURCE_DIR=$(pwd) | |
DESTINATION_DIR=/tmp/update | |
LOGFILE=$SOURCE_DIR/update.log | |
########################## CODE | |
echo "Starting log" >> $LOGFILE | |
echo -n "Adding key... " >> $LOGFILE | |
cd ~/.ssh | |
cat id_rsa.pub >> authorized_keys | |
SORTIDA=$? | |
echo OK >> $LOGFILE | |
echo -n "Cleaning folders... " >> $LOGFILE | |
rm -rf $DESTINATION_DIR 2> /dev/null | |
mkdir $DESTINATION_DIR | |
cp $SOURCE_DIR/$PLAYBOOK_GZ $DESTINATION_DIR | |
SORTIDA=$? | |
echo OK >> $LOGFILE | |
echo -n "Uncompressing update... " >> $LOGFILE | |
cd $DESTINATION_DIR | |
unzip $PLAYBOOK_GZ | |
rm $PLAYBOOK_GZ | |
SORTIDA=$? | |
echo OK >> $LOGFILE | |
echo "Updating... " >> $LOGFILE | |
cd $DESTINATION_DIR/playbook | |
ansible-playbook $PLAYBOOK_FILE -i hosts/$INVENTORY_FILE $EXTRA_ARGS >> $LOGFILE | |
SORTIDA=$? | |
echo OK >> $LOGFILE | |
if [ "$SORTIDA" -eq 0 ]; | |
then | |
echo "Updated!" | |
else | |
echo "Update failed!" | |
fi | |
echo "Ending log" >> $LOGFILE | |
exit $SORTIDA |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment