action-ansible-playbook/main.sh

52 lines
1.1 KiB
Bash
Raw Normal View History

2020-03-21 13:01:42 -06:00
#!/bin/sh
2020-03-21 10:06:40 -06:00
set -e
default_inventory="hosts"
default_vault_file=".vault_password"
2020-03-21 10:06:40 -06:00
playbook="$INPUT_PLAYBOOK"
2020-03-24 15:23:41 -06:00
directory="$INPUT_DIRECTORY"
2020-03-21 10:06:40 -06:00
key="$INPUT_KEY"
inventory="$INPUT_INVENTORY"
vault_password="$INPUT_VAULT_PASSWORD"
2020-03-21 10:06:40 -06:00
options="$INPUT_OPTIONS"
if test -z "$playbook"; then
echo "You need to specify 'playbook' input (Ansible playbook filepath)"
exit 1
fi
2020-03-21 13:54:44 -06:00
if test -z "$key"; then
echo "You need to specify 'key' input (SSH private key)"
exit 1
2020-03-21 10:06:40 -06:00
fi
if test -n "$directory"; then
cd "$directory"
fi
2020-03-24 15:23:41 -06:00
2020-03-21 13:54:44 -06:00
mkdir -p "$HOME/.ssh"
echo "$key" > "$HOME/.ssh/id_rsa"
chmod 600 "$HOME/.ssh/id_rsa"
if [ "$inventory" ]; then
echo "Writing inventory with custom content:"
echo -e "$inventory" | tee "$default_inventory"
options="${options} --inventory ${default_inventory}"
fi
if [ "$vault_password" ]; then
echo "Setting vault password"
echo "$vault_password" > "$default_vault_file"
options="${options} --vault-password-file ${default_vault_file}"
fi
echo "$options"
echo "$playbook"
2020-03-21 10:06:40 -06:00
export ANSIBLE_HOST_KEY_CHECKING=False
export ANSIBLE_FORCE_COLOR=True
ansible-playbook $options $playbook