Skip to content

Conversation

@KRMV
Copy link

@KRMV KRMV commented Oct 10, 2014

I need to be able to parse ansible response as JSON, and having silent mode.

So, now, it can add a json array to result, which is a collection of object using this model:

{ "host": "XXX",
  "code": "success|FAILED",
  "data": {"var1": "value1", "var2": "value2", ...} 
}

so final result is :

{ "code": "XXX",
  "output": "XXX",
  "json": [ { "host": "XXX", "code": "XXX", "data": "XXX" }, {}, "..." ]
}

for compatibility, option must be toggled on using exec({json: true});

I added to silent mode to shelljs,
for compatibility, option must be toggled on using exec({silent: true});

I'ld test more as soon (code) as I far as I would use ansible for my projects.
I'ld have doc if accepted too ;)

Ex:

var Ansible = require('node-ansible');

var cmd = new Ansible.AdHoc().hosts('call').module('mysql_replication').user('root').args({mode: 'getslave'}).verbose(0);
var go = cmd.exec({json: true, silent: true});

go.then(function (result) {
    for(var i in result.json) {
        console.log('Host: ', result.json[i].host, ' DB is ', result.json[i].data['Slave_IO_Running'], result.json[i].data['Slave_SQL_Running'], ' with ', result.json[i].data['Seconds_Behind_Master'], 's');
    }
}

results:

Host: HOST1 DB is Yes Yes with 0 s
Host: HOST2 DB is Yes Yes with 0 s
Host: HOST3 DB is Yes Yes with 0 s

@shaharke
Copy link
Owner

@KRMV Thanks for the effort! I think I understand the motivation around this pull request, but I have a few concerns:

  1. Every code added to the project should be fully tested.
  2. I think that any execution of shelljs should be silent. It was an error on my part that it wasn't. What do you think about always calling shelljs with the silent flag?
  3. I will probably completely re-design output handling to be a) more modular and b) to support streaming. By modular I mean that decoders (such as the JSON decoder you wrote) should be plugged into an execution and not simply enabled using an option flag. Also, the fact that we need to wait for the whole execution to complete before giving access to the output is wrong. Output should be streamed. If I merge your code now, it'll be harder for me to move to streaming.

@KRMV
Copy link
Author

KRMV commented Oct 27, 2014

Hello, thanks for your reply.

About your points:
1/ I'll see to design test, when I will deeply integrate node with ansible, to make "dynamic" ansible script ;) So, as I'm quite busy, it will not be necessary soon :(
2/ I made a auto-off silent mode, to be fully compatible with your code results, but, we can easily toggle to auto-on
It just need to patch 7ef78e7 by setting silent to false.
3/ JSON converter can be use as a single module, directly inside the go.then method, so won't change your code, and could be adapted later when you redesign all.

So, I can patch 2/, setting default to false, enabling options using ({silent: false}), allowing "debug" (always useful for dumb things ;)) with ({silent: true}) and publish a pull request with just that one.
And seeing to 1/+3/ when will be able to test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants