Ansible roles: change file extension


In a Vagrant setup, I use Ansible to provision the virtual machine.

In one of the Ansible roles, I have a task that copies some files inside ~/bin/ folder:


I want to create symlinks of those files so the end result looks like:

~/bin/one (symlink to ~/bin/
~/bin/two (symlink to ~/bin/

How can I achieve that? So far I have tried this but it doesn’t work:

I need to put a regex inside dest=~/bin/ that takes the file name (like and removes the extension ( becomes one) but I’m not sure how to do it.


I finally used this tasks:


From other useful filters chapter:

To get the root and extension of a path or filename (new in version 2.0):

You need to reference the first element of the list, so:

Of course, the above task works given the provisions specified in the task which you later edited out of the question, because fileglob lookup is running locally on the control machine (and in the previous task you used to copy the same files, so this one assumes they exist on local machine).

If you want to run the task singlehandedly you’d have first to prepare a list of the files on the target node with the find module and then running the above looping over the results.

Leave a Reply