firasKoubaa
firasKoubaa

Reputation: 6867

Ansible : how to rename a file when copying it

I have a task , where should I copy a file from its source , to its destination while renaming it in the destination .

My task looks like this :

- name: Go to the target folder
  shell: ls
  args:
   chdir: "{{pathTest}}/target"
  register: resultLS
- debug:
   msg: "{{resultLS}}"
- name: copy jar file
  copy:
   src: "{{resultLS.stdout}}"
   dest: "{{pathTest}}"
   mode: 0777

But, like this it copies the jar file with its same name , my purpose is how to rename it in the dest (ideally with the copy action)

Ideas ?

Upvotes: 0

Views: 6992

Answers (1)

techraf
techraf

Reputation: 68529

rename it to: renamed.jar

Here you are:

- name: Ensure the first matched file from {{ pathTest }}/target is present on the target
  copy:
    src: "{{ lookup('fileglob', pathTest + '/target/*') | first }}"
    dest: "{{ pathTest }}/renamed.jar"
    mode: 0777

Remarks:

  • Don't parse ls output!

  • Think how you should handle multiple files.

    in the example above - copy only the first one

Upvotes: 2

Related Questions