我无法修复 ansible 剧本
I am unable to fix the ansible playbook
我正在尝试编写一个剧本来创建两个文件。定义嵌套变量时。我得到了输出,但这不是我所期望的。我需要创建两个仅包含变量 defines
的文件
playbook.yml
- hosts: all
vars:
variable:
- name: "item1"
vars:
- { id: 1, type: "get" , resource: "Customerid" }
- { id: 17, type: "post", resource: "Cus" }
- name: "item2"
vars:
- { id: 2, type: "get", resource: "Customerid" }
tasks:
- template:
src: template.j2
dest: "{{ item.name }}"
with_items: "{{ variable }}"
template.j2 文件内容
{% for item in variable %}
Item Name: {{ item.name }}
{% for item_var in item.vars %}
{% if item_var['type'] == 'get' %}
Item ID: {{ item_var['id'] }}
Item Resource: {{ item_var['resource'] }}
Get function
{% else %}
Item ID: {{ item_var['id'] }}
Item Resource: {{ item_var['resource'] }}
Post function
{% endif %}
{% endfor %}
{% endfor %}
item1 和 item2 创建的两个文件的当前结果:
item1
Item Name: item1
Item ID: 1
Item Resource: Customerid
Get function
Item ID: 17
Item Resource: Cus
Post function
Item Name: item2
Item ID: 2
Item Resource: Customerid
Get function
item2
Item Name: item1
Item ID: 1
Item Resource: Customerid
Get function
Item ID: 17
Item Resource: Cus
Post function
Item Name: item2
Item ID: 2
Item Resource: Customerid
Get function
我想在创建两个文件时获得以下输出。不知道如何得到这个。期望的输出:
item1
Item Name: item1
Item ID: 1
Item Resource: Customerid
Get function
Item ID: 17
Item Resource: Cus
Post function
item2
Item Name: item2
Item ID: 2
Item Resource: Customerid
Get function
只需从模板中删除重复的循环。
Item Name: {{ item.name }}
{% for item_var in item.vars %}
{% if item_var['type'] == 'get' %}
Item ID: {{ item_var['id'] }}
Item Resource: {{ item_var['resource'] }}
Get function
{% else %}
Item ID: {{ item_var['id'] }}
Item Resource: {{ item_var['resource'] }}
Post function
{% endif %}
{% endfor %}
模板可能会被简化
Item Name: {{ item.name }}
{% for item_var in item.vars %}
Item ID: {{ item_var.id }}
Item Resource: {{ item_var.resource }}
{{ item_var.type|capitalize }} function
{% endfor %}
我正在尝试编写一个剧本来创建两个文件。定义嵌套变量时。我得到了输出,但这不是我所期望的。我需要创建两个仅包含变量 defines
的文件playbook.yml
- hosts: all
vars:
variable:
- name: "item1"
vars:
- { id: 1, type: "get" , resource: "Customerid" }
- { id: 17, type: "post", resource: "Cus" }
- name: "item2"
vars:
- { id: 2, type: "get", resource: "Customerid" }
tasks:
- template:
src: template.j2
dest: "{{ item.name }}"
with_items: "{{ variable }}"
template.j2 文件内容
{% for item in variable %}
Item Name: {{ item.name }}
{% for item_var in item.vars %}
{% if item_var['type'] == 'get' %}
Item ID: {{ item_var['id'] }}
Item Resource: {{ item_var['resource'] }}
Get function
{% else %}
Item ID: {{ item_var['id'] }}
Item Resource: {{ item_var['resource'] }}
Post function
{% endif %}
{% endfor %}
{% endfor %}
item1 和 item2 创建的两个文件的当前结果:
item1
Item Name: item1
Item ID: 1
Item Resource: Customerid
Get function
Item ID: 17
Item Resource: Cus
Post function
Item Name: item2
Item ID: 2
Item Resource: Customerid
Get function
item2
Item Name: item1
Item ID: 1
Item Resource: Customerid
Get function
Item ID: 17
Item Resource: Cus
Post function
Item Name: item2
Item ID: 2
Item Resource: Customerid
Get function
我想在创建两个文件时获得以下输出。不知道如何得到这个。期望的输出:
item1
Item Name: item1
Item ID: 1
Item Resource: Customerid
Get function
Item ID: 17
Item Resource: Cus
Post function
item2
Item Name: item2
Item ID: 2
Item Resource: Customerid
Get function
只需从模板中删除重复的循环。
Item Name: {{ item.name }}
{% for item_var in item.vars %}
{% if item_var['type'] == 'get' %}
Item ID: {{ item_var['id'] }}
Item Resource: {{ item_var['resource'] }}
Get function
{% else %}
Item ID: {{ item_var['id'] }}
Item Resource: {{ item_var['resource'] }}
Post function
{% endif %}
{% endfor %}
模板可能会被简化
Item Name: {{ item.name }}
{% for item_var in item.vars %}
Item ID: {{ item_var.id }}
Item Resource: {{ item_var.resource }}
{{ item_var.type|capitalize }} function
{% endfor %}