Last active
January 12, 2021 12:50
-
-
Save hvanderlaan/9d47c4c9ba7cef82a36b4d023ad8264a to your computer and use it in GitHub Desktop.
lxc deploy alpine linux ansible ready
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# file : deploy-lxc-alpine.sh | |
# purpose: deploy a new alpine container that is ready for ansible management | |
# | |
# author : harald van der laan | |
# date : 2016/04/19 | |
# version: v1.0 | |
lxcName=${1} | |
if [ -z ${lxcName} ]; then | |
echo "[-]: usage: ${0} <container name>" | |
exit 1 | |
fi | |
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 container" | |
lxc launch alpine ${lxcName} &> /dev/null | |
sleep 2 | |
echo "[ ]: upgrade alpine linux 3.3 container" | |
lxc exec ${lxcName} -- apk --update-cache upgrade &> /dev/null | |
echo "[ ]: installing requirements for ansible management" | |
lxc exec ${lxcName} -- apk add openssh python &> /dev/null | |
echo "[ ]: injecting public key of ansible server" | |
lxc exec ${lxcName} -- mkdir -p /root/.ssh | |
if [ -f "ansible.pub" ]; then | |
lxc file push ansible.pub ${lxcName}/root/.ssh/authorized_keys --uid=0 --gid=0 --mode=0644 | |
else | |
echo "[-]: could not find the ansible public key" | |
echo "[ ]: please set a password on the root user" | |
echo "[ ]: lxc exec ${lxcName} -- passwd root" | |
fi | |
echo "[ ]: enable and starting ssh server" | |
lxc exec ${lxcName} -- rc-update add sshd &> /dev/null | |
lxc exec ${lxcName} -- /etc/init.d/sshd start &> /dev/null | |
echo "[+]: container ${lxcName} is ready for ansible management" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment