Skip to content

Instantly share code, notes, and snippets.

@deuscapturus
Last active March 9, 2016 13:39
SaltStack Bootstrap for AWS EC2

SaltStack Bootstrap Script for AWS EC2

This script will currently only work with Windows8, Windows 2012R2, Amazon Linux, RHEL6 and RHEL7.

#!/bin/bash
function HELP {
echo -e \\n"Output a bootstrap script to install SaltStack with parameters"\\n
echo -e "${REV}Basic usage:${NORM} ${BOLD} ec2-bootstrap-args.sh [OPTIONS...]${NORM}"\\n
echo "Command line switches are required. The following switches are recognized."\\n
echo "-m --Define one or two salt masters"
echo "-i --Define the minion id"
echo "-g --Define one or more grains as \"key value\" with quotes."
echo "-l --Define one or more grains lists as \"key value\" with quotes."
echo "-u --Perform upgrade 'yum upgrade' values. Values: yes|no"
echo "-h --Perform highstate after install. Values: yes|no"\\n
echo "-w --Create Microsoft windows bootstrap script"\\n
echo -e "Example: ec2-bootstrap-args.sh -g \"deployment production\" -l \"roles web\" -l \"roles web\" -u \"yes\" -h \"no\""\\n
exit 1
}
WINDOWS=false
UPGRADE=false
HIGHSTATE=false
while getopts "wg:l:m:i:uh" opt
do
case $opt in
g) GRAINS+=("$OPTARG");;
l) GRAINSLIST+=("$OPTARG");;
m) MASTERS+=("$OPTARG");;
u) UPGRADE=true;;
h) HIGHSTATE=true;;
w) WINDOWS=true;;
i) ID=("$OPTARG");;
\?) echo -e \\n"Option -${BOLD}$OPTARG${NORM} not allowed."
HELP
exit 2;;
esac
done
shift $((OPTIND -1))
if $WINDOWS
then
echo "<powershell>"
COUNTER=1
for master in "${MASTERS[@]}"
do
echo "\$MASTER$COUNTER='$master'"
let COUNTER=COUNTER+1
done
echo "\$saltURL = 'http://' + \$MASTER1 + '/Salt-Minion-AMD64-Setup.exe'
\$NAME='$ID'
Start-Transcript -path c:\salt-bootstrap.log -append
Invoke-WebRequest \$saltURL -OutFile c:\salt.exe
c:\salt.exe /S /master=\$MASTER1 /minion-name=\$NAME
while (!(Test-Path \"c:\salt\salt-call.exe\") -and !(Test-Path \"c:\salt\salt-call.bat\")) { echo \"Waiting for salt-call.exe to appear\" ; Start-Sleep 5 }
Start-Sleep -s 15
"
for val in "${GRAINS[@]}"
do
echo "echo 'Setting grains...'"
echo "c:\salt\salt-call --local grains.setval $val"
done
for val in "${GRAINSLIST[@]}"
do
echo "echo 'Setting grains...'"
echo "c:\salt\salt-call --local grains.append $val"
done
echo "
Start-Sleep -s 5
c:\salt\salt-call pkg.refresh_db
Restart-Service -Name salt-minion"
if [ $HIGHSTATE == "true" ]
then
echo "Start-Sleep -s 15"
echo "c:\salt\salt-call --local state.highstate"
fi
echo "Stop-Transcript
</powershell>"
else
echo "#!/bin/bash
# Created by Theodore Cowan, t@theodore.me
######################CONFIGURATION############################
#Configure a primary and second salt master"
COUNTER=1
for master in "${MASTERS[@]}"
do
echo "MASTER$COUNTER='$master'"
let COUNTER=COUNTER+1
done
echo "
#Configure grains
GRAINS=("
for val in "${GRAINS[@]}"
do
echo "'$val'"
done
echo " )
#Configure any grains as lists here
GRAINSLIST=("
for val in "${GRAINSLIST[@]}"; do
echo "'$val'"
done
echo " )
#Perform yum upgrade
upgrade='$UPGRADE'
#Startup highstate
highstate='$HIGHSTATE'
#####################END CONFIGURATION#########################
platform=\$(cat /etc/system-release-cpe | cut -d ':' -f 3)
platform_version=\$(cat /etc/system-release-cpe | cut -d ':' -f 5)
# Add epel repo
if [ \$platform == 'amazon' ]
then
yum-config-manager --enable epel
elif [ \$platform == 'redhat' ] || [ \$platform == 'centos' ]
then
yum install -y yum-utils
yum-config-manager --add-repo=http://${MASTERS[0]}/yum/saltstack-rhel\$platform_version.repo
else
echo 'Unknown platform \$platform'
exit 1
fi
# Perform update
if [ \$upgrade == 'true' ]
then
yum update -y -q
fi
# Install SaltStack minion
yum install salt-minion -y -q
# Configure master servers
if [[ \"\$MASTER1\" && \"\$MASTER2\" ]]
then
sed -i \"s/#master: salt/master:\n - \$MASTER1\n - \$MASTER2/g\" /etc/salt/minion
elif [ \"\$MASTER1\" ] || [ \"\$MASTER2\" ]
then
sed -i \"s/#master: salt/master: \$MASTER1\$MASTER2/g\" /etc/salt/minion
fi
# Start minion service
chkconfig salt-minion on
service salt-minion start
#wait 5 seconds
sleep 5
# Configure Grains
for g in \"\${GRAINS[@]}\"
do
salt-call --local grains.setval \$g
done
for g in \"\${GRAINSLIST[@]}\"
do
salt-call --local grains.append \$g
done
# Sync all modules
salt-call saltutil.sync_grains
# Restart salt minion service
salt-call --local service.restart salt-minion
# Run highstate
if [ \$highstate == \"true\" ]
then
salt-call state.highstate
fi"
fi
@deuscapturus
Copy link
Author

Will now install the EPEL if not installed.

@fii
Copy link

fii commented Oct 13, 2015

Overkill ...Just use Salt-Cloud + Reactors + Terraform.

@mcansky
Copy link

mcansky commented Oct 29, 2015

@fii : do you have a couple of good links to refer to please ?

@deuscapturus
Copy link
Author

I personally think salt-cloud is overkill. this is simple.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment