Ansible roles: change file extension

14,901

Solution 1

From other useful filters chapter:

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

# with path == 'nginx.conf' the return would be ('nginx', '.conf')
{{ path | splitext }}

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

- name: Common tasks => Ensure symlinks for utilities exist
  file: src=~/bin/{{ item }} dest=~/bin/{{ (item | splitext)[0] }} mode=755 state=link
  with_fileglob:
    - bin/*

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.

Solution 2

I am using the following expression to change filename e.g. from test.file.name.txt to test.file.name.csv

"{{ ((item| splitext)[:-1] | join('.')) }}.csv"

This will split the item on any dot, cut the latest off and join all together again with a dot. After this you cann add the extension of your favour.

Share:
14,901

Related videos on Youtube

numediaweb
Author by

numediaweb

Hey there, I'm Abdel from Berlin!

Updated on June 04, 2022

Comments

  • numediaweb
    numediaweb almost 2 years

    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:

    ~/bin/one.sh
    ~/bin/two.sh

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

    ~/bin/one.sh
    ~/bin/two.sh
    ~/bin/one (symlink to ~/bin/one.sh)
    ~/bin/two (symlink to ~/bin/two.sh)

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

    - name: Common tasks =>Create symlinks for utilities
      file: src=~/bin/{{ item }} dest=~/bin/  mode=755 state=link
      with_fileglob:
        - bin/*
    

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

    Update

    I finally used this tasks:

    - name: Copy common files to ~/bin
      copy: src={{ item }} dest=~/bin/  mode=755
      with_fileglob:
        - bin/*
    
    - name: Ensure symlinks for utilities exist
      file: src=~/bin/{{ item | basename | regex_replace('(\w+(?:\.\w+)*$)', '\1') }} dest=~/bin/{{ item | basename | regex_replace('\.sh','') }} mode=755 state=link
      with_fileglob:
        - bin/*
    
    • numediaweb
      numediaweb about 7 years
      @techraf I updated my question thanks!
  • numediaweb
    numediaweb about 7 years
    Is there a way to combine with_fileglob with with_items ?
  • techraf
    techraf about 7 years
    I don't understand the question. Combine in what way?
  • numediaweb
    numediaweb about 7 years
    What I mean is that in one of my ansible roles I have some files inside devenv\ansible\roles\common\files\bin those are *.sh scripts that i want to create symlinks for inside the ~/bin/ folder of the VM.. so Instead of manually typing those file names, i want to get the names using with_fileglob and pass them into with_items.
  • numediaweb
    numediaweb about 7 years
    I tried using regex_replace with this expression \w+(?:\.\w+)*$ to extract file name but ansible says invalid group reference - name: Common tasks => Ensure symlinks for utilities exist file: src=~/bin/{{ item | regex_replace('\w+(?:\.\w+)*$', '\\g<1>') }} dest=~/bin/{{ (item | splitext)[0] }} mode=755 state=link with_fileglob: - bin/* this is the regex: regex101.com/r/LOfN61/1