Home > OS >  Copy remote file to local using ansible - filename is variable
Copy remote file to local using ansible - filename is variable

Time:10-29

I'm trying to fetch some files from a remote server, however, the file name is generate by a variable date.

$(date --date="$(date %Y-%m-15) -1 month" '%Y-%m')

- hosts: remote_server
  tasks:
       fetch:
         src: /tmp/$(date --date="$(date  %Y-%m-15) -1 month"  '%Y-%m') 
         dst: /tmp/$(date --date="$(date  %Y-%m-15) -1 month"  '%Y-%m') 

CodePudding user response:

Declare the date command output as an ansible variable and use it in the task

- hosts: remote_server
  vars:
    dt: "{{ lookup('pipe', 'date --date="$(date  %Y-%m-15) -1 month"   "%Y-%m"') }}"
  tasks:
    fetch:
      src: "/tmp/{{ dt }}"
      dest: "/tmp/{{ dt }}"

Note that the value of dt will be dependant on the date of the control node.

  • Related