ansibleapi v1.2.0
Ansible API
============ This is a programmatic interface in NodeJS for executing Ansible ad-hoc commands and playbooks
Installation
npm
Install the module via npm specifying a version.
$ npm install ansibleapi --save
NOTE: I think it goes without saying, but I'll mention it anyway - you MUST have ansible installed on the same machine on which your node process is going to run.
Running AdHoc Commands
The Ansible object contains an AdHoc method that mimics the cli commands.
var Ansible = require('ansibleapi');
var command = Ansible.AdHoc().module('shell').hosts('local').args("echo 'hello'");
command.exec();
This is equivalent to the following shell command:
ansible local -m shell -a "echo 'hello'"
The exec method returns a promise that can provide status codes and output asynchronously
var promise = command.exec();
promise.then(function (result)
{
console.log(result.code); // Exit code of the executed command
console.log(result.output) // Standard output/error of the executed command
}, function (error)
{
console.error(error);
});
Running Playbooks
The Ansible object contains a Playbook method that mimics the cli commands.
var Ansible = require('ansibleapi');
var playbook = Ansible.Playbook().playbook('myplaybook');
playbook.exec();
This is equivalent to the following shell command:
ansible-playbook myplaybook.yml
The exec method returns a promise that can provide status codes and output asynchronously
var promise = playbook.exec();
promise.then(function (result)
{
console.log(result.code); // Exit code of the executed command
console.log(result.output) // Standard output/error of the executed command
}, function (error)
{
console.error(error);
});
Realtime Streaming
We can also get the results of a command streamed in real time (from both playbooks and adhoc commands):
playbook.on('stdout', function(data) { console.log(data.toString()); });
playbook.on('stderr', function(data) { console.log(data.toString()); });
var promise = playbook.exec();
8 years ago