Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I'm trying to turn these lines into something I can put in an ansible playbook:

# Install Prezto files shopt -s extglob shopt -s nullglob files=( "${ZDOTDIR:-$HOME}"/.zprezto/runcoms/!(README.md) ) for rcfile in "${files[@]}"; do     [[ -f $rcfile ]] && ln -s "$rcfile" "${ZDOTDIR:-$HOME}/.${rcfile##*/}" done 

So far I've got the following:

- name: Link Prezto files   file: src={{ item }} dest=~ state=link   with_fileglob:     - ~/.zprezto/runcoms/z* 

I know it isn't the same, but it would select the same files: except with_fileglob looks on the host machine, and I want it to look on the remote machine.

Is there any way to do this, or should I just use a shell script?

like image 677
callumacrae Avatar asked Jun 08 '14 23:06

callumacrae


People also ask

Which module can be used to copy files from a remote machine to control machine?

The copy module copies a file from the local or remote machine to a location on the remote machine. Use the fetch module to copy files from remote locations to the local box. If you need variable interpolation in copied files, use the template module. For Windows targets, use the win_copy module instead.

How copy file from remote server to local machine in ansible?

To copy a file from remote to local in ansible we use ansible fetch module. Fetch module is used to fetch the files from remote to local machine. In the following example i will show you how to copy a file from remote to local using ansible fetch module. Note: If you execute above playbook the target.

How do you copy multiple files into remote nodes by ansible in a task?

You can use the copy module in your Ansible playbook. This module can copy one or more files to the remote system. But you need to use the item keyword in your playbook for multiple files as shown below.


2 Answers

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 }}" 
like image 106
Doru C. Avatar answered Oct 15 '22 16:10

Doru C.


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 
like image 24
AdamY Avatar answered Oct 15 '22 17:10

AdamY