Skip to content

Instantly share code, notes, and snippets.

@adamrushuk
Last active January 10, 2019 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamrushuk/aac1146af536e4648d5c7644d0beca10 to your computer and use it in GitHub Desktop.
Save adamrushuk/aac1146af536e4648d5c7644d0beca10 to your computer and use it in GitHub Desktop.
Vagrant provisioning scripts for Azure / Ansible blog post: https://adamrushuk.github.io/azure-provisioning-ansible-awx/
---
# Update with your Azure Service Principal credentials
subscription: "aaaa1111-bbbb-cccc-abcd-aaabbbcccddd"
client: "abcd1234-abcd-efff-1234-abcd12345678"
secret: "MyStrongPassw0rd!"
tenant: "12345678-ab12-cd34-ef56-1234abcd5678"
#!/bin/bash
# Vars
awx_host_url="http://192.168.10.20"
awx_username="admin"
awx_password="password"
awx_projects_source_folder="/vagrant/ansible-projects/"
awx_projects_dest_folder="/var/lib/awx/projects"
azure_credential_file_path="/vagrant/azure_ansible_credentials.yml"
ssh_public_key_path="$HOME/.ssh/id_rsa.pub"
awx_http_port_check=80
awx_demo_data_import_check="tower-cli instance_group get tower 2> /dev/null"
# Create SSH key
if [ ! -f "$ssh_public_key_path" ]
then
echo -e "\nINFO: Started Creating new SSH key..."
echo -e "\n\n\n" | ssh-keygen -t rsa -C "dev@adamrushuk.github.io" -N ""
else
echo -e "\nINFO: SSH key already exists...SKIPPING."
fi
ssh_public_key=`cat "$ssh_public_key_path"`
# Configure Ansible AWX using Tower CLI
echo -e "\nINFO: Started Configuring Ansible AWX using Tower CLI..."
# Configure host - include "http:" as it default to HTTPS
tower-cli config host $awx_host_url
# Disable SSL verification to allow insecure HTTP traffic
tower-cli config verify_ssl false
# Configure login
tower-cli config username $awx_username
tower-cli config password $awx_password
# Wait for AWX Web Server to be online
while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' localhost:$awx_http_port_check)" -ne "200" ]]; do
echo "INFO: AWX Web Server NOT online yet...waiting 30 seconds"
sleep 30
done
echo "INFO: AWX Web Server now online...READY"
# Wait for AWX Demo Data import to finish
eval $awx_demo_data_import_check
while [[ $? -ne 0 ]]; do
echo "INFO: AWX Data Import not complete yet...waiting 5 seconds"
sleep 5
eval $awx_demo_data_import_check
done
echo "INFO: AWX Data Import now complete"
# Copy projects folder
if [ -d "$awx_projects_source_folder" ]
then
echo -e "\nINFO: Copying Ansible Projects folder(s) for AWX..."
rsync -avz "$awx_projects_source_folder"* $awx_projects_dest_folder
else
echo -e "\nINFO: Ansible Projects source folder missing...SKIPPING."
fi
# Create project
echo -e "\nINFO: Creating Azure Project in AWX..."
tower-cli project create --name "Azure Project" --description "Azure Playbooks" --scm-type "manual" --local-path "azure-linux-vm" --organization "Default"
# Create Azure inventory
echo -e "\nINFO: Creating Azure Inventory in AWX..."
tower-cli inventory create --name "Azure Inventory" --description "Azure Inventory" --organization "Default" --variables "ssh_public_key: \"$ssh_public_key\""
# Create Azure credential
echo -e "\nINFO: Creating Azure Credential in AWX..."
tower-cli credential create --name "Azure Credential" --description "Azure Credential" --organization "Default" --credential-type "Microsoft Azure Resource Manager" --inputs "@$azure_credential_file_path"
# Create Azure job template for a simple Resource Group
echo -e "\nINFO: Creating job template for a simple Azure Resource Group..."
# WORKAROUND: you must supply an SSH credential type initially
tower-cli job_template create --name "Azure Resource Group" --description "Azure Resource Group - Job Template" --inventory "Azure Inventory" --project "Azure Project" --playbook "resource_group.yml" --credential "Demo Credential"
# WORKAROUND: you can then associate an Azure credential afterwards
tower-cli job_template associate_credential --job-template "Azure Resource Group" --credential "Azure Credential"
# Create Azure job template for a CentOS Linux VM and all required resources
echo -e "\nINFO: Creating job template for a CentOS Linux VM and all required resources in Azure..."
# WORKAROUND: you must supply an SSH credential type initially
tower-cli job_template create --name "Azure CentOS Linux VM" --description "Azure CentOS Linux VM - Job Template" --inventory "Azure Inventory" --project "Azure Project" --playbook "centos_vm.yml" --credential "Demo Credential"
# WORKAROUND: you can then associate an Azure credential afterwards
tower-cli job_template associate_credential --job-template "Azure CentOS Linux VM" --credential "Azure Credential"
#!/bin/bash
# Installs Ansible AWX
echo "INFO: Started Installing Ansible AWX..."
# Install prereq: Docker SDK for Python
echo "INFO: Started Installing Docker SDK for Python..."
pip install docker
echo "INFO: Finished Installing Docker SDK for Python."
# Clone AWX repo
ansible_git_folder="/root/awx/"
if [ ! -d "$ansible_git_folder" ]
then
echo "INFO: Cloning Ansible AWX repo..."
cd /root/
git clone https://github.com/ansible/awx.git
else
echo "INFO: Ansible AWX repo already exists...SKIPPING."
fi
# Copy (overwrite) inventory file from vagrant share
echo "INFO: Copying Ansible AWX Inventory file..."
\cp /vagrant/vagrant/scripts/awx_inventory.ini /root/awx/installer/inventory
# Run installer Playbook
echo "INFO: Running Ansible AWX install playbook..."
cd /root/awx/installer/
ansible-playbook -i inventory install.yml
# Install Ansible Tower CLI
pip install ansible-tower-cli
echo "INFO: Finished Installing Ansible AWX. View containers with 'docker ps'
INFO: Confirm AWX migration tasks have completed by running 'docker logs -f awx_task'
INFO: Look for these Final migration log messages:
Default organization added
Demo Credential, Inventory, and Job Template added
Successfully registered instance awx
Creating instance group tower"
#!/bin/bash
# Install Ansible
echo "INFO: Started Installing Ansible for Azure..."
# Install pre-requisite packages
sudo yum check-update; sudo yum install -y gcc libffi-devel python-devel openssl-devel epel-release
sudo yum install -y python-pip python-wheel
# Install Ansible and Azure SDKs via pip
sudo pip install ansible[azure]
# Add support for Windows via WinRM
sudo pip install pywinrm
echo "INFO: Finished Installing Ansible for Azure."
#!/bin/bash
# Install common utils
echo "INFO: Started Installing Extra Packages Repo and useful utils..."
yum -y install epel-release --enablerepo=extras
yum -y update
yum -y install tree git vim bash-completion
yum -y install python-pip
pip install pip --upgrade
echo "INFO: Finished Installing Extra Packages Repo and useful utils."
#!/bin/bash
# Installs Docker CE
echo "INFO: Started Installing Docker..."
# Remove older versions
# yum -y remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-selinux docker-engine-selinux docker-engine
# Set up repo
yum -y install yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# Install Docker
yum -y install docker-ce
# Start Docker and enable auto-start on boot
systemctl start docker
systemctl enable docker
# Check Docker status
systemctl status docker
# Check Docker version (was "Docker version 18.09.0, build 4d60db4" during testing)
docker -v
echo "INFO: Finished Installing Docker."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment