如何使用ansible获取模式匹配文件夹列表

时间:2018-03-27 09:28:05

标签: find ansible pattern-matching

ansible find模块未列出匹配模式。 这是我的目录结构

path: /home/ryan/ws

.
|___file1.txt
|___file2.yaml
|___file3.jar
|___service1
| |___1.0.7.0
|   |___service1_1.0.7.0.jar
| |___1.0.19.0
|   |___service1_1.0.19.0.jar
| |___1.0.123.0
|   |___service1_1.0.123.0.jar
|____service2
| |___1.0.23.0
|   |___service2_1.0.23.0.jar
| |___1.0.9.0
|   |___service2_1.0.9.0.jar
| |___1.0.143.0
|   |___service2_1.0.143.0.jar
|____service3
| |___1.0.2.0
|   |___service3_1.0.2.0.jar
| |___1.0.4.0
|   |___service3_1.0.4.0.jar
| |___1.0.13.0
|   |___service3_1.0.13.0.jar

同样聪明的我每个服务有近20个文件夹。所以我想列出每个服务中的所有目录。

这是我正在尝试的

- hosts: myserver
  become: true
  become_user: myuser
  vars:
    workspace_path: /home/ryan/ws

tasks:

- name: find folders from location
  find:
    paths: "{{ workspace_path }}/{{ item }}"
    file_type: directory
    patterns: "1.0.*.0/"
    recurse: yes
    use_regex: yes
  with_items:
    - service1                         
    - service2               
    - service3         
    - app1
    - app2 
  register: files_to_delete

- debug: var=files_to_delete

有人能给我更好的方法在ansible中单独列出文件夹吗?

1 个答案:

答案 0 :(得分:0)

请勿在样式中包含路径分隔符。它应该显示为

patterns: "1.0.*.0"