Skip to content

Instantly share code, notes, and snippets.

@tnt
Created April 10, 2015 08:49
Show Gist options
  • Save tnt/f7e5020029b8c65d3f3c to your computer and use it in GitHub Desktop.
Save tnt/f7e5020029b8c65d3f3c to your computer and use it in GitHub Desktop.
diff --git a/ansible/centos-specifics.sh b/ansible/centos-specifics.sh
new file mode 100644
index 0000000..9a23d57
--- /dev/null
+++ b/ansible/centos-specifics.sh
@@ -0,0 +1,27 @@
+function package_update_cache {
+ local FORCE=$1
+ CU_TIME_FILE=/tmp/yum_last_upd_cch
+ if [ -n "$FORCE" ] || [ ! -f $CU_TIME_FILE ] || [ $(date +%s) -gt $(($(date -r $CU_TIME_FILE +%s) + 3600)) ]; then
+ echo -e "${GREEN}Updating yum cache${NORMAL}"
+ with_root yum -q makecache
+ [[ "$?" == "0" ]] && touch $CU_TIME_FILE
+ fi
+}
+
+function package_install {
+ local MESSAGE=$1
+ shift
+ local PACKAGE_NAMES=$@
+ if ! rpm -q $PACKAGE_NAMES; then
+ package_update_cache
+ echo -e "$MESSAGE"
+ with_root yum install -q -y $PACKAGE_NAMES
+ fi
+}
+
+ANSIBLE_DEPENDENCIES="git PyYAML python-paramiko python-jinja2 python-pip sshpass"
+
+if [[ ! -f "/etc/yum.repos.d/epel.repo" ]]; then
+ package_install "${GREEN}Installing EPEL repo${NORMAL}" epel-release
+ package_update_cache force
+fi
diff --git a/ansible/git-install.sh b/ansible/git-install.sh
index fea0408..4096437 100644
--- a/ansible/git-install.sh
+++ b/ansible/git-install.sh
@@ -25,9 +25,10 @@ set -e
# --- configuration defaults ---
ANSIBLE_PROJECT_FOLDER=${ANSIBLE_PROJECT_FOLDER:=$(pwd)}
ANSIBLE_DIR=${ANSIBLE_DIR:=/opt/ansible}
-ANSIBLE_VERSION=${ANSIBLE_VERSION:=1.8.2}
+ANSIBLE_VERSION=${ANSIBLE_VERSION:=1.9.0.1-1}
ANSIBLE_GIT_REPO=${ANSIBLE_GIT_REPO:=git://github.com/ansible/ansible.git}
ANSIBLE_GIT_BRANCH=${ANSIBLE_GIT_BRANCH:=release$ANSIBLE_VERSION}
+ANSIBLE_GIT_TAG=${ANSIBLE_GIT_TAG:=v$ANSIBLE_VERSION}
# --- constants ---
RED='\033[0;31m'
@@ -48,27 +49,24 @@ function with_root {
fi
}
-function apt_get_update {
- if [ "$(date +%F-%H)" != "date -r /var/lib/apt/periodic/update-success-stamp +%F-%H" ]; then
- echo -e "${GREEN}Updating apt cache${NORMAL}"
- with_root apt-get update -qq
- fi
+function get_distro_info {
+ local VAR_NAME=$1
+ . /etc/os-release
+ eval echo \$$VAR_NAME
}
-function apt_get_install {
- local MESSAGE=$1
- shift
- local PACKAGE_NAMES=$@
- if [ ! -z "$(apt-get -qq -s -o=APT::Get::Show-User-Simulation-Note=no install $PACKAGE_NAMES)" ]; then
- apt_get_update
- echo -e "$MESSAGE"
- with_root apt-get install -y $PACKAGE_NAMES
- fi
-}
+DISTRO_SPECIFICS_FILE="$PROJECT_FOLDER/$VAGRANT_ANSIBLE_REMOTE/ansible/$(get_distro_info ID)-specifics.sh"
+
+if [[ -f "$DISTRO_SPECIFICS_FILE" ]]; then
+ source "$DISTRO_SPECIFICS_FILE"
+else
+ echo "${RED}Can't find '$DISTRO_SPECIFICS_FILE'!${NORMAL}"
+ exit 1
+fi
# --- remove mismatching version ---
if [ -f ${ANSIBLE_DIR}/VERSION ]; then
- if [ $(<${ANSIBLE_DIR}/VERSION) != $ANSIBLE_VERSION ]; then
+ if [ "$(<${ANSIBLE_DIR}/VERSION)" != "${ANSIBLE_VERSION/-/ }" ]; then
echo -e "${RED}Removing old Ansible version $(<${ANSIBLE_DIR}/VERSION)${NORMAL}"
if [ -w ${ANSIBLE_DIR} ]; then
rm -rf ${ANSIBLE_DIR}
@@ -79,14 +77,15 @@ if [ -f ${ANSIBLE_DIR}/VERSION ]; then
fi
# --- dependencies ---
-apt_get_install "${GREEN}Installing Ansible dependencies and Git${NORMAL}" \
- git python-yaml python-paramiko python-jinja2 sshpass
+package_install "${GREEN}Installing Ansible dependencies and Git${NORMAL}" $ANSIBLE_DEPENDENCIES
# --- install ---
if [ ! -d $ANSIBLE_DIR ]; then
echo -e "${GREEN}Cloning Ansible${NORMAL}"
mkdir -p $ANSIBLE_DIR 2>/dev/null || GIT_PREFIX="with_root "
- $GIT_PREFIX git clone --recurse-submodules --branch $ANSIBLE_GIT_BRANCH --depth 1 $ANSIBLE_GIT_REPO $ANSIBLE_DIR
+ # $GIT_PREFIX git clone --recurse-submodules --branch $ANSIBLE_GIT_BRANCH --depth 1 $ANSIBLE_GIT_REPO $ANSIBLE_DIR
+ # $GIT_PREFIX bash $ANSIBLE_DIR/hacking/env-setup
+ $GIT_PREFIX git clone --recurse-submodules --branch $ANSIBLE_GIT_TAG --depth 1 $ANSIBLE_GIT_REPO $ANSIBLE_DIR
fi
SOURCE_ANSIBLE=true
diff --git a/ansible/run.sh b/ansible/run.sh
index 6562306..410ded4 100644
--- a/ansible/run.sh
+++ b/ansible/run.sh
@@ -81,9 +81,15 @@ fi
# --- use non system Ansible ---
if [ "$SOURCE_ANSIBLE" == true ]; then
- echo -e "${GREEN}Using Ansible from ${ANSIBLE_DIR} v$(cat $ANSIBLE_DIR/VERSION)${NORMAL}"
+ ANSIBLE_VERSION_FROM_FILE=$(grep -Eo "^[^ ]+" $ANSIBLE_DIR/VERSION)
+ echo -e "${GREEN}Using Ansible from ${ANSIBLE_DIR} v${ANSIBLE_VERSION_FROM_FILE}${NORMAL}"
cd $ANSIBLE_DIR # switch folder otherwise vagrant folder might not work
- source ${ANSIBLE_DIR}/hacking/env-setup
+ # set +e
+ # source ${ANSIBLE_DIR}/hacking/env-setup
+ # set -e
+ if [ "$(pip show ansible | grep Version | grep -oE "[^ ]+$")" != "$ANSIBLE_VERSION_FROM_FILE" ]; then
+ with_root pip install -e .
+ fi
fi
# --- use ansible-galaxy Rolefile ---
diff --git a/ansible/ubuntu-specifics.sh b/ansible/ubuntu-specifics.sh
new file mode 100644
index 0000000..7c370c0
--- /dev/null
+++ b/ansible/ubuntu-specifics.sh
@@ -0,0 +1,19 @@
+function package_update_cache {
+ if [ "$(date +%F-%H)" != "date -r /var/lib/apt/periodic/update-success-stamp +%F-%H" ]; then
+ echo -e "${GREEN}Updating apt cache${NORMAL}"
+ with_root apt-get update -qq
+ fi
+}
+
+function package_install {
+ local MESSAGE=$1
+ shift
+ local PACKAGE_NAMES=$@
+ if [ ! -z "$(apt-get -qq --simulate -o=APT::Get::Show-User-Simulation-Note=no install $PACKAGE_NAMES)" ]; then
+ package_update_cache
+ echo -e "$MESSAGE"
+ with_root apt-get install -y $PACKAGE_NAMES
+ fi
+}
+
+ANSIBLE_DEPENDENCIES="git python-yaml python-paramiko python-jinja2 python-pip sshpass"
diff --git a/remote.sh b/remote.sh
index afff0ef..074e21f 100644
--- a/remote.sh
+++ b/remote.sh
@@ -19,6 +19,7 @@ PROJECT_FOLDER=${PROJECT_FOLDER:=$(pwd)}
VAGRANT_ANSIBLE_REMOTE=${VAGRANT_ANSIBLE_REMOTE:=vagrant_ansible_remote}
ANSIBLE_PROJECT_FOLDER=${ANSIBLE_PROJECT_FOLDER:=$PROJECT_FOLDER/ansible}
VAGRANT_INVOKED=${VAGRANT_INVOKED:=false}
+# export ANSIBLE_SCP_IF_SSH=${ANSIBLE_SCP_IF_SSH:=true}
# --- contstants ---
RED='\033[0;31m'
diff --git a/vagrant/ssh-ansible.sh b/vagrant/ssh-ansible.sh
index 168a68a..32aa521 100644
--- a/vagrant/ssh-ansible.sh
+++ b/vagrant/ssh-ansible.sh
@@ -46,7 +46,7 @@ if [ ! -z $VAGRANT_SSH_ARGS ]; then
fi
# --- vagrant requires to be in the folder ---
-pushd $PROJECT_FOLDER
+pushd $PROJECT_FOLDER > /dev/null
# --- check that all vagrant machines are up ---
vagrant status --machine-readable | while IFS=, read time name kind state; do
@@ -62,4 +62,4 @@ done
# --- run the command ---
vagrant ssh $VAGRANT_ANSIBLE_MACHINE --command "$COMMAND"$VAGRANT_SSH_ARGS 2>/dev/null
-popd
+popd > /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment