How to make Ansible with_fileglob include hidden files?

4.1k views Asked by At

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?

2

There are 2 answers

1
techraf On BEST ANSWER

Ansible uses Python's glob.

If the directory contains files starting with . they won’t be matched by default.

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

Add .* explicitly to the list of patterns.

2
kramer65 On

Ok, found the answer myself. I found that with_fileglob simply calls the python glob.glob() function. So after some fideling around I found just had to add a fileglob with .* to it:

- 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/.*