Restoring an ansible backup file

1,373 views
Skip to first unread message

Tony Owens

unread,
Aug 15, 2018, 6:56:06 PM8/15/18
to Ansible Development
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.



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

   - name: "get backup file"
     find:
       paths: /mydir/opt/repo
       patterns: "*{{ ansible_date_time.date }}*"
     register: myfile

   - name: "restore file"
     copy:
       src: "{{myfile.files|map(attribute='path')|list}}"
       dest: "/mydir/opt/repo/{{ jksfile }}"
       owner: me
       group: me
       remote_src: yes
     when: fallback == "true"

PLAY [all] ***************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************
ok: [server2]
ok: [server1]

TASK [copy jks] **********************************************************************************************************************************

TASK [get backup file] ****************************************************************************************************************************
ok: [server2]
ok: [server1]

TASK [debug] *************************************************************************************************************************************
ok: [server1] => {
    "msg": [
        "/mydir/opt/repo/trustedcerts_pn.jks.10547.2018-08-15@13:01:50~"
    ]
}
ok: [server2] => {
    "msg": [
        "/mydir/opt/repo/trustedcerts_pn.jks.13224.2018-08-15@13:01:50~"
    ]
}

TASK [restore file] ******************************************************************************************************************************
fatal: [server2]: FAILED! => {"changed": false, "failed": true, "msg": "Source ['/mydir/opt/repo/trustedcerts_pn.jks.13224.2018-08-15@13:01:50~'] not found"}
fatal: [server1]: FAILED! => {"changed": false, "failed": true, "msg": "Source ['/mydir/opt/repo/trustedcerts_pn.jks.10547.2018-08-15@13:01:50~'] not found"}

PLAY RECAP ***************************************************************************************************************************************
server1            : ok=3    changed=0    unreachable=0    failed=1
server2            : ok=3    changed=0    unreachable=0    failed=1 


Kai Stian Olstad

unread,
Aug 16, 2018, 6:43:16 AM8/16/18
to ansibl...@googlegroups.com
On Thursday, 16 August 2018 00.56.06 CEST Tony Owens wrote:
> 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


> ---
> - hosts: all
> become: true
> vars:
> push: false
> fallback: false
> jksfile: ''
> tasks:
> - name: "copy jks"
> copy:
> src: "/etc/ansible/mydir/install/repo/{{ jksfile }}"
> dest: "/mydir/opt/repo/{{ jksfile }}"
> owner: me
> group: me
> backup: yes
> 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

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


> - name: "get backup file"
> find:
> paths: /mydir/opt/repo
> patterns: "*{{ ansible_date_time.date }}*"
> register: myfile
>
> - name: "restore file"
> copy:
> src: "{{myfile.files|map(attribute='path')|list}}"
> dest: "/mydir/opt/repo/{{ jksfile }}"
> owner: me
> group: me
> remote_src: yes
> 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
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.


--
Kai Stian Olstad


Reply all
Reply to author
Forward
0 new messages