From a4d1256d3c5460d5bb14b2feda57e56c66154263 Mon Sep 17 00:00:00 2001 From: Alex van den Hoogen Date: Mon, 10 Feb 2025 18:11:33 +0100 Subject: [PATCH] Add check mode and fixes boolean inputs (#111) * Add check mode option * Fixes boolean inputs Boolean inputs weren't working properly before. Passing any value would result in `true`, which is unexpected and not according to inputs description. This change retrieves booleans with `getBooleanInput()` and sets a default to `false`. Relevant GitHub discussion and comment: https://github.com/actions/toolkit/issues/361#issuecomment-829507270 * Update action.yml * Update test.yml * Update test.yml --------- Co-authored-by: Dawid Dziurla --- .github/workflows/test.yml | 9 ++++++++- action.yml | 6 ++++++ main.js | 9 +++++++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 72658c8..0bed71c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -64,7 +64,7 @@ jobs: PermitRootLogin no Subsystem sftp /usr/lib/openssh/sftp-server EOF - sudo systemctl restart sshd + sudo systemctl restart ssh echo 'SSH_KNOWN_HOSTS<> $GITHUB_ENV echo $(ssh-keyscan localhost) >> $GITHUB_ENV echo 'EOF' >> $GITHUB_ENV @@ -97,6 +97,13 @@ jobs: steps: - name: Checkout code uses: actions/checkout@v4 + - name: With check mode + uses: ./ + with: + playbook: playbook.yml + directory: test + check_mode: true + options: --inventory hosts - name: With custom ansible.cfg uses: ./ with: diff --git a/action.yml b/action.yml index 0371f80..8af5a08 100644 --- a/action.yml +++ b/action.yml @@ -34,9 +34,15 @@ inputs: sudo: description: Set to "true" if root is required for running your playbook required: false + default: false no_color: description: Set to "true" if the Ansible output should not include colors (defaults to "false") required: false + default: false + check_mode: + description: Set to "true" to enable check (dry-run) mode + required: false + default: false outputs: output: description: The captured output of both stdout and stderr from the Ansible Playbook run diff --git a/main.js b/main.js index 2ad6292..0ef3b5e 100644 --- a/main.js +++ b/main.js @@ -15,8 +15,9 @@ async function main() { const vaultPassword = core.getInput("vault_password") const knownHosts = core.getInput("known_hosts") const options = core.getInput("options") - const sudo = core.getInput("sudo") - const noColor = core.getInput("no_color") + const sudo = core.getBooleanInput("sudo") + const noColor = core.getBooleanInput("no_color") + const checkMode = core.getBooleanInput("check_mode") const fileMode = 0600 let cmd = ["ansible-playbook", playbook] @@ -94,6 +95,10 @@ async function main() { process.env.ANSIBLE_FORCE_COLOR = "True" } + if (checkMode) { + cmd.push("--check") + } + let output = "" await exec.exec(cmd.join(' '), null, { listeners: {