From 3ee033951740030237f1f0fd096e5cc02e68ff4f Mon Sep 17 00:00:00 2001 From: Ben Thomas Date: Fri, 29 Jul 2016 11:04:11 -0400 Subject: [PATCH] Execute AdHoc or Playbook command with a vault password file --- lib/ansible.js | 6 ++++++ test/adhoc.spec.js | 11 +++++++++++ test/playbook.spec.js | 12 ++++++++++++ 3 files changed, 29 insertions(+) diff --git a/lib/ansible.js b/lib/ansible.js index 4bb7e87..a2b0204 100644 --- a/lib/ansible.js +++ b/lib/ansible.js @@ -103,6 +103,11 @@ AbstractAnsibleCommand.prototype.asSudo = function() { return this; }; +AbstractAnsibleCommand.prototype.vaultPasswordFile = function(vaultPasswordFile) { + this.config.vaultPasswordFile = vaultPasswordFile; + return this; +}; + AbstractAnsibleCommand.prototype.addParam = function(commandParams, param, flag) { if (this.config[param]) { return this.addParamValue(commandParams, this.config[param], flag) @@ -143,6 +148,7 @@ AbstractAnsibleCommand.prototype.commonCompileParams = function(commandParams) { commandParams = this.addParam(commandParams, "limit", "l"); commandParams = this.addParam(commandParams, "su", "U"); commandParams = this.addPathParam(commandParams, "privateKey", "-private-key"); + commandParams = this.addParam(commandParams, "vaultPasswordFile", "-vault-password-file"); commandParams = this.addVerbose(commandParams); commandParams = this.addSudo(commandParams); diff --git a/test/adhoc.spec.js b/test/adhoc.spec.js index 56d4fc1..76279fb 100644 --- a/test/adhoc.spec.js +++ b/test/adhoc.spec.js @@ -157,6 +157,17 @@ describe('AdHoc command', function() { }) }) + describe('with vault password file', function() { + + it('should contain vault password file in execution', function(done) { + var command = new AdHoc().module('shell').hosts('local').args("echo 'hello'").vaultPasswordFile("/vault.txt"); + expect(command.exec()).to.be.fulfilled.then(function() { + expect(spawnSpy).to.be.calledWith('ansible', ['local', '-m', 'shell', '-a', 'echo \'hello\'', '--vault-password-file', '/vault.txt']); + done(); + }).done(); + }) + }) + after(function() { process.spawn = oldSpawn; spawnSpy.restore(); diff --git a/test/playbook.spec.js b/test/playbook.spec.js index a73f039..18913af 100644 --- a/test/playbook.spec.js +++ b/test/playbook.spec.js @@ -164,6 +164,18 @@ describe('Playbook command', function () { }) + describe('with vault password file', function() { + + it('should execute the playbook with specified vault password file', function (done) { + var command = new Playbook().playbook('test').vaultPasswordFile("/vault.txt"); + expect(command.exec()).to.be.fulfilled.then(function () { + expect(spawnSpy).to.be.calledWith('ansible-playbook' ,['test.yml', '--vault-password-file', '/vault.txt']); + done(); + }).done(); + }) + + }) + describe('with working directory', function () { var path = require('path');