Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ansible:如何制作列表的选择子列表_Ansible - Fatal编程技术网

Ansible:如何制作列表的选择子列表

Ansible:如何制作列表的选择子列表,ansible,Ansible,我有一个文件列表(带完整路径)。 如何仅选择与模式匹配的文件 例如,我有: /tmp/test/a.txt /tmp/test/b.txt /tmp/test/c.log /tmp/test/d.log /tmp/test/e.txt 我只想得到以日志结尾的文件 /tmp/test/c.log /tmp/test/d.log 我认为我的清单与你的例子不符 这是我的脚本及其输出 - name: List all files debug: msg: "{{ item }}"

我有一个文件列表(带完整路径)。 如何仅选择与模式匹配的文件

例如,我有:

/tmp/test/a.txt
/tmp/test/b.txt
/tmp/test/c.log
/tmp/test/d.log
/tmp/test/e.txt
我只想得到以日志结尾的文件

/tmp/test/c.log
/tmp/test/d.log
我认为我的清单与你的例子不符

这是我的脚本及其输出

- name: List all files 
    debug:
      msg: "{{ item }}"
      verbosity: 3
    with_items: "{{ valid_logs_dir_files.stdout_lines }}"
将打印

changed: [localhost] => (item=/tmp/test/a.txt)
ok: [localhost] => (item=/tmp/test/b.txt)
ok: [localhost] => (item=/tmp/test/c.log)
ok: [localhost] => (item=/tmp/test/d.log)
ok: [localhost] => (item=/tmp/test/e.txt)

如果列表来自远程文件系统的动态列表,则可以使用find,例如

- name: Get file name based on pattern
  find:
     paths: /tmp/test
     patterns: "*.log"
  register: find_results

- name: Download files log files, just as example about using find_result
  fetch: src="{{ item['path'] }}" dest="myLocalDir"
  with_items: "{{ find_results['files'] }}"
有关详细信息,请参阅

要列出与本地计算机上的模式匹配的文件,请参见

如果列表不是从文件系统动态获取的,但已经完成,请使用
match
search


文件
模块相应地替换
调试
,以删除您的文件。

远程列表不再存在,我不想搜索整个目录并删除不需要的文件,只有列表中与模式匹配的文件才可以安全删除我已更新问题,我无法使我的列表与您的示例一起工作示例通过筛选列表来工作。您没有显示剧本的完整任务,也没有设置事实和相关选择。
- hosts: 127.0.0.1
  connection: local   
  vars:
     myFiles:
          - /tmp/test/a.txt
          - /tmp/test/b.txt
          - /tmp/test/c.log
          - /tmp/test/d.log
          - /tmp/test/e.txt

  tasks:
     - name: Filter list
       set_fact:
          new_list: "{{ myFiles | select('match','^/tmp/test/.*..log$') | list }}"

     - name: Show filtered list
       debug: msg="{{ item }}"
       with_items: "{{ new_list }}"