1
0
Fork 0
mirror of https://github.com/dawidd6/action-ansible-playbook.git synced 2024-11-21 23:19:24 +00:00

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}} key: ${{env.SSH_PRIVATE_KEY}}
known_hosts: ${{env.SSH_KNOWN_HOSTS}} known_hosts: ${{env.SSH_KNOWN_HOSTS}}
directory: test directory: test
configuration: |
[defaults]
callbacks_enabled = ansible.posix.profile_tasks, ansible.posix.timer
stdout_callback = yaml
nocows = false
vault_password: test vault_password: test
requirements: requirements.yml requirements: requirements.yml
inventory: | inventory: |
@ -92,6 +97,17 @@ jobs:
steps: steps:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v4 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 - name: With requirements
uses: ./ uses: ./
with: with:

View file

@ -14,6 +14,12 @@ Should work on any OS, if `ansible-playbook` command is available in `PATH`.
playbook: deploy.yml playbook: deploy.yml
# Optional, directory where playbooks live # Optional, directory where playbooks live
directory: ./ 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 # Optional, SSH private key
key: ${{secrets.SSH_PRIVATE_KEY}} key: ${{secrets.SSH_PRIVATE_KEY}}
# Optional, literal inventory file contents # Optional, literal inventory file contents

View file

@ -13,6 +13,9 @@ inputs:
directory: directory:
description: Root directory of Ansible project (defaults to current) description: Root directory of Ansible project (defaults to current)
required: false required: false
configuration:
description: Ansible configuration file content (ansible.cfg)
required: false
key: key:
description: SSH private key used to connect to the host description: SSH private key used to connect to the host
required: false required: false

16
main.js
View file

@ -9,6 +9,7 @@ async function main() {
const playbook = core.getInput("playbook", { required: true }) const playbook = core.getInput("playbook", { required: true })
const requirements = core.getInput("requirements") const requirements = core.getInput("requirements")
const directory = core.getInput("directory") const directory = core.getInput("directory")
const configuration = core.getInput("configuration")
const key = core.getInput("key") const key = core.getInput("key")
const inventory = core.getInput("inventory") const inventory = core.getInput("inventory")
const vaultPassword = core.getInput("vault_password") const vaultPassword = core.getInput("vault_password")
@ -16,6 +17,7 @@ async function main() {
const options = core.getInput("options") const options = core.getInput("options")
const sudo = core.getInput("sudo") const sudo = core.getInput("sudo")
const noColor = core.getInput("no_color") const noColor = core.getInput("no_color")
const fileMode = 0600
let cmd = ["ansible-playbook", playbook] let cmd = ["ansible-playbook", playbook]
@ -28,6 +30,12 @@ async function main() {
core.saveState("directory", directory) core.saveState("directory", directory)
} }
if (configuration) {
const ansibleConfigurationFile = "ansible.cfg"
fs.writeFileSync(ansibleConfigurationFile, configuration, { mode: fileMode })
core.saveState("ansibleConfigurationFile", ansibleConfigurationFile)
}
if (requirements) { if (requirements) {
const requirementsContent = fs.readFileSync(requirements, 'utf8') const requirementsContent = fs.readFileSync(requirements, 'utf8')
const requirementsObject = yaml.parse(requirementsContent) const requirementsObject = yaml.parse(requirementsContent)
@ -44,7 +52,7 @@ async function main() {
if (key) { if (key) {
const keyFile = ".ansible_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) core.saveState("keyFile", keyFile)
cmd.push("--key-file") cmd.push("--key-file")
cmd.push(keyFile) cmd.push(keyFile)
@ -52,7 +60,7 @@ async function main() {
if (inventory) { if (inventory) {
const inventoryFile = ".ansible_inventory" const inventoryFile = ".ansible_inventory"
fs.writeFileSync(inventoryFile, inventory, { mode: 0600 }) fs.writeFileSync(inventoryFile, inventory, { mode: fileMode })
core.saveState("inventoryFile", inventoryFile) core.saveState("inventoryFile", inventoryFile)
cmd.push("--inventory-file") cmd.push("--inventory-file")
cmd.push(inventoryFile) cmd.push(inventoryFile)
@ -60,7 +68,7 @@ async function main() {
if (vaultPassword) { if (vaultPassword) {
const vaultPasswordFile = ".ansible_vault_password" const vaultPasswordFile = ".ansible_vault_password"
fs.writeFileSync(vaultPasswordFile, vaultPassword, { mode: 0600 }) fs.writeFileSync(vaultPasswordFile, vaultPassword, { mode: fileMode })
core.saveState("vaultPasswordFile", vaultPasswordFile) core.saveState("vaultPasswordFile", vaultPasswordFile)
cmd.push("--vault-password-file") cmd.push("--vault-password-file")
cmd.push(vaultPasswordFile) cmd.push(vaultPasswordFile)
@ -68,7 +76,7 @@ async function main() {
if (knownHosts) { if (knownHosts) {
const knownHostsFile = ".ansible_known_hosts" const knownHostsFile = ".ansible_known_hosts"
fs.writeFileSync(knownHostsFile, knownHosts, { mode: 0600 }) fs.writeFileSync(knownHostsFile, knownHosts, { mode: fileMode })
core.saveState("knownHostsFile", knownHostsFile) core.saveState("knownHostsFile", knownHostsFile)
cmd.push(`--ssh-common-args="-o UserKnownHostsFile=${knownHostsFile}"`) cmd.push(`--ssh-common-args="-o UserKnownHostsFile=${knownHostsFile}"`)
process.env.ANSIBLE_HOST_KEY_CHECKING = "True" process.env.ANSIBLE_HOST_KEY_CHECKING = "True"

View file

@ -11,6 +11,7 @@ function rm(file) {
async function main() { async function main() {
try { try {
const directory = core.getState("directory") const directory = core.getState("directory")
const ansibleConfigurationFile = core.getState("ansibleConfigurationFile")
const keyFile = core.getState("keyFile") const keyFile = core.getState("keyFile")
const inventoryFile = core.getState("inventoryFile") const inventoryFile = core.getState("inventoryFile")
const vaultPasswordFile = core.getState("vaultPasswordFile") const vaultPasswordFile = core.getState("vaultPasswordFile")
@ -18,6 +19,9 @@ async function main() {
if (directory) if (directory)
process.chdir(directory) process.chdir(directory)
if (ansibleConfigurationFile)
rm(ansibleConfigurationFile)
if (keyFile) if (keyFile)
rm(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