# Restoring an ansible backup file

**URL:** <https://forum.ansible.com/t/restoring-an-ansible-backup-file/28338>\
**Category:** Ansible Developer\
**Created:** [August 15, 2018, 10:56pm UTC](https://forum.ansible.com/t/restoring-an-ansible-backup-file/28338 "2018-08-15T22:56:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony\_Owens](https://avatars.discourse-cdn.com/v4/letter/t/58956e/32.png) [@Tony\_Owens](https://forum.ansible.com/u/Tony_Owens)\
**Post date:** [August 15, 2018, 10:56pm UTC](https://forum.ansible.com/t/restoring-an-ansible-backup-file/28338/1 "2018-08-15T22:56:06Z")

</div>

I’m trying to copy a file out with backup option which works fine. I’m pretty sure I am overlooking over something simple here. Any help is appreciated.

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 16, 2018, 10:43am UTC](https://forum.ansible.com/t/restoring-an-ansible-backup-file/28338/2 "2018-08-16T10:43:06Z")

</div>

> I'm trying to copy a file out with backup option which works fine. I'm  
> pretty sure I am overlooking over something simple here. Any help is  
> appreciated.

For starter you shouldn't use the developer list for user questions  
[https://docs.ansible.com/ansible/2.6/community/communication.html#mailing-list-information](https://docs.ansible.com/ansible/2.6/community/communication.html#mailing-list-information)

> ---  
> - hosts: all  
> &nbsp;&nbsp;become: true  
> &nbsp;&nbsp;vars:  
> &nbsp;&nbsp;&nbsp;&nbsp;push: false  
> &nbsp;&nbsp;&nbsp;&nbsp;fallback: false  
> &nbsp;&nbsp;&nbsp;&nbsp;jksfile: ''  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;- name: "copy jks"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;copy:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "/etc/ansible/mydir/install/repo/{{ jksfile }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "/mydir/opt/repo/{{ jksfile }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner: me  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group: me  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backup: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: push == "true"

If you check the return values for the copy module you'll see it return backup\_file  
[https://docs.ansible.com/ansible/2.6/modules/copy\_module.html#return-values](https://docs.ansible.com/ansible/2.6/modules/copy_module.html#return-values)

So add «register: result» then you'll have the filename in {{ result.backup\_file }}, this might be easier.

> &nbsp;&nbsp;&nbsp;- name: "get backup file"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;find:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paths: /mydir/opt/repo  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;patterns: "\*{{ ansible\_date\_time.date }}\*"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: myfile
> 
> &nbsp;&nbsp;&nbsp;- name: "restore file"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;copy:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "{{myfile.files|map(attribute='path')|list}}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "/mydir/opt/repo/{{ jksfile }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner: me  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group: me  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;remote\_src: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: fallback == "true"

src doesn't take a list so you need to choose one of the element in the list, to take the first you need  
&nbsp;&nbsp;src: "{{(myfile.files|map(attribute='path')|list)[0]}}"

But remember, you might have multiple backups on the same day and find will return them all so you never now which one is in the first element.
