action-ansible-playbook/main.js

115 lines
3.9 KiB
JavaScript
Raw Normal View History

2020-05-01 12:11:28 -06:00
const core = require('@actions/core')
const exec = require('@actions/exec')
const yaml = require('yaml')
2020-05-01 12:11:28 -06:00
const fs = require('fs')
const os = require('os')
async function main() {
try {
const playbook = core.getInput("playbook", { required: true })
2020-12-06 09:06:02 -07:00
const requirements = core.getInput("requirements")
2020-05-01 12:11:28 -06:00
const directory = core.getInput("directory")
const configuration = core.getInput("configuration")
2020-05-01 12:11:28 -06:00
const key = core.getInput("key")
const inventory = core.getInput("inventory")
const vaultPassword = core.getInput("vault_password")
const knownHosts = core.getInput("known_hosts")
2020-05-01 12:11:28 -06:00
const options = core.getInput("options")
const sudo = core.getInput("sudo")
const noColor = core.getInput("no_color")
const fileMode = 0600
2020-05-01 12:11:28 -06:00
let cmd = ["ansible-playbook", playbook]
if (options) {
2020-05-03 04:50:24 -06:00
cmd.push(options.replace(/\n/g, " "))
2020-05-01 12:11:28 -06:00
}
if (directory) {
process.chdir(directory)
2020-05-14 12:13:55 -06:00
core.saveState("directory", directory)
2020-05-01 12:11:28 -06:00
}
if (configuration) {
const ansibleConfigurationFile = "ansible.cfg"
fs.writeFileSync(ansibleConfigurationFile, configuration, { mode: fileMode })
core.saveState("ansibleConfigurationFile", ansibleConfigurationFile)
}
2020-12-06 09:06:02 -07:00
if (requirements) {
const requirementsContent = fs.readFileSync(requirements, 'utf8')
const requirementsObject = yaml.parse(requirementsContent)
if (Array.isArray(requirementsObject)) {
await exec.exec("ansible-galaxy", ["install", "-r", requirements])
} else {
if (requirementsObject.roles)
await exec.exec("ansible-galaxy", ["role", "install", "-r", requirements])
if (requirementsObject.collections)
await exec.exec("ansible-galaxy", ["collection", "install", "-r", requirements])
}
2020-12-06 09:06:02 -07:00
}
2020-05-01 12:11:28 -06:00
if (key) {
const keyFile = ".ansible_key"
fs.writeFileSync(keyFile, key + os.EOL, { mode: fileMode })
2020-05-07 03:41:33 -06:00
core.saveState("keyFile", keyFile)
2020-05-01 12:11:28 -06:00
cmd.push("--key-file")
cmd.push(keyFile)
}
if (inventory) {
const inventoryFile = ".ansible_inventory"
fs.writeFileSync(inventoryFile, inventory, { mode: fileMode })
2020-05-07 03:41:33 -06:00
core.saveState("inventoryFile", inventoryFile)
2020-05-01 12:11:28 -06:00
cmd.push("--inventory-file")
cmd.push(inventoryFile)
}
if (vaultPassword) {
const vaultPasswordFile = ".ansible_vault_password"
fs.writeFileSync(vaultPasswordFile, vaultPassword, { mode: fileMode })
2020-05-07 03:41:33 -06:00
core.saveState("vaultPasswordFile", vaultPasswordFile)
2020-05-01 12:11:28 -06:00
cmd.push("--vault-password-file")
cmd.push(vaultPasswordFile)
}
if (knownHosts) {
const knownHostsFile = ".ansible_known_hosts"
fs.writeFileSync(knownHostsFile, knownHosts, { mode: fileMode })
core.saveState("knownHostsFile", knownHostsFile)
2021-07-26 12:45:05 -06:00
cmd.push(`--ssh-common-args="-o UserKnownHostsFile=${knownHostsFile}"`)
process.env.ANSIBLE_HOST_KEY_CHECKING = "True"
} else {
process.env.ANSIBLE_HOST_KEY_CHECKING = "False"
}
if (sudo) {
2021-07-26 10:32:34 -06:00
cmd.unshift("sudo", "-E", "env", `PATH=${process.env.PATH}`)
}
if (noColor) {
process.env.ANSIBLE_NOCOLOR = "True"
} else {
process.env.ANSIBLE_FORCE_COLOR = "True"
}
2020-05-01 12:11:28 -06:00
let output = ""
await exec.exec(cmd.join(' '), null, {
listeners: {
stdout: function(data) {
output += data.toString()
},
stderr: function(data) {
output += data.toString()
}
}
})
core.setOutput("output", output)
2020-05-01 12:11:28 -06:00
} catch (error) {
core.setFailed(error.message)
}
}
main()