Skip to content

Instantly share code, notes, and snippets.

@AndrianBdn
Last active October 24, 2018 08:44
Show Gist options
  • Save AndrianBdn/ab7e473682d1c4fab9c0ba3fc082de8d to your computer and use it in GitHub Desktop.
Save AndrianBdn/ab7e473682d1c4fab9c0ba3fc082de8d to your computer and use it in GitHub Desktop.
bash script that creates inventory file and calls ansible (works with bastion hosts)
#!/bin/bash
export ANSIBLE_SSH_PIPELINING=1
ANSIBLE_PYTHON3_CONFIG="ansible_python_interpreter=/usr/bin/python3"
if [ $# -ne 2 ]
then
echo "Usage: $0 [user@]host[#bastion_user@bastion_host] playbook.yml"
exit 1
fi
hostinventory=$(mktemp /tmp/ansible-host-playbook.XXXXXX)
trap "rm -f $hostinventory" EXIT
ssh_user_and_host=$1
ssh_bastion=""
IFS='#' read -a bastion_parsed <<< "$ssh_user_and_host"
if [ ${#bastion_parsed[@]} -eq 2 ]; then
ssh_user_and_host=${bastion_parsed[0]}
ssh_bastion=${bastion_parsed[1]}
fi
IFS='@' read -a ssh_parsed <<< "$ssh_user_and_host"
if [ ${#ssh_parsed[@]} -eq 2 ]; then
echo -n "${ssh_parsed[1]} ansible_ssh_user=${ssh_parsed[0]} $ANSIBLE_PYTHON3_CONFIG" > $hostinventory
else
echo -n "$ssh_user_and_host $ANSIBLE_PYTHON3_CONFIG" > $hostinventory
fi
if [ "$ssh_bastion" != "" ]; then
echo ' ansible_ssh_common_args="-o StrictHostKeyChecking=no -o ProxyCommand=\"ssh -W %h:%p -q '$ssh_bastion'\""' >> $hostinventory
else
echo >> $hostinventory
fi
cmd="ansible-playbook -i $hostinventory $2"
echo Calling $cmd with following inventory:
cat $hostinventory
eval $cmd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment