A library that helps developing Ansible module in javascript.
Create a javascript file with the node shebang :
#! node
Import the ansible-node-module :
const ansible = require('ansible-node-module');
Call ansible.main passing a callback containing your logic :
ansible.main(() => {
// TO DO
});
Use your module in playbooks. For example if your script is called your-module.js, put it in a library folder located near your playbook.
---
- hosts: localhost
connection: local
tasks:
- your-module:
WARNING : The ansible_node_module package must be located in /.ansible/node_modules.
Eventually run your playbook :
ansible-playbook your-playbook.yml
Your callback just have to return a JSON object. ansible-node-module will print it in the standard output as the ansible process expects. If your callback returns nothing, an empty object is returned.
ansible.main(() => {
return {content: 'any content'};
});
If your callback throws an exception, ansible-node-module will print an object JSON containing the flag failed set to true and a msg field containing the message of your error.
ansible.main(() => {
throw new Error('your error');
});
Your callback can access the arguments set in the playbook, by declaring one parameter in its signature. Then each argument is accessible as a field of this parameter. For example, in the playbook :
---
- hosts: localhost
connection: local
tasks:
- your-module: arg1=value arg2=value2
then arg1 and arg2 will be accessible this way :
ansible.main((args) => {
args.arg1 ...
args.arg2 ...
});
To receive dictionary arguments or complex objects, place the following at the top of your module and Ansible will pass along arguments as JSON:
// WANT_JSON
const ansible = require('ansible-node-module');
ansible.main(() => {
// TO DO
});
If your script uses other npm package they must be installed in /.ansible/node_modules. This is also true for ansible-node-module.