From 0689c791d8f1bcab336105f5565e037dba41a371 Mon Sep 17 00:00:00 2001 From: "H.Hedhly" <9963093+thehedhly@users.noreply.github.com> Date: Wed, 17 Jan 2024 10:06:09 +0100 Subject: [PATCH] Introduce optional support for ansible.cfg (#88) * Introduce optional support for ansible.cfg * Add missing ansible.cfg local & remote tests * Update Readme * Fix broken local test --------- Co-authored-by: thehedhly --- .github/workflows/test.yml | 16 ++++++++++++++++ README.md | 6 ++++++ action.yml | 3 +++ main.js | 16 ++++++++++++---- post.js | 4 ++++ test/ansible.cfg | 4 ++++ 6 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 test/ansible.cfg diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 84d025e..72658c8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -75,6 +75,11 @@ jobs: key: ${{env.SSH_PRIVATE_KEY}} known_hosts: ${{env.SSH_KNOWN_HOSTS}} directory: test + configuration: | + [defaults] + callbacks_enabled = ansible.posix.profile_tasks, ansible.posix.timer + stdout_callback = yaml + nocows = false vault_password: test requirements: requirements.yml inventory: | @@ -92,6 +97,17 @@ jobs: steps: - name: Checkout code uses: actions/checkout@v4 + - name: With custom ansible.cfg + uses: ./ + with: + playbook: playbook.yml + directory: test + configuration: | + [defaults] + callbacks_enabled = ansible.posix.profile_tasks, ansible.posix.timer + stdout_callback = yaml + nocows = false + options: --inventory hosts - name: With requirements uses: ./ with: diff --git a/README.md b/README.md index cdb2170..da43121 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,12 @@ Should work on any OS, if `ansible-playbook` command is available in `PATH`. playbook: deploy.yml # Optional, directory where playbooks live directory: ./ + # Optional, ansible configuration file content (ansible.cfg) + configuration: | + [defaults] + callbacks_enabled = ansible.posix.profile_tasks, ansible.posix.timer + stdout_callback = yaml + nocows = false # Optional, SSH private key key: ${{secrets.SSH_PRIVATE_KEY}} # Optional, literal inventory file contents diff --git a/action.yml b/action.yml index bf1e67a..98be187 100644 --- a/action.yml +++ b/action.yml @@ -13,6 +13,9 @@ inputs: directory: description: Root directory of Ansible project (defaults to current) required: false + configuration: + description: Ansible configuration file content (ansible.cfg) + required: false key: description: SSH private key used to connect to the host required: false diff --git a/main.js b/main.js index 147f323..2ad6292 100644 --- a/main.js +++ b/main.js @@ -9,6 +9,7 @@ async function main() { const playbook = core.getInput("playbook", { required: true }) const requirements = core.getInput("requirements") const directory = core.getInput("directory") + const configuration = core.getInput("configuration") const key = core.getInput("key") const inventory = core.getInput("inventory") const vaultPassword = core.getInput("vault_password") @@ -16,6 +17,7 @@ async function main() { const options = core.getInput("options") const sudo = core.getInput("sudo") const noColor = core.getInput("no_color") + const fileMode = 0600 let cmd = ["ansible-playbook", playbook] @@ -28,6 +30,12 @@ async function main() { core.saveState("directory", directory) } + if (configuration) { + const ansibleConfigurationFile = "ansible.cfg" + fs.writeFileSync(ansibleConfigurationFile, configuration, { mode: fileMode }) + core.saveState("ansibleConfigurationFile", ansibleConfigurationFile) + } + if (requirements) { const requirementsContent = fs.readFileSync(requirements, 'utf8') const requirementsObject = yaml.parse(requirementsContent) @@ -44,7 +52,7 @@ async function main() { if (key) { const keyFile = ".ansible_key" - fs.writeFileSync(keyFile, key + os.EOL, { mode: 0600 }) + fs.writeFileSync(keyFile, key + os.EOL, { mode: fileMode }) core.saveState("keyFile", keyFile) cmd.push("--key-file") cmd.push(keyFile) @@ -52,7 +60,7 @@ async function main() { if (inventory) { const inventoryFile = ".ansible_inventory" - fs.writeFileSync(inventoryFile, inventory, { mode: 0600 }) + fs.writeFileSync(inventoryFile, inventory, { mode: fileMode }) core.saveState("inventoryFile", inventoryFile) cmd.push("--inventory-file") cmd.push(inventoryFile) @@ -60,7 +68,7 @@ async function main() { if (vaultPassword) { const vaultPasswordFile = ".ansible_vault_password" - fs.writeFileSync(vaultPasswordFile, vaultPassword, { mode: 0600 }) + fs.writeFileSync(vaultPasswordFile, vaultPassword, { mode: fileMode }) core.saveState("vaultPasswordFile", vaultPasswordFile) cmd.push("--vault-password-file") cmd.push(vaultPasswordFile) @@ -68,7 +76,7 @@ async function main() { if (knownHosts) { const knownHostsFile = ".ansible_known_hosts" - fs.writeFileSync(knownHostsFile, knownHosts, { mode: 0600 }) + fs.writeFileSync(knownHostsFile, knownHosts, { mode: fileMode }) core.saveState("knownHostsFile", knownHostsFile) cmd.push(`--ssh-common-args="-o UserKnownHostsFile=${knownHostsFile}"`) process.env.ANSIBLE_HOST_KEY_CHECKING = "True" diff --git a/post.js b/post.js index ac6bbe0..430a26e 100644 --- a/post.js +++ b/post.js @@ -11,6 +11,7 @@ function rm(file) { async function main() { try { const directory = core.getState("directory") + const ansibleConfigurationFile = core.getState("ansibleConfigurationFile") const keyFile = core.getState("keyFile") const inventoryFile = core.getState("inventoryFile") const vaultPasswordFile = core.getState("vaultPasswordFile") @@ -18,6 +19,9 @@ async function main() { if (directory) process.chdir(directory) + + if (ansibleConfigurationFile) + rm(ansibleConfigurationFile) if (keyFile) rm(keyFile) diff --git a/test/ansible.cfg b/test/ansible.cfg new file mode 100644 index 0000000..2535c29 --- /dev/null +++ b/test/ansible.cfg @@ -0,0 +1,4 @@ +[defaults] +callbacks_enabled = ansible.posix.profile_tasks, ansible.posix.timer +stdout_callback = yaml +nocows = false