From 59aad8bf8bb44e3c73ac076be863200f160bcd22 Mon Sep 17 00:00:00 2001 From: Jesse Pretorius Date: Fri, 14 Oct 2016 17:15:37 +0100 Subject: [PATCH] Automatically source the rc file for ansible/ansible-playbook For Newton onwards, when both ansible and ansible-playbook commands are run the /usr/local/bin/openstack-ansible.rc file needs to be sourced before executing the commands. It's very often the case that we get reports that commands have failed because deployers are trying to use 'ansible-playbook' instead of 'openstack-ansible'. This patch ensures that both these commands execute using the openstack-ansible wrapper to ensure that the rc file is sourced appropriately and that /etc/openstack_deploy/user*.yml are included as extra vars under the right circumstances. The wrapper only implements the sourced config and extra vars if the ansible/ansible-playbook commands are executed from the git clone root directory. This protection has been added in order to ensure that non-OSA execution is not interfered with. As learned and applied in https://review.openstack.org/385511 the order of parameters is changed to ensure that the playbook is before all other parameters. Change-Id: I6706dcca6046addb57733b53bc1610644bcbb2d1 Closes-Bug: #1633411 --- scripts/bootstrap-aio.sh | 11 +++- scripts/bootstrap-ansible.sh | 111 +++++++++++++++++++++++++---------- scripts/gate-check-commit.sh | 14 +++-- scripts/scripts-library.sh | 2 +- 4 files changed, 98 insertions(+), 40 deletions(-) diff --git a/scripts/bootstrap-aio.sh b/scripts/bootstrap-aio.sh index df778006c4..ac03ddc77c 100755 --- a/scripts/bootstrap-aio.sh +++ b/scripts/bootstrap-aio.sh @@ -25,8 +25,13 @@ export BOOTSTRAP_OPTS=${BOOTSTRAP_OPTS:-''} # Run AIO bootstrap playbook pushd tests - ansible-playbook -i test-inventory.ini \ - -e "${BOOTSTRAP_OPTS}" \ - bootstrap-aio.yml + if [ -z "${BOOTSTRAP_OPTS}" ]; then + ansible-playbook bootstrap-aio.yml \ + -i test-inventory.ini + else + ansible-playbook bootstrap-aio.yml \ + -i test-inventory.ini \ + -e "${BOOTSTRAP_OPTS}" + fi popd diff --git a/scripts/bootstrap-ansible.sh b/scripts/bootstrap-ansible.sh index 2af7f4f068..5b00198b43 100755 --- a/scripts/bootstrap-ansible.sh +++ b/scripts/bootstrap-ansible.sh @@ -41,6 +41,9 @@ info_block "Checking for required libraries." 2> /dev/null || ## Main ---------------------------------------------------------------------- info_block "Bootstrapping System with Ansible" +# Store the clone repo root location +export OSA_CLONE_DIR="$(pwd)" + # Set the variable to the role file to be the absolute path ANSIBLE_ROLE_FILE="$(readlink -f "${ANSIBLE_ROLE_FILE}")" @@ -106,32 +109,23 @@ ${PIP_COMMAND} install ${PIP_OPTS} ${PIP_INSTALL_OPTIONS} || ${PIP_COMMAND} inst # Install the required packages for ansible $PIP_COMMAND install $PIP_OPTS -r requirements.txt ${ANSIBLE_PACKAGE} || $PIP_COMMAND install --isolated $PIP_OPTS -r requirements.txt ${ANSIBLE_PACKAGE} -# Link the venv installation of Ansible to the local path -pushd /usr/local/bin - find /opt/ansible-runtime/bin/ -name 'ansible*' -exec ln -sf {} \; +# Ensure that Ansible binaries run from the venv +pushd /opt/ansible-runtime/bin + for ansible_bin in $(ls -1 ansible*); do + if [ "${ansible_bin}" == "ansible" ] || [ "${ansible_bin}" == "ansible-playbook" ]; then + + # For the 'ansible' and 'ansible-playbook' commands we want to use our wrapper + ln -sf /usr/local/bin/openstack-ansible /usr/local/bin/${ansible_bin} + + else + + # For any other commands, we want to link directly to the binary + ln -sf /opt/ansible-runtime/bin/${ansible_bin} /usr/local/bin/${ansible_bin} + + fi + done popd -# If the Ansible plugins are in the old location remove them. -[[ -d "/etc/ansible/plugins" ]] && rm -rf "/etc/ansible/plugins" - -# Update dependent roles -if [ -f "${ANSIBLE_ROLE_FILE}" ]; then - if [[ "${ANSIBLE_ROLE_FETCH_MODE}" == 'galaxy' ]];then - # Pull all required roles. - ansible-galaxy install --role-file="${ANSIBLE_ROLE_FILE}" \ - --force - elif [[ "${ANSIBLE_ROLE_FETCH_MODE}" == 'git-clone' ]];then - pushd tests - ansible-playbook -i "localhost ansible-connection=local," \ - get-ansible-role-requirements.yml \ - -e role_file="${ANSIBLE_ROLE_FILE}" - popd - else - echo "Please set the ANSIBLE_ROLE_FETCH_MODE to either of the following options ['galaxy', 'git-clone']" - exit 99 - fi -fi - # Copy the OSA Ansible rc file into place if [[ ! -f "/usr/local/bin/openstack-ansible.rc" ]]; then cp scripts/openstack-ansible.rc /usr/local/bin/openstack-ansible.rc @@ -161,21 +155,74 @@ cat > /usr/local/bin/openstack-ansible < /dev/null; then + + # Discover the variable files. + VAR1="\$(for i in \$(ls /etc/openstack_deploy/user_*.yml); do echo -ne "-e @\$i "; done)" + + # Provide information on the discovered variables. + info "Variable files: \"\${VAR1}\"" + + fi + +else + + # If you're not executing 'openstack-ansible' and are + # not in the OSA git clone root, then do not source + # the configuration and do not add extra vars. + VAR1="" + +fi + +# Execute the Ansible command. +if [ "\${RUN_CMD}" == "openstack-ansible" ] || [ "\${RUN_CMD}" == "ansible-playbook" ]; then + /opt/ansible-runtime/bin/ansible-playbook \${@} \${VAR1} +else + /opt/ansible-runtime/bin/\${RUN_CMD} "\${@}" +fi EOF # Ensure wrapper tool is executable chmod +x /usr/local/bin/openstack-ansible -echo "openstack-ansible script created." +echo "openstack-ansible wrapper created." + +# If the Ansible plugins are in the old location remove them. +[[ -d "/etc/ansible/plugins" ]] && rm -rf "/etc/ansible/plugins" + +# Update dependent roles +if [ -f "${ANSIBLE_ROLE_FILE}" ]; then + if [[ "${ANSIBLE_ROLE_FETCH_MODE}" == 'galaxy' ]];then + # Pull all required roles. + ansible-galaxy install --role-file="${ANSIBLE_ROLE_FILE}" \ + --force + elif [[ "${ANSIBLE_ROLE_FETCH_MODE}" == 'git-clone' ]];then + pushd tests + ansible-playbook get-ansible-role-requirements.yml \ + -i ${OSA_CLONE_DIR}/tests/test-inventory.ini \ + -e role_file="${ANSIBLE_ROLE_FILE}" + popd + else + echo "Please set the ANSIBLE_ROLE_FETCH_MODE to either of the following options ['galaxy', 'git-clone']" + exit 99 + fi +fi + echo "System is bootstrapped and ready for use." diff --git a/scripts/gate-check-commit.sh b/scripts/gate-check-commit.sh index 31310adf7e..bb22fa9bf4 100755 --- a/scripts/gate-check-commit.sh +++ b/scripts/gate-check-commit.sh @@ -72,10 +72,16 @@ iptables -P OUTPUT ACCEPT # Bootstrap an AIO pushd "$(dirname "${0}")/../tests" - ansible-playbook -i test-inventory.ini \ - -e "${BOOTSTRAP_OPTS}" \ - ${ANSIBLE_PARAMETERS} \ - bootstrap-aio.yml + if [ -z "${BOOTSTRAP_OPTS}" ]; then + ansible-playbook bootstrap-aio.yml \ + -i test-inventory.ini \ + ${ANSIBLE_PARAMETERS} + else + ansible-playbook bootstrap-aio.yml \ + -i test-inventory.ini \ + -e "${BOOTSTRAP_OPTS}" \ + ${ANSIBLE_PARAMETERS} + fi popd # Implement the log directory diff --git a/scripts/scripts-library.sh b/scripts/scripts-library.sh index 4d835b547a..d93316f177 100755 --- a/scripts/scripts-library.sh +++ b/scripts/scripts-library.sh @@ -82,7 +82,7 @@ function successerator { function install_bits { # Use the successerator to run openstack-ansible - successerator openstack-ansible ${ANSIBLE_PARAMETERS} $@ + successerator openstack-ansible $@ ${ANSIBLE_PARAMETERS} } function ssh_key_create {