Skip to content

Instantly share code, notes, and snippets.

@fedorg
Last active January 7, 2021 21:56
Show Gist options
  • Save fedorg/5ec9561d4dd456a74b8aabe87f50acd2 to your computer and use it in GitHub Desktop.
Save fedorg/5ec9561d4dd456a74b8aabe87f50acd2 to your computer and use it in GitHub Desktop.
Install Ansible, Hashicorp products and AWS CLI with 2-FA into a fresh Ubuntu 20 install (tested in WSL 2)
#!/usr/bin/bash
set -Eeuxo pipefail
if [ true ]; then
if [ $(id -u) -eq 0 ] ; then
echo "Error: need to call this script as regular user!"
exit 1
fi
# add software repos
sudo apt update
sudo apt install -y unzip software-properties-common jq
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository --yes "deb [arch=$(dpkg --print-architecture)] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
# fetch software repos
sudo apt update
# install software
sudo apt install -y terraform nomad consul packer
terraform -v
consul -v
nomad -v
packer -v
sudo apt install -y python3-pip
pip3 install ansible argcomplete
sudo activate-global-python-argcomplete
# cd
# wget -O aws-vault https://github.com/99designs/aws-vault/releases/download/v6.2.0/aws-vault-linux-amd64
# chmod +x ./aws-vault && mv $_ /usr/bin/
if [ ! -f $(which aws) ]; then
cd
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip "awscliv2.zip"
chmod +x ./aws/install
./aws/install
aws configure
fi
fi
if [ true ]; then
MFA_DEVICE_ARN_=""
read -p 'Enter the Multi-factor device ARN: ' MFA_DEVICE_ARN_
while [[ $MFA_DEVICE_ARN_ = "" ]]; do
read MFA_DEVICE_ARN_
done
cat <<EOF >>~/.bashrc
# AWS installer
complete -C \$(which aws_completer) aws
aws_login() {
session=\$(aws sts get-session-token "\$@")
# echo "\${session}"
AWS_ACCESS_KEY_ID=\$(echo "\${session}" | jq -r '.Credentials.AccessKeyId')
export AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY=\$(echo "\${session}" | jq -r '.Credentials.SecretAccessKey')
export AWS_SECRET_ACCESS_KEY
AWS_SESSION_TOKEN=\$(echo "\${session}" | jq -r '.Credentials.SessionToken')
export AWS_SESSION_TOKEN
}
alias aws-login='read -p "Enter the MFA code: " MFA_TOKEN_CODE_ && aws_login --serial-number "$MFA_DEVICE_ARN_" --token-code \$MFA_TOKEN_CODE_'
EOF
echo "Finished setting up AWS"
fi
terraform -install-autocomplete
consul -autocomplete-install
nomad -autocomplete-install
packer -autocomplete-install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment