Created
September 11, 2014 16:27
-
-
Save stefanotorresi/39aa4f30220589fd386f to your computer and use it in GitHub Desktop.
Script to run an Ansible playbook inside an Ubuntu virtual machine against a local inventory.
This file contains hidden or 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
| #!/usr/bin/env bash | |
| ANSIBLE_PLAYBOOK=$1 | |
| ANSIBLE_HOSTS=$2 | |
| INVENTORY_FILE="/tmp/ansible_hosts" | |
| function check_arguments() { | |
| if [ -z ${ANSIBLE_PLAYBOOK} ]; then | |
| echo "Usage: ansible-playbook-helper playbook-file [inventory-file|hostname]" | |
| exit 1 | |
| fi | |
| if [ ! -f ${ANSIBLE_PLAYBOOK} ]; then | |
| echo "Missing specified playbook file" | |
| exit 1 | |
| fi | |
| if [ -z ${ANSIBLE_HOSTS} ]; then | |
| ANSIBLE_HOSTS="local" | |
| fi | |
| } | |
| function install() { | |
| if [ $(dpkg-query -W -f='${Status}\n' ansible | grep -c "install ok installed") -eq 1 ]; then | |
| echo "Ansible already found, skipping installation" | |
| return 0 | |
| fi | |
| if [ $(grep ^ /etc/apt/sources.list /etc/apt/sources.list.d/* | grep -c "rquillo/ansible") -eq 0 ]; then | |
| echo "Adding Ansible PPA" | |
| sudo add-apt-repository -y ppa:rquillo/ansible | |
| fi | |
| echo "Installing Ansible" | |
| sudo apt-get update | |
| sudo apt-get install -y ansible | |
| } | |
| function prepare_inventory_file() { | |
| echo "Preparing Ansible inventory" | |
| if [ -f ANSIBLE_HOSTS ]; then | |
| cp ${ANSIBLE_HOSTS} ${INVENTORY_FILE} | |
| return 0 | |
| fi | |
| if [ -z ANSIBLE_HOSTS ]; then | |
| ANSIBLE_HOSTS="local" | |
| fi | |
| echo -e "[${ANSIBLE_HOSTS}]\n127.0.0.1" > ${INVENTORY_FILE} | |
| } | |
| function run() { | |
| echo "Running playbook" | |
| ansible-playbook -s ${ANSIBLE_PLAYBOOK} --inventory-file=${INVENTORY_FILE} --connection=local | |
| rm ${INVENTORY_FILE} | |
| } | |
| check_arguments | |
| install | |
| prepare_inventory_file | |
| run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment