Skip to content

Instantly share code, notes, and snippets.

@hvanderlaan
Created April 19, 2016 13:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hvanderlaan/3623905c4383683e1616ae38e9bb2207 to your computer and use it in GitHub Desktop.
Save hvanderlaan/3623905c4383683e1616ae38e9bb2207 to your computer and use it in GitHub Desktop.
create ansible alpine linux container
#!/bin/bash
# file : create-lxc-alpine-ansible-node.sh
# purpose: deploy a new alpine container that is the ansible server
#
# author : harald van der laan
# date : 2016/04/19
# version: v1.0
lxcName="ansible01"
if [ -z $(which lxc) ]; then
echo "[-]: lxc is not installed please install lxc"
echo "[ ]: sudo apt-add-repository ppa:ubuntu-lxc/lxd-stable"
echo "[ ]: sudo apt-get update"
echo "[ ]: sudo apt-get install lxd"
exit 1
fi
lxcAlpineImage=$(lxc image list | egrep "^\|\ alpine\ " &> /dev/null; echo ${?})
lxcContainerExists=$(lxc list | egrep "^\|\ ${lxcName}\ " &> /dev/null; echo ${?})
if [ ${lxcAlpineImage} -ne 0 ]; then
echo "[ ]: downloading alpine linux 3.3"
lxc image copy images:alpine/3.3/amd64 local: --alias=alpine &> /dev/null
fi
if [ ${lxcContainerExists} -eq 0 ]; then
echo "[-]: there is already a container with the name: ${lxcName}"
exit 1
fi
echo "[ ]: starting alpine linux 3.3"
lxc launch alpine ${lxcName} &> /dev/null
sleep 2
echo "[ ]: update alpine linux 3.3"
lxc exec ${lxcName} -- apk --update-cache upgrade &> /dev/null
echo "[ ]: installing required packages"
lxc exec ${lxcName} -- apk add openssh git vim bash bash-completion nmap python py-pip py-crypto &> /dev/null
echo "[ ]: installing python modules"
lxc exec ${lxcName} -- pip install -U pip &> /dev/null
lxc exec ${lxcName} -- pip install -U pysphere &> /dev/null
lxc exec ${lxcName} -- pip install -U pywinrm &> /dev/null
echo "[ ]: installing ansible"
lxc exec ${lxcName} -- apk --update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ add ansible &> /dev/null
echo "[ ]: configure openssh"
lxc exec ${lxcName} -- mkdir -p /root/.ssh
lxc file push ~/.ssh/id_rsa.pub ${lxcName}/root/.ssh/authorized_keys --uid=0 --gid=0 --mode=0644
lxc exec ${lxcName} -- rc-update add sshd &> /dev/null
lxc exec ${lxcName} -- /etc/init.d/sshd start &> /dev/null
lxc exec ${lxcName} -- ssh-keygen -t rsa
lxc file pull ${lxcName}/root/.ssh/id_rsa.pub ${lxcName}.pub
echo "[+]: ansible server is ready to use."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment