Install and Configure Ansible AWX on Ubuntu 18+

Complete installation script for deploying AWX on Ubuntu with Kubernetes

Install and Configure Ansible AWX on Ubuntu 18+

Since AWX moved the primary installation method to Kubernetes, it’s become a little more difficult to setup a test/dev installation of AWX. Nearly every guide I could find on the internet was written for AWX 17 and below, so I assembled this script to deploy Ansible AWX on Ubuntu 18 and above.

Requirements

  • SSH access to Ubuntu 18/20 host which will run AWX (system requires 4 CPUs and 4GB RAM)
  • Logged-in user account with sudo access
  • Hostname set correctly such that hostname and hostname --fqdn return the correct responses

Run script from GitHub

wget https://raw.githubusercontent.com/aderusha/aderusha.github.io/master/scripts/ubuntu-minikube-awx.sh
chmod +x ubuntu-minikube-awx.sh
./ubuntu-minikube-awx.sh

ubuntu-minikube-awx.sh

#!/bin/bash

# Color codes for progress text
WHITE='\033[1;37m'
GREEN='\033[0;32m'
NC='\033[0m'

# This script needs to run with permissions granted to the docker user group later, so check all of that first
DOCKERGROUP=docker
if [ ! $(getent group ${DOCKERGROUP}) ]; then
  echo -e "${WHITE}$(date --iso-8601=seconds) ${GREEN}Creating group ${DOCKERGROUP}${NC}"
  sudo groupadd ${DOCKERGROUP}
fi

if [ ! $(getent group ${DOCKERGROUP} | grep -q ":${USER}" ) ]; then
  echo -e "${WHITE}$(date --iso-8601=seconds) ${GREEN}Adding user ${USER} to group ${DOCKERGROUP}${NC}"
  sudo usermod -aG ${DOCKERGROUP} ${USER}
fi

if [ $(id -gn) != ${DOCKERGROUP} ]; then
  echo -e "${WHITE}$(date --iso-8601=seconds) ${GREEN}Relaunching with group ${DOCKERGROUP}${NC}"
  exec sg ${DOCKERGROUP} "$0 $*"
fi

# download and install docker
echo -e "${WHITE}$(date --iso-8601=seconds) ${GREEN}Downloading and installing Docker${NC}"
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io

sudo systemctl restart docker

# install kubectl snap
echo -e "${WHITE}$(date --iso-8601=seconds) ${GREEN}Downloading and installing kubectl${NC}"
sudo snap install kubectl --classic

# install minikube
echo -e "${WHITE}$(date --iso-8601=seconds) ${GREEN}Downloading and installing minikube${NC}"
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_latest_amd64.deb
sudo dpkg -i minikube_latest_amd64.deb
minikube start --addons=ingress --install-addons=true --kubernetes-version=stable --driver=docker 

# confirm permissions on minikube dirs
sudo chown -R $USER $HOME/.kube $HOME/.minikube
chmod -R u+wrx $HOME/.kube $HOME/.minikube

# show running pods
minikube kubectl -- get pods -A

echo -e "${WHITE}$(date --iso-8601=seconds) ${GREEN}Creating and starting minikube services${NC}"
# setup minikube as a service to launch on start
sudo tee /etc/systemd/system/minikube.service << EOF
[Unit]
Description=minikube Kubernetes service
After=network.target

[Install]
WantedBy=multi-user.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/minikube start
ExecStop=/usr/bin/minikube stop
Group=docker
User=${USER}
EOF

# reload systemd and enable the service
sudo systemctl --system daemon-reload
sudo systemctl enable minikube.service
sudo systemctl start minikube.service

# setup k8s dashboard as a service to launch on start
sudo tee /etc/systemd/system/minikube-dashboard.service << EOF
[Unit]
Description=minikube Kubernetes Dashboard UI
After=minikube.service

[Install]
WantedBy=multi-user.target

[Service]
ExecStart=/usr/bin/minikube dashboard
Group=docker
User=${USER}
EOF

# reload systemd and enable the service
sudo systemctl --system daemon-reload
sudo systemctl enable minikube-dashboard.service
sudo systemctl start minikube-dashboard.service

# setup kubectl proxy for dashboard, leave disabled
sudo tee /etc/systemd/system/kubectl-proxy-8001.service << EOF
[Unit]
Description=minikube Kubernetes proxy service 8001
After=minikube-dashboard.service

[Install]
WantedBy=multi-user.target

[Service]
ExecStart=/bin/bash -c "/snap/bin/kubectl proxy --address 0.0.0.0 --port=8001 --accept-hosts '.*'"
Group=docker
User=${USER}
EOF

# reload systemd and disable the service (k8s dashboard has no user auth, enable as needed)
sudo systemctl --system daemon-reload
sudo systemctl disable kubectl-proxy-8001.service

echo -e "${WHITE}$(date --iso-8601=seconds) ${GREEN}Deploying AWX Operator${NC}"

AWX_OPERATOR_RELEASE=$(curl --silent "https://api.github.com/repos/ansible/awx-operator/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
minikube kubectl -- apply -f https://raw.githubusercontent.com/ansible/awx-operator/${AWX_OPERATOR_RELEASE}/deploy/awx-operator.yaml

# continue once STATUS == Running
echo -e "${WHITE}$(date --iso-8601=seconds) ${GREEN}Waiting for AWX Operator${NC}"
until [ $(minikube kubectl get pods | grep "awx-operator.*Running" | wc -l) -ge "1" ]; do 
  sleep 1
done

# create deployment file
echo -e "${WHITE}$(date --iso-8601=seconds) ${GREEN}Deploying AWX${NC}"
cat << EOF >> awx-deploy.yml
---
apiVersion: awx.ansible.com/v1beta1
kind: AWX
metadata:
  name: $(hostname)
spec:
  service_type: NodePort
  ingress_type: ingress
  hostname: $(hostname --fqdn)
EOF

# execute deployment file
minikube kubectl apply -- -f awx-deploy.yml

# wait until we have 2 pods running which start with "${HOSTNAME}-"
echo -e "${WHITE}$(date --iso-8601=seconds) ${GREEN}Waiting for AWX${NC}"
until [ $(minikube kubectl get pods | grep "${HOSTNAME}-.*Running" | wc -l) -ge "2" ]
do 
  sleep 1
done

# create a port forward service for access to AWX
echo -e "${WHITE}$(date --iso-8601=seconds) ${GREEN}Creating AWX port forward${NC}"
sudo tee /etc/systemd/system/kubectl-awx-forward.service << EOF
[Unit]
Description=minikube AWX port forward 80
After=minikube.service

[Install]
WantedBy=multi-user.target

[Service]
ExecStart=/bin/bash -c "/snap/bin/kubectl port-forward --address=0.0.0.0 --namespace=default  --kubeconfig /root/.kube/config service/${HOSTNAME}-service 80:80"
EOF

# copy kubectl config to root homedir for service
sudo cp -R $HOME/.kube /root

# reload systemd and enable the service
sudo systemctl --system daemon-reload
sudo systemctl enable kubectl-awx-forward.service
sudo systemctl start kubectl-awx-forward.service

# continue after AWX upgrade
echo -e "${WHITE}$(date --iso-8601=seconds) ${GREEN}Waiting for AWX upgrade to complete${NC}"
sleep 30
while wget -qO- http://127.0.0.1 | grep "AWX is currently upgrading" > /dev/null;
do 
  sleep 1
done

# collect the password for AWX user "admin"
AWX_ADMINPASS=$(minikube kubectl -- get secret ${HOSTNAME}-admin-password -o jsonpath='{.data.password}' | base64 --decode)
echo -e "${WHITE}$(date --iso-8601=seconds) ${GREEN}AWX installed and running, access with user ${WHITE}admin${GREEN} and password ${WHITE}${AWX_ADMINPASS}"

Comments