Reputation: 27
I want to create dynamic json file depend upon data available. Suppose I have data like below. then jinja template should create 2 list. if data available 3 time then it should create 3 list in jinja template.
"datastore_details": [
{
"name": "XYZ",
"size": 6258
},
{
"name": "XYZ-1",
"size": 32192
}
]
{
"company": "Test",
"table": "data",
"message_id": "2022222",
"p_filter": {
"key": "u_topic",
"value": "test-datastore"
},
"c_filter": {
"key": "u_topic",
"value": "test-disk_capacity"
},
"p_data": [
{
"u_label": "XYZ",
"u_catalog_item": "c08c7936db71d4503fa396f8f49619db",
"u_value": "XYZ",
"c_data": [
{
"u_label": "XYZ",
"u_catalog_item": "c08c7936db71d4503fa396f8f49619db",
"u_value": "6258"
}
]
},
{
"u_label": "XYZ-1",
"u_catalog_item": "c08c7936db71d4503fa396f8f49619db",
"u_value": "XYZ-1",
"c_data": [
{
"u_label": "XYZ-1",
"u_catalog_item": "c08c7936db71d4503fa396f8f49619db",
"u_value": "32192"
}
]
}
]
}
I am not sure how to use loop here to run 3 or 4 time depend upon the data. using below jinja template.
{% for n in range(count) %}
"u_label": "{{ datastore_details[item | int]['name'] }}",
"u_catalog_item": "c08c7936db71d4503fa396f8f49619db",
"u_value": "{{ datastore_details[item | int]['size'] }}",
"c_data": [
{
"u_label": "{{ datastore_details[item | int]['name'] }}",
"u_catalog_item": "c08c7936db71d4503fa396f8f49619db",
"u_value": "{{ datastore_details[item | int]['size'] }}"
}
]
{% endfor %}
Upvotes: 2
Views: 13353
Reputation: 153
The only clean way I found to do this was Create the json file and insert your variables where you want them as you would with any template file
"mariaDB": {
"type": "object",
"description": "Add this section to connect MeshCentral to a MariaDB database instance.",
"properties": {
"host": "{{ sm_internal_ip }}",
"user": "{{ mesh_central_db_admin_user }}",
"port": 3306,
"password": "{{ mesh_central_db_admin_password }}",
"database": "{{ mesh_central_db_name }}"
}
},
Copy the file over using the Ansible copy command
- name: copy the json file over
copy:
src: "config.json"
dest: "/SM_DATA/mesh_central/meshcentral-data/config.json"
mode: 0644
Do a string replace of your {{ variables }} using the following command
- name: Ansible multiple replace example
replace:
path: "/SM_DATA/mesh_central/meshcentral-data/config.json"
regexp: "{{ item.regexp1 }}"
replace: "{{ item.replace }}"
with_items:
- { regexp1: "{% raw %}{{ public_domain_name }}{% endraw %}", replace: "{{ public_domain_name }}"}
- { regexp1: "{% raw %}{{ sm_internal_ip }}{% endraw %}", replace: "{{ sm_internal_ip }}"}
- { regexp1: "{% raw %}{{ mesh_central_db_name }}{% endraw %}", replace: "{{ sm_mesh_central_db_nameinternal_ip }}"}
- { regexp1: "{% raw %}{{ mesh_central_db_admin_user }}{% endraw %}", replace: "{{ mesh_central_db_admin_user }}"}
- { regexp1: "{% raw %}{{ mesh_central_db_admin_password }}{% endraw %}", replace: "{{ mesh_central_db_admin_password }}"}
This gave me the end result I was looking for.
Upvotes: 0
Reputation: 4574
First of all you need to get those variables into ansible. The easiest way is to define them in the inventory. If you can not do that (e.g. because this json file is generated on the host), you can use the include_vars module to load the variables.
Assuming you have your variables accessible to ansible, you can loop over the list in a jinja2 template like this:
Example Inventory:
---
all:
hosts:
localhost:
ansible_connection: local
datastore_details:
- name: "XYZ"
size: 6258
- name: "XYZ-1"
size: 32192
- name: "as-many-as-you-want"
size: 1337
Example Playbook:
---
- hosts: all
tasks:
# if you have your variables somewhere else, use the `include_vars` module here
- name: template
template:
src: tmpl.j2
dest: /tmp/out.file
Example Template:
... some data ...
{% for d in datastore_details %}
"u_label": "{{ d['name'] }}",
"u_catalog_item": "c08c7936db71d4503fa396f8f49619db",
"u_value": "{{ d['size'] }}",
"c_data": [
{
"u_label": "{{ d['name'] }}",
"u_catalog_item": "c08c7936db71d4503fa396f8f49619db",
"u_value": "{{ d['size'] }}"
}
]
... some data in between ...
{% endfor %}
... some more data ...
Output:
... some data ...
"u_label": "XYZ",
"u_catalog_item": "c08c7936db71d4503fa396f8f49619db",
"u_value": "6258",
"c_data": [
{
"u_label": "XYZ",
"u_catalog_item": "c08c7936db71d4503fa396f8f49619db",
"u_value": "6258"
}
]
... some data in between ...
"u_label": "XYZ-1",
"u_catalog_item": "c08c7936db71d4503fa396f8f49619db",
"u_value": "32192",
"c_data": [
{
"u_label": "XYZ-1",
"u_catalog_item": "c08c7936db71d4503fa396f8f49619db",
"u_value": "32192"
}
]
... some data in between ...
"u_label": "as-many-as-you-want",
"u_catalog_item": "c08c7936db71d4503fa396f8f49619db",
"u_value": "1337",
"c_data": [
{
"u_label": "as-many-as-you-want",
"u_catalog_item": "c08c7936db71d4503fa396f8f49619db",
"u_value": "1337"
}
]
... some data in between ...
... some more data ...
The important thing here is to get your data in as variables, I do it using the inventory, but (as said before) you can use include_vars
to load variables on the host you are running on.
Upvotes: 0