如何在 ansible 中使用 jinja2 模板创建一个包含多个条目的 json 文件?

How to create one json file with multiple entries using jinja2 template in ansible?

我正在尝试创建一个 json 具有多个秘密(字典)和存储 class 的文件。但是模板的创建方式是它会创建多个 json 文件,每个文件都有一个秘密和一个存储空间 class。

我的模板 (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 }}"
    }
  ]
}

我正在循环调用的库文件中创建 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

在 Create_config_for_multiple_users.yml 中,它正在为每个用户创建 json 文件(因此总共创建了 5 个 json 文件)。下面给出了从模板创建 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

实际上我想要一个 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"
        }    
      ]
    }

我不知道如何以只创建一个的方式创建模板 json。

您正在包括 create_config_for_multiple_users.yaml 任务,这些任务有效地迭代了 template: 任务。如果模板任务运行多次,它会创建多个模板。在这种情况下,迭代应该在模板内。模板任务应该只有 used/invoked 一次。

请参阅下面的示例剧本。我像你一样包含了一个任务文件,但这没关系。

  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

然后在我的create_config.yml中,我使用模板模块:

- name: Create dynamic Json file with user
  template:
    dest: '/tmp/resources.json'
    src: '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 %}
  ]
}

我正在创建一个目标 JSON 文件,名称简单 /tmp/resources.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"
    },