[英]How to create one json file with multiple entries using jinja2 template in ansible?
I am trying to create a json file with multiple secret (dictionary) and storage class.我正在尝试创建一个具有多个秘密(字典)和存储类的 json 文件。 But the template is created in such a way that it will create multiple json file with single secret and a storage class in each file.但是模板是以这样一种方式创建的,它将创建多个 json 文件,每个文件中都有一个秘密和一个存储类。
my template (resources.j2) is look below我的模板(resources.j2)如下所示
"secret": [
{
"name": "secret{{ user_name }}",
"label": "{{ user_name }}",
"backend": "{{ local_array_ip }}",
"username": "{{ user_name }}",
"password": "{{password | default('test123')}}",
"namespace": "{{ default_namespace }}"
}
],
"storageclass": [
{
"name": "sc{{ user_name }}",
"secret": "{{ user_name }}",
"label": "sc-{{ user_name }}",
"limitMbps": "800",
"fstype": "xfs",
"accessProtocol": "{{ protocol }}"
}
]
}
I am creating json inside a library file which is called in loop我在循环调用的库文件中创建 json
- name: Create config files for mulitple users
include: lib/create_config_for_multiple_users.yaml
vars:
user_name: "{{ userName }}{{iterationCount}}"
with_sequence: count="5"
loop_control:
loop_var: iterationCount
In the Create_config_for_multiple_users.yml , it is creating json file for each user (so totally 5 json files are getting created).在 Create_config_for_multiple_users.yml 中,它为每个用户创建 json 文件(因此总共创建了 5 个 json 文件)。 Creation of json file from template is given below.下面给出了从模板创建 json 文件。
- set_fact:
tmp_dynamic_json_file: "/tmp/temp-json-{{User}}-{{ lookup('pipe', 'date +%Y-%m-%d-%H-%M-%s') }}.json"
- name: Create dynamic Json file with user
template:
dest: "{{tmp_dynamic_json_file}}"
src: "resources.j2"
delegate_to: localhost
Actually i wanted one single json file with multiple entries like below.实际上我想要一个带有多个条目的单个 json 文件,如下所示。
"secret": [
{
"name": "secrettest1",
"label": "test1",
"backend": "{{ local_array_ip }}",
"username": "test1",
"password": "test123",
"namespace": "kube-system"
},
{
"name": "secrettest2",
"label": "test2",
"backend": "{{ local_array_ip }}",
"username": "test2",
"password": "test123",
"namespace": "kube-system"
}
],
"storageclass": [
{
"name": "sctest1",
"secret": "test1",
"label": "sc-test1",
"limitMbps": "800",
"fstype": "xfs",
"accessProtocol": "iscsi"
},
{
"name": "sctest2",
"secret": "test2",
"label": "sc-test2",
"limitMbps": "800",
"fstype": "xfs",
"accessProtocol": "iscsi"
}
]
}
I don't know how to create template in such a way that it will create only one json.我不知道如何以只创建一个 json 的方式创建模板。
You are including create_config_for_multiple_users.yaml
tasks which effectively iterates over the template:
task.您正在包括有效迭代template:
create_config_for_multiple_users.yaml
任务template:
任务。 If the template task runs multiple times, it creates multiple templates.如果模板任务运行多次,它会创建多个模板。 The iteration in this case, should be within the template.在这种情况下,迭代应该在模板内。 The template task should be only used/invoked once.模板任务应该只使用/调用一次。
Please see the example playbook below.请参阅下面的示例剧本。 I am including a task file as you do, but it doesn't matter.我和你一样包含了一个任务文件,但这没关系。
vars:
userName: 'test'
local_array_ip: '192.168.1.1'
default_namespace: 'kube-system'
protocol: 'iscsi'
tasks:
- name: Create config files for mulitple users
include: create_config.yml
Then in my create_config.yml
, I use the template module:然后在我的create_config.yml
,我使用模板模块:
- name: Create dynamic Json file with user
template:
dest: '/tmp/resources.json'
src: 'resources.json.j2'
Then comes the iteration for user numbers in the template resources.json.j2
:然后是模板resources.json.j2
用户编号的迭代:
{
"secret": [
{% for user_num in range(1, 6) %}
{
"name": "secret{{ userName }}{{ user_num }}",
"label": "{{ userName }}{{ user_num }}",
"backend": "{{ local_array_ip }}",
"username": "{{ userName }}{{ user_num }}",
"password": "{{ password | default('test123') }}",
"namespace": "{{ default_namespace }}"
}{% if user_num < 5 %},{% endif %}
{% endfor %}
],
"storageclass": [
{% for user_num in range(1, 6) %}
{
"name": "sc{{ userName }}{{ user_num }}",
"secret": "{{ userName }}{{ user_num }}",
"label": "sc-{{ userName }}{{ user_num }}",
"limitMbps": "800",
"fstype": "xfs",
"accessProtocol": "{{ protocol }}"
}{% if user_num < 5 %},{% endif %}
{% endfor %}
]
}
I am creating a destination JSON
file with a simple name /tmp/resources.json
(abridged).我正在创建一个具有简单名称/tmp/resources.json
(删节)的目标JSON
文件。
{
"secret": [
{
"name": "secrettest1",
"label": "test1",
"backend": "192.168.1.1",
"username": "test1",
"password": "test123",
"namespace": "kube-system"
},
{
"name": "secrettest2",
"label": "test2",
"backend": "192.168.1.1",
"username": "test2",
"password": "test123",
"namespace": "kube-system"
},
"storageclass": [
{
"name": "sctest1",
"secret": "test1",
"label": "sc-test1",
"limitMbps": "800",
"fstype": "xfs",
"accessProtocol": "iscsi"
},
{
"name": "sctest2",
"secret": "test2",
"label": "sc-test2",
"limitMbps": "800",
"fstype": "xfs",
"accessProtocol": "iscsi"
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.