From 6d1f10719846a6dcdc21ecf110a17a73189231d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20Peliz=C3=A4us?= Date: Sun, 22 Mar 2020 20:30:44 +0100 Subject: [PATCH] Add capabilities for custom inventory and vault password --- README.md | 8 +++++++- action.yml | 6 ++++++ main.sh | 19 ++++++++++++++++++- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5ea5866..527d992 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,14 @@ An action that executes given Ansible playbook on selected hosts. with: playbook: deploy.yml key: ${{secrets.SSH_PRIVATE_KEY}} + inventory: | + [all] + example.com + + [group1] + example.com + vault_password: ${{secrets.VAULT_PASSWORD}} options: | - --inventory hosts --limit dev --extra-vars hello=there --verbose diff --git a/action.yml b/action.yml index 0d1626e..ae273a7 100644 --- a/action.yml +++ b/action.yml @@ -10,6 +10,12 @@ inputs: key: description: SSH private key used to connect to the host required: true + inventory: + description: Custom content to write into hosts + required: false + vault_password: + description: The password used for decrypting vaulted files + required: false options: description: Extra options that should be passed to ansible-playbook command required: false diff --git a/main.sh b/main.sh index c16dbc5..10b51c8 100755 --- a/main.sh +++ b/main.sh @@ -2,8 +2,13 @@ set -e +default_inventory="hosts" +default_vault_file=".vault_password" + playbook="$INPUT_PLAYBOOK" key="$INPUT_KEY" +inventory="$INPUT_INVENTORY" +vault_password="$INPUT_VAULT_PASSWORD" options="$INPUT_OPTIONS" if test -z "$playbook"; then @@ -20,10 +25,22 @@ 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" export ANSIBLE_HOST_KEY_CHECKING=False export ANSIBLE_FORCE_COLOR=True -ansible-playbook $options $playbook \ No newline at end of file +ansible-playbook $options $playbook