1
0
Fork 0
mirror of https://github.com/dawidd6/action-ansible-playbook.git synced 2024-11-26 02:11:30 +00:00
action-ansible-playbook/main.js

60 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-05-01 18:11:28 +00:00
const core = require('@actions/core')
const exec = require('@actions/exec')
const fs = require('fs')
const os = require('os')
async function main() {
try {
const playbook = core.getInput("playbook", { required: true })
const directory = core.getInput("directory")
const key = core.getInput("key")
const inventory = core.getInput("inventory")
const vaultPassword = core.getInput("vault_password")
const options = core.getInput("options")
let cmd = ["ansible-playbook", playbook]
if (options) {
2020-05-03 10:50:24 +00:00
cmd.push(options.replace(/\n/g, " "))
2020-05-01 18:11:28 +00:00
}
if (directory) {
process.chdir(directory)
2020-05-14 18:13:55 +00:00
core.saveState("directory", directory)
2020-05-01 18:11:28 +00:00
}
if (key) {
const keyFile = ".ansible_key"
fs.writeFileSync(keyFile, key + os.EOL, { mode: 0600 })
2020-05-07 09:41:33 +00:00
core.saveState("keyFile", keyFile)
2020-05-01 18:11:28 +00:00
cmd.push("--key-file")
cmd.push(keyFile)
}
if (inventory) {
const inventoryFile = ".ansible_inventory"
fs.writeFileSync(inventoryFile, inventory, { mode: 0600 })
2020-05-07 09:41:33 +00:00
core.saveState("inventoryFile", inventoryFile)
2020-05-01 18:11:28 +00:00
cmd.push("--inventory-file")
cmd.push(inventoryFile)
}
if (vaultPassword) {
const vaultPasswordFile = ".ansible_vault_password"
fs.writeFileSync(vaultPasswordFile, vaultPassword, { mode: 0600 })
2020-05-07 09:41:33 +00:00
core.saveState("vaultPasswordFile", vaultPasswordFile)
2020-05-01 18:11:28 +00:00
cmd.push("--vault-password-file")
cmd.push(vaultPasswordFile)
}
process.env.ANSIBLE_HOST_KEY_CHECKING = "False"
process.env.ANSIBLE_FORCE_COLOR = "True"
await exec.exec(cmd.join(" "))
} catch (error) {
core.setFailed(error.message)
}
}
main()