From 3bfd530fdada4a4704879867ec762cea5800a79b Mon Sep 17 00:00:00 2001 From: Romain Bossart Date: Wed, 4 Mar 2015 08:04:12 +0100 Subject: [PATCH] terser hostfile fallback mechanism --- src/_ansible | 18 ++++-------------- src/_ansible-playbook | 19 +++++-------------- 2 files changed, 9 insertions(+), 28 deletions(-) diff --git a/src/_ansible b/src/_ansible index 5a10e3a..0440e3e 100644 --- a/src/_ansible +++ b/src/_ansible @@ -58,20 +58,10 @@ __host_file_location () { # 2. else check /etc/ansible/hosts or /usr/local/etc/... # (depending on platform) # - if [ -n "${ANSIBLE_HOSTS}" ]; then # use envvar - HOST_FILE=${ANSIBLE_HOSTS} - else - if [[ "$OSTYPE" == darwin* ]]; then - CANDIDATE="/usr/local/etc/ansible/hosts" - else - CANDIDATE="/etc/ansible/hosts" - fi - if [ -f "$CANDIDATE" ]; then - HOST_FILE=${CANDIDATE} - else - HOST_FILE=/dev/null - fi - fi + [[ "$OSTYPE" == darwin* ]] && FALLBACK="/usr/local/etc/ansible/hosts" + [[ "$OSTYPE" == linux* ]] && FALLBACK="/etc/ansible/hosts" + HOST_FILE=${ANSIBLE_HOSTS:=${FALLBACK}} + [[ -f ${HOST_FILE} ]] || HOST_FILE=/dev/null echo ${HOST_FILE} } diff --git a/src/_ansible-playbook b/src/_ansible-playbook index 4b641fc..bbeccc0 100644 --- a/src/_ansible-playbook +++ b/src/_ansible-playbook @@ -56,20 +56,11 @@ __host_file_location () { # 2. else check /etc/ansible/hosts or /usr/local/etc/... # (depending on platform) # - if [ -n "${ANSIBLE_HOSTS}" ]; then # use envvar - HOST_FILE=${ANSIBLE_HOSTS} - else - if [[ "$OSTYPE" == darwin* ]]; then - CANDIDATE="/usr/local/etc/ansible/hosts" - else - CANDIDATE="/etc/ansible/hosts" - fi - if [ -f "$CANDIDATE" ]; then - HOST_FILE=${CANDIDATE} - else - HOST_FILE=/dev/null - fi - fi + + [[ "$OSTYPE" == darwin* ]] && FALLBACK="/usr/local/etc/ansible/hosts" + [[ "$OSTYPE" == linux* ]] && FALLBACK="/etc/ansible/hosts" + HOST_FILE=${ANSIBLE_HOSTS:=${FALLBACK}} + [[ -f ${HOST_FILE} ]] || HOST_FILE=/dev/null echo ${HOST_FILE} }