How to load a json data?

Hi

I need to parse JSON configuration files. The directory sctructure is like this:

├── configs
│ ├── dir1
│ │ └── config.json
│ └── dir2
│ └── config.json
│ …
│ └── dirN
│ └── config.json

N is not known prior to proceeding. Every config.json has the same set of variables (variables with same name, but different values) defining different virtual hosts like:
config.json:

{
listen: 8081
name: dev1
cache: no
}

How should I write Ansible playbook to process such kind of data and generate virtual host configs from them?

I tried with_fileglob loops but they dont work. Maybe because with_fileglob is for one-dir listings. Module "find" isn included in my version of Ansible (1.9.4). And there`s a bigger problem: how to parse this kind of data and set Ansible variables to use it?

Just load them as you would load yaml files, json is a subset of yaml
so vars_files and include_vars will read json files.

Thank you, Brian.

But how do I do this provided that (as I mentioned in the first post):

  1. Files to include with vars_files ot include vars are located by the path configs*\config.json, and this path should be processed dynamically (the names of directories * and the number of directories are not known prior to proceeding). How do I load them dynamically travelling through the structure configs//.json? How to go through this structure of files dynamically?

  2. Just loading each config.json does not solve the problem since in every config.json variables have the same name (as Ive mentioned it in my first post). E.g. variable 'name' from the configs/dir1/config.json will be overwritten by the variable 'name' loaded from configs/dir2/config.json . The solution could be add values from each config to some array. But I dont understand how is it possible to fill this array. Another solution could be generate vhost configs at each step after loading every config, but since Ansible does not support multiple tasks within one loop (as far as I know) I don`t understand how to do that.

So, any ideas?

1. can be solved using either a find task or a lookup (fileglob, pipe, etc)

2. you can set facts on each file using the path for namespacing

Thank you, Brian.

I cant use find because its only itroduced in version 2.0.

And the main limitation now is inability to use several actions in the same loop.

I tried something like

set_fact: sites_array = “{{ include_vars: {{item}} }}”
with_fileglob:

  • configs/*.config.json

But this kind of construction is not allowed (several actions at once: incude_vars + set_fact). And with_fileglob does`n work for files that are not in the same directory.

So the question is:
How I should build Ansible instructions to: (1) walk through the file structure configs/*.config.json, (2) load each json config, and (3) fill in array sites_array with data from each config.

Seems to be several actions for the same loop, which is not allowed. Is it possible to do it using Ansible?

I would be grateful if someone shows me the correct way to do this :slight_smile: