ansible: Is there something like with_fileglobs for files on remote machine? ansible: Is there something like with_fileglobs for files on remote machine? bash bash

ansible: Is there something like with_fileglobs for files on remote machine?


A clean Ansible way of purging unwanted files matching a glob is:

- name: List all tmp files  find:    paths: /tmp/foo    patterns: "*.tmp"  register: tmp_glob- name: Cleanup tmp files  file:    path: "{{ item.path }}"    state: absent  with_items:    - "{{ tmp_glob.files }}"


Bruce P's solution works, but it requires an addition file and gets a little messy. Below is a pure ansible solution.

The first task grabs a list of filenames and stores it in files_to_copy. The second task appends each filename to the path you provide and creates symlinks.

- name: grab file list  shell: ls /path/to/src  register: files_to_copy- name: create symbolic links  file:    src: "/path/to/src/{{ item }}"    dest: "path/to/dest/{{ item }}"    state: link  with_items: files_to_copy.stdout_lines


The file module does indeed look on the server where ansible is running for files when using with_fileglob, etc. Since you want to work with files that exist solely on the remote machine then you could do a couple things. One approach would be to copy over a shell script in one task then invoke it in the next task. You could even use the fact that the file was copied as a way to only run the script if it didn't already exist:

- name: Copy link script  copy: src=/path/to/foo.sh        dest=/target/path/to/foo.sh        mode=0755  register: copied_script- name: Invoke link script  command: /target/path/to/foo.sh  when: copied_script.changed

Another approach would be to create an entire command line that does what you want and invoke it using the shell module:

- name: Generate links  shell: find ~/.zprezto/runcoms/z* -exec ln -s {} ~ \;