将列表中每个路径的基本名称保存到新列表

问题描述 投票:0回答:1

我有一个路径列表,我想将其转换为新变量中的文件名列表,即来自:

paths:
- a/b/c/d.txt
- e/f/g/h.txt

对此:

filenames:
- d.txt
- h.txt

如何将此调试打印变成带有新变量的

set_fact
指令?

- name: "Show base name, but how to save the list?"
  debug: msg={{ item | basename }}
  loop: "{{ paths }}"

我可以使用

map(regex_replace)
,但我正在寻找一种不那么老套的方法来实现这一点。谢谢!

ansible
1个回答
4
投票

映射过滤器基本名称。例如,

    - set_fact:
        files: "{{ paths | map('basename') | list }}"
    - debug:
        var: files

给予

  files:
  - d.txt
  - h.txt
© www.soinside.com 2019 - 2024. All rights reserved.