2020-05-01 18:11:28 +00:00
|
|
|
const core = require('@actions/core')
|
|
|
|
const exec = require('@actions/exec')
|
2020-12-09 10:57:43 +00:00
|
|
|
const yaml = require('yaml')
|
2020-05-01 18:11:28 +00:00
|
|
|
const fs = require('fs')
|
|
|
|
const os = require('os')
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
try {
|
|
|
|
const playbook = core.getInput("playbook", { required: true })
|
2020-12-06 16:06:02 +00:00
|
|
|
const requirements = core.getInput("requirements")
|
2020-05-01 18:11:28 +00:00
|
|
|
const directory = core.getInput("directory")
|
|
|
|
const key = core.getInput("key")
|
|
|
|
const inventory = core.getInput("inventory")
|
|
|
|
const vaultPassword = core.getInput("vault_password")
|
2021-04-04 18:51:37 +00:00
|
|
|
const knownHosts = core.getInput("known_hosts")
|
2020-05-01 18:11:28 +00:00
|
|
|
const options = core.getInput("options")
|
2021-07-19 15:49:48 +00:00
|
|
|
const sudo = core.getInput("sudo")
|
2020-05-01 18:11:28 +00:00
|
|
|
|
2021-07-30 17:47:06 +00:00
|
|
|
let args = ["ansible-playbook", playbook]
|
2020-05-01 18:11:28 +00:00
|
|
|
|
|
|
|
if (options) {
|
2021-07-30 17:47:06 +00:00
|
|
|
args.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
|
|
|
}
|
|
|
|
|
2020-12-06 16:06:02 +00:00
|
|
|
if (requirements) {
|
2020-12-09 10:57:43 +00:00
|
|
|
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 16:06:02 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2021-07-30 17:47:06 +00:00
|
|
|
args.push("--key-file")
|
|
|
|
args.push(keyFile)
|
2020-05-01 18:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (inventory) {
|
|
|
|
const inventoryFile = ".ansible_inventory"
|
|
|
|
fs.writeFileSync(inventoryFile, inventory, { mode: 0600 })
|
2020-05-07 09:41:33 +00:00
|
|
|
core.saveState("inventoryFile", inventoryFile)
|
2021-07-30 17:47:06 +00:00
|
|
|
args.push("--inventory-file")
|
|
|
|
args.push(inventoryFile)
|
2020-05-01 18:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2021-07-30 17:47:06 +00:00
|
|
|
args.push("--vault-password-file")
|
|
|
|
args.push(vaultPasswordFile)
|
2020-05-01 18:11:28 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 18:51:37 +00:00
|
|
|
if (knownHosts) {
|
|
|
|
const knownHostsFile = ".ansible_known_hosts"
|
|
|
|
fs.writeFileSync(knownHostsFile, knownHosts, { mode: 0600 })
|
|
|
|
core.saveState("knownHostsFile", knownHostsFile)
|
2021-07-30 17:47:06 +00:00
|
|
|
args.push(`--ssh-common-args="-o UserKnownHostsFile=${knownHostsFile}"`)
|
2021-04-04 18:51:37 +00:00
|
|
|
process.env.ANSIBLE_HOST_KEY_CHECKING = "True"
|
|
|
|
} else {
|
|
|
|
process.env.ANSIBLE_HOST_KEY_CHECKING = "False"
|
|
|
|
}
|
|
|
|
|
2021-07-19 15:49:48 +00:00
|
|
|
if (sudo) {
|
2021-07-30 17:47:06 +00:00
|
|
|
args.unshift("sudo", "-E", "env", `PATH=${process.env.PATH}`)
|
2021-07-19 15:49:48 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 18:11:28 +00:00
|
|
|
process.env.ANSIBLE_FORCE_COLOR = "True"
|
|
|
|
|
2021-07-30 17:46:41 +00:00
|
|
|
const execOptions = {}
|
2021-07-30 15:42:16 +00:00
|
|
|
execOptions.listeners = {
|
2021-07-30 17:39:29 +00:00
|
|
|
stdout: function(data) {
|
2021-07-30 15:42:16 +00:00
|
|
|
core.setOutput('stdout', data.toString());
|
|
|
|
},
|
2021-07-30 17:39:29 +00:00
|
|
|
stderr: function(data) {
|
2021-07-30 15:42:16 +00:00
|
|
|
core.setOutput('stderr', data.toString());
|
|
|
|
}
|
2021-07-30 17:46:41 +00:00
|
|
|
}
|
2021-07-30 15:42:16 +00:00
|
|
|
|
2021-07-30 17:47:06 +00:00
|
|
|
await exec.exec(args.join(' '), execOptions)
|
2020-05-01 18:11:28 +00:00
|
|
|
} catch (error) {
|
|
|
|
core.setFailed(error.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main()
|