title: How to create latest backup file every time in ansible using time stamp. How to copy the latest file(same file to destination)
body: <p>I have a use case i want to create a backup file with latest time stamp and then I want to copy the same/latest back up file to destination.(I was using <code>item</code> and <code>with_items</code> for that). But <code>ansible</code> overriding the existing backup file and creating new file with same time stamp. When I copy the latest backup file it is not moving to destination because same time stamp file already exists. </p>
  
  <pre><code>---    
  # understanding with_items for correct syntax        
  - name: create a back up file         
    file:        
      path: /test/backup_path/backup_{{ansible_date_time.date}}.bkp    
      state: touch    
      mode: u=rw,g=r,o=r    
  
  - name: get the latest back up file    
    shell: ls -1t /test/backup_path | head -1    
    register: latest_bkp    
  - debug: msg="{{ latest_bkp.stdout_lines }}"    
  
  - name: Copy the latest file to restore path    
    copy:    
      src: /test/backup_path/{{ item }}    
      dest: /test/restore_path/    
      remote_src: yes     
      owner: root    
      group: root     
      mode: '0644'    
      force: yes    
    with_items: "{{ latest_bkp.stdout_lines }}"    
  
  </code></pre>
  
  <p>My target node output    </p>
  
  <pre><code>[root@localhost ~]# ls -alrt  /test/backup_path/    
  total 0    
  -rw-r--r--. 1 root root  0 Aug 19 17:33 backup1    
  -rw-r--r--. 1 root root  0 Aug 19 17:34 backup2    
  drwxr-xr-x. 5 root root 59 Aug 19 18:03 ..    
  drwxr-xr-x. 2 root root 65 Aug 19 18:49 .    
  -rw-r--r--. 1 root root  0 Aug 19 19:07 backup_2019-08-19.bkp    
  
  [root@localhost ~]# ls -alrt /test/restore_path/    
  total 0    
  -rw-r--r--. 1 root root  0 Aug 19 17:34 backup2    
  drwxr-xr-x. 5 root root 59 Aug 19 18:03 ..    
  -rw-r--r--. 1 root root  0 Aug 19 18:49 backup_2019-08-19.bkp    
  drwxr-xr-x. 2 root root 50 Aug 19 18:49 .
  </code></pre>