4

I'm using the following task in my Ansible script to copy all files from a local data folder to the server:

- name: copy basic files to folder
  copy:
    src: "{{ item }}"
    dest: ~/data/
    mode: 755
    owner: "www-data"
    group: "www-data"
  with_fileglob:
    - ../files/data/*

This works fine, except for that it skips hidden files (such as .htaccess).

Does anybody know how I can make with_fileglob also include hidden files?

4

2 回答 2

12

好的,我自己找到了答案。我发现它with_fileglob只是调用了 python glob.glob()函数。因此,经过一番折腾后,我发现只需要添加一个 fileglob.*即可:

- name: copy basic files to folder
  copy:
    src: "{{ item }}"
    dest: ~/data/
    mode: 755
    owner: "www-data"
    group: "www-data"
  with_fileglob:
    - ../files/data/*
    - ../files/data/.*
于 2017-09-05T13:15:33.127 回答
9

Ansible使用Python 的glob

如果目录包含以它们开头的文件,.则默认情况下不会匹配。

>>> import glob
>>> glob.glob('*.gif')
['card.gif']
>>> glob.glob('.c*')
['.card.gif']

显式添加.*到模式列表中。

于 2017-09-05T13:15:15.603 回答