我必须遍历一个模板并以系统的方式从中创建单个文件。例如。
模板样本(例如shop_names.conf.j2
)
{% for item in shop_names_csv_list %}
# Delete the id
DELETE root/{{item.shop_id}}
# Create the id with contents
PUT root/{{item.shop_id}}
{
"shop_name": "{{item.shop_name}}"
"shop_state": "{{item.shop_state}}"
}
{% endfor %}
我有以下格式的CSV,其中包含用于填充模板的数据
shop_id,shop_name,shop_state
101,Walmart,NY
102,Cosco,MT
103,Apple,DC
我能够遍历使用Ansible并将其正确填充到模板中,并输出为shop_names.conf
(单个文件)
我目前使用的逻辑是:
- read_csv:
path: shop_names.csv
register: shop_names_csv_list
- name: "Build shop name API endpoints from Template"
template:
src: "{{ item }}"
dest: "{{buildDir}}/{{ item | replace('.j2', '')}}"
with_lines:
- "find {{base_dir_template}} -type f -name '*.j2'"
所以目前我的输出为
DELETE root/101
PUT root/101
{
"shop_name": "Walmart"
"shop_state": "NY"
}
# ------- Next Line ------ #
DELETE root/102
PUT root/102
{
"shop_name": "Cosco"
"shop_state": "MT"
}
# ------- Next Line ------ #
DELETE root/103
PUT root/103
{
"shop_name": "Apple"
"shop_state": "DC"
}
# ------- Next Line ------ #
但是我一直希望将其输出为以下格式的单独文件:{{shop_id}}.{{shop_names}}.conf
,因此每个shop_id
的每个DELETE&PUT都放入一个文档中
所以我正在寻找的输出是3个文件
DELETE root/101
PUT root/101
{
"shop_name": "Walmart"
"shop_state": "NY"
}
DELETE root/102
PUT root/102
{
"shop_name": "Cosco"
"shop_state": "MT"
}
以此类推...
任何有助于将模板创建为单个文件的帮助。