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
This commit is contained in:
Jesse Pretorius 2016-10-14 17:15:37 +01:00
parent 398d8afc40
commit 59aad8bf8b
4 changed files with 98 additions and 40 deletions

View File

@ -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

View File

@ -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 <<EOF
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}"
function info() {
echo -e "\e[0;35m\${@}\e[0m"
if [ "\${ANSIBLE_NOCOLOR:-0}" -eq "1" ]; then
echo -e "\${@}"
else
echo -e "\e[0;35m\${@}\e[0m"
fi
}
# Discover the variable files.
VAR1="\$(for i in \$(ls /etc/openstack_deploy/user_*.yml); do echo -ne "-e @\$i "; done)"
# Figure out which Ansible binary was executed
RUN_CMD=\$(basename \${0})
# Provide information on the discovered variables.
info "Variable files: \"\${VAR1}\""
# Apply the OpenStack-Ansible configuration selectively.
if [[ "\${PWD}" == *"${OSA_CLONE_DIR}"* ]] || [ "\${RUN_CMD}" == "openstack-ansible" ]; then
# Run the ansible playbook command.
. /usr/local/bin/openstack-ansible.rc && \$(which ansible-playbook) \${VAR1} \$@
# Source the Ansible configuration.
. /usr/local/bin/openstack-ansible.rc
# Check whether there are any user configuration files
if ls -1 /etc/openstack_deploy/user_*.yml &> /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."

View File

@ -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

View File

@ -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 {