【问题标题】:Ansible getting list of files in folders containg special fileAnsible获取包含特殊文件的文件夹中的文件列表
【发布时间】:2019-05-12 08:09:52
【问题描述】:

我正在尝试在包含“完成”文件的侧文件夹中查找文件

我试图在纯 ansible 中执行此操作,但我无法使其工作,因此我尝试使用 find、ls、xargs 执行此操作,然后在 ansible shell 命令中运行它。

文件夹和文件结构示例:

├── a12
│   ├── 1.txt
│   ├── 2.txt
│   └── 3.txt
├── a13
│   ├── 4.txt
│   ├── 5.txt
│   ├── 6.txt
│   └── done
└── a14
    ├── 7.txt
    ├── 8.txt
    ├── 9.txt
    └── done

我想得到

4.txt
5.txt
6.txt 
7.txt
8.txt
9.txt 

用命令

 find /tmp/test_an/ -type f -name 'done' | xargs dirname  | xargs ls -1 | grep -v 'done'

我得到了

/tmp/test_an/a13:
4.txt
5.txt
6.txt

/tmp/test_an/a14:
7.txt
8.txt
9.txt

我可以使用 grep 排除文件夹,但我正在寻找更清洁\更好的解决方案

【问题讨论】:

  • 似乎是使用 grep 仅包含文本文件或排除目录名称的最简单解决方案是您最好的选择。或者您可以考虑开发自己的 Ansible 模块 (docs.ansible.com/ansible/latest/dev_guide/…)。
  • 我也在寻找类似的解决方案,有什么进展吗?

标签: ansible ls xargs


【解决方案1】:

到目前为止我最好的解决方案:

 find /tmp/test_an/ -type f -name 'done' | xargs dirname  | xargs -I{} find {}  -type f 

/tmp/test_an/a13/5.txt
/tmp/test_an/a13/4.txt
/tmp/test_an/a13/done
/tmp/test_an/a13/6.txt
/tmp/test_an/a14/8.txt
/tmp/test_an/a14/9.txt
/tmp/test_an/a14/7.txt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多