Last active
July 11, 2018 14:53
-
-
Save smolin/082c9995c9964f4996e6f0643d7353ba to your computer and use it in GitHub Desktop.
Wrapper script for ansible-playbook to apply a role to a host without playbook or hosts file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Originally by Konstantin Suvorov, see for more info: | |
# https://stackoverflow.com/questions/38350674/ansible-can-i-execute-role-from-command-line | |
# | |
# With modifications by Steve Molin | |
# Number (vs lexical) comparison | |
if [[ $# -lt 2 ]]; then | |
cat <<HELP | |
Wrapper script for ansible-playbook to apply a role to a host without playbook or hosts file. | |
Usage: $0 <host-pattern> <role-name> [ansible-playbook options] | |
Examples: | |
$0 dest_host my_role | |
$0 custom_host my_role -i 'custom_host,' -vv --check | |
HELP | |
exit | |
fi | |
HOST_PATTERN=$1 | |
shift | |
ROLE=$1 | |
shift | |
# Use a temp file so that the --step flag (which requires stdin) works | |
temp_file=$(mktemp) | |
cat > $temp_file <<END | |
--- | |
- hosts: $HOST_PATTERN | |
become: true | |
roles: | |
- $ROLE | |
END | |
echo "Trying to apply role \"$ROLE\" to host/group \"$HOST_PATTERN\"..." | |
export ANSIBLE_ROLES_PATH="ansible/roles" | |
export ANSIBLE_RETRY_FILES_ENABLED="False" | |
set -x | |
ansible-playbook $temp_file -i "${HOST_PATTERN}," "$@" | |
set +x | |
/bin/rm temp_file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment