replace - How to create list of files containing only filename in one set_fact task
问题描述
In Ansible 2.9 I am trying to create a list of files using the lookup plugin with the fileglob parameter that contains only the names of the files (not the path) and can be done in a single set_fact. My problem seems to be that I cannot figure out how to properly use regex when calling the replace method. I'm probably confusing some of the terminology there.
This is what I have so far, and once this works was going to send to split to build list.
- name: compile list of template files
set_fact:
list_of_files: "{{ lookup('fileglob', 'mydir/*').replace('/', '') }}"
But I cannot get any wildcard or regex I try to work. I have tried dozens of different options, that probably aren't worth repeating here.
解决方案
This is a non-regex solution using basename
filer, this would use ansible's in-built logic to filter out the file name from the full path.
- name: compile list of template files
set_fact:
list_of_files: "{{ lookup('fileglob', 'mydir/*').split(',')|map('basename') |list}}"
map
function feeds on an iterable(in this case output of lookup and split) and applies the function inside map
(in this case basename
) to all the elements of the iterable.
推荐阅读
- javascript - 使用 forEach 处理函数
- javascript - 通过循环添加事件侦听器与添加到文档并检查事件目标是否匹配元素
- javascript - 仅用第二个字符替换两个字符的任意组合
- python - 代码可以在 Anaconda 上运行,但不能通过 cmd 在 Python 上运行
- django - 为什么删除 django 服务器缓存中的会话记录不会让我注销?
- r - 描述 R 中的入射密度
- python - 如何使用 skimage.segmentation.felzenszwalb 围绕轮廓获得框?
- json - 将 JSON 数组值分配给变量时出错
- java - 无法获取我的 Java 应用程序的致命错误日志
- c# - 当窗口失去焦点时,UWP AcrylicBrush HostBackdrop 导致颜色变化