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 <thehedhly@users.noreply.github.com>
This commit is contained in:
H.Hedhly 2024-01-17 10:06:09 +01:00 committed by GitHub
parent 3c4a9420c1
commit 0689c791d8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 45 additions and 4 deletions

View File

@ -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:

View File

@ -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

View File

@ -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

16
main.js
View File

@ -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"

View File

@ -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")
@ -19,6 +20,9 @@ async function main() {
if (directory)
process.chdir(directory)
if (ansibleConfigurationFile)
rm(ansibleConfigurationFile)
if (keyFile)
rm(keyFile)

4
test/ansible.cfg Normal file
View File

@ -0,0 +1,4 @@
[defaults]
callbacks_enabled = ansible.posix.profile_tasks, ansible.posix.timer
stdout_callback = yaml
nocows = false