Getting data from json format

Hi all,

I have just discovered ansible and I must admit in certain tasks it this way easier to use ansible than puppet :slight_smile:
It is really stunning how easy are some tasks - thanks guys :slight_smile:

I am trying to get some data from nodes. I have just run

ansible -m setup node100

but I get output in json (I guess):

node100 | success >> {
ā€œansible_factsā€: {
ā€œansible_all_ipv4_addressesā€: [
ā€œx.x.x.xā€
],
ā€œansible_architectureā€: ā€œx86_64ā€,
ā€¦

ā€¦

Is there any easy way to read data from particular fields, i.e.:
var1 = ansible_facts ā†’ ansible_all_ipv4_addresses
Python-native way:
(var1, var2, var3) = (ansible_facts ā†’ ansible_all_ipv4_addresses, ansible_facts ā†’ ā€¦, ansible_facts ā†’ ā€¦)
would be ideal :wink:

Thanks
Przemek

If you want to use this in a playbook or template, this is pretty straightforward.

Jinja2 evaluation is basically Python. The only thing is you canā€™t do list comprehensions.

First element:

{{ ansible_all_ipv4_addresses[0] }}

At first I wanted to use it in the CLI (listing some facts about nodes) so I guess I need to download some external json parser.
Am I right ?

Regarding using it in a playbook/template: can you provide simple but full example how to use it ?

ā€œRegarding using it in a playbook/template: can you provide simple but full example how to use it ?ā€

http://ansibleworks.com/docs

Does anybody know how to parse the output from the following command ?
ansible groupofhosts -m setup -a ā€˜filter=some_filterā€™
?

Why do you want to parse the output of this command?

To gather config data from nodes for further analysis.

Ok, so the API is great for that.

You also have --tree mode to dump things into individual JSON files per host.

Where I could read about it ?
Any example program ?

ansible with the --help flag, then just try the option.

It will output JSON files into the specified directory with one file per hostname.

Oh I see Michael - I am testing the ā€œtreeā€ option now :slight_smile:
I was asking about API: docs + examples.

Yeah /usr/bin/ansible is a pretty self explanatory example of the Runner API.

I would look at that closely.