Ansible:如何在CSV文件中查找并填充相关文件内容

Ansible:如何在CSV文件中查找并填充相关文件内容,ansible,Ansible,所以我要找的逻辑是 从模板目录获取文件名列表 整理文件名以匹配CSV文件中employeeID的格式 检查查找文件(csv文件)中的employeeID 如果找到,获取其他参考信息并填充employeeID.conf的模板 我在下面的查找文件中找到了类似的数据 EmployeeID,EmployeeName,EmployeeCountry E123,John,USA E345,George,UK ... 模板集文件名 E123.conf.j2 E345.conf.j2 ... 每个模板包

所以我要找的逻辑是

  • 从模板目录获取文件名列表
  • 整理文件名以匹配CSV文件中employeeID的格式
  • 检查查找文件(csv文件)中的employeeID
  • 如果找到,获取其他参考信息并填充employeeID.conf的模板
我在下面的查找文件中找到了类似的数据

EmployeeID,EmployeeName,EmployeeCountry
E123,John,USA
E345,George,UK
...
模板集文件名

E123.conf.j2
E345.conf.j2
...
每个模板包含(例如E123.conf)

我能够在模板中填充逻辑,但与文件名比较不起作用

到目前为止,我所做的编码是(但是在注册值之后,我有点卡住了)。下面是我到现在为止所做的

- name: "List templates and get filenames from a huge list of templates"
  find:
      paths: "{{base_dir_template}}"
      patterns: "*.j2"
      file_type: file
  register: emp_usecase_templates

- name: "Derive EmpID from filenames so as to compare it with lookup"
  set_fact: emp_usecase_derived_list="{{item.path | basename | replace('.conf.j2', '')}}"
  with_items: "{{emp_usecase_templates.files}}"
  register: emp_usecase_derived_list_result

- name: "Set Employee variables into template. But not working."
  set_fact:
    EmployeeName: "{{ lookup_file | selectattr('EmployeeID','match',item) | map(attribute='EmployeeName') | list }}"
  with_items: "{{emp_usecase_derived_list_result}}"
下面的任务

-读取\u csv:
路径:employees.csv
关键字:EmployeeID
登记:雇员
-名称:列出模板并从大量模板列表中获取文件名
查找:
路径:“{base\u dir\u template}”
模式:“*.j2”
文件类型:文件
注册:emp_用例_模板
-名称:将员工变量设置到模板中
模板:
src:“{{item}}”
目标:{{my_filename}}”
循环:{{emp_usecase_templates.files | map(attribute='path')| list}
变量:
my_模板:“{item | basename}”
my_文件名:“{(my_模板| splitext.0}”
EmployeeID:“{{my_template.split('.').0}”
EmployeeName:“{{employees.dict[EmployeeID]['EmployeeName']}”
EmployeeCountry:“{{employees.dict[EmployeeID]['EmployeeCountry']}”
创建了这些文件

shell>cat E123.conf
{
“id”:E123,
“姓名”:约翰,
“国家”:美国,
“somethingUnique”:“hardcodedValueForEmployee”
}
外壳>cat E345.conf
{
“id”:E345,
“姓名”:乔治,
“国家”:英国,
“somethingUnique”:“hardcodedValueForEmployee”
}

非常感谢
- name: "List templates and get filenames from a huge list of templates"
  find:
      paths: "{{base_dir_template}}"
      patterns: "*.j2"
      file_type: file
  register: emp_usecase_templates

- name: "Derive EmpID from filenames so as to compare it with lookup"
  set_fact: emp_usecase_derived_list="{{item.path | basename | replace('.conf.j2', '')}}"
  with_items: "{{emp_usecase_templates.files}}"
  register: emp_usecase_derived_list_result

- name: "Set Employee variables into template. But not working."
  set_fact:
    EmployeeName: "{{ lookup_file | selectattr('EmployeeID','match',item) | map(attribute='EmployeeName') | list }}"
  with_items: "{{emp_usecase_derived_list_result}}"