# unable to generate backup file with registered variable in ansible

**URL:** <https://forum.ansible.com/t/unable-to-generate-backup-file-with-registered-variable-in-ansible/33012>\
**Category:** Ansible Project\
**Created:** [May 24, 2020, 6:46am UTC](https://forum.ansible.com/t/unable-to-generate-backup-file-with-registered-variable-in-ansible/33012 "2020-05-24T06:46:43Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadanand\_Alegaonkar](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@Sadanand\_Alegaonkar](https://forum.ansible.com/u/Sadanand_Alegaonkar)\
**Post date:** [May 24, 2020, 6:46am UTC](https://forum.ansible.com/t/unable-to-generate-backup-file-with-registered-variable-in-ansible/33012/1 "2020-05-24T06:46:43Z")

</div>

SUMMARY

I trynig to take backup of existing file. reported below error

ERROR! failed at splitting arguments, either an unbalanced jinja2 block or quotes: cp “{{file.stdout}” “{{file.stdout}}”\_bkp

ansible-playbook pb\_DBrename\_deletefile.yml -i alertdbhost

ISSUE TYPE

[root@XXXXXX database]#

ERROR! failed at splitting arguments, either an unbalanced jinja2 block or quotes: cp “{{file.stdout}” “{{file.stdout}}”\_bkp

Ansible Version:

[root@XXXXXX database]# ansible --version  
ansible 2.8.5  
config file = /etc/ansible/ansible.cfg  
configured module search path = [u’/root/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]  
ansible python module location = /usr/lib/python2.7/site-packages/ansible  
executable location = /bin/ansible  
python version = 2.7.5 (default, Jun 11 2019, 12:19:05) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]

OS / ENVIRONMENT / INSTALL METHOD

Red Hat Enterprise Linux Server release 7.6 (Maipo)

STEPS TO REPRODUCE

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 24, 2020, 4:52pm UTC](https://forum.ansible.com/t/unable-to-generate-backup-file-with-registered-variable-in-ansible/33012/2 "2020-05-24T16:52:28Z")

</div>

> SUMMARY
> 
> I trynig to take backup of existing file. reported below error
> 
> ERROR! failed at splitting arguments, either an unbalanced jinja2 block or quotes: cp “{{file.stdout}” “{{file.stdout}}”\_bkp

This error is correct, see below

> ansible-playbook pb\_DBrename\_deletefile.yml -i alertdbhost
> 
> ISSUE TYPE
> 
> [root@XXXXXX database]#
> 
> ERROR! failed at splitting arguments, either an unbalanced jinja2 block or quotes: cp “{{file.stdout}” “{{file.stdout}}”\_bkp
> 
> Ansible Version:
> 
> [root@XXXXXX database]# ansible --version  
> ansible 2.8.5  
> config file = /etc/ansible/ansible.cfg  
> configured module search path = [u’/root/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]  
> ansible python module location = /usr/lib/python2.7/site-packages/ansible  
> executable location = /bin/ansible  
> python version = 2.7.5 (default, Jun 11 2019, 12:19:05) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
> 
> OS / ENVIRONMENT / INSTALL METHOD
> 
> Red Hat Enterprise Linux Server release 7.6 (Maipo)
> 
> ## STEPS TO REPRODUCE
> 
> - name: Rename listener Trace file and Delete  
> hosts: dbhost  
> gather\_facts: no  
> tasks:
> - name: list file  
> shell: cd “{{item}}”; find \*.log  
> with\_items: “{{mount\_points}}”  
> register: file
> - debug:  
> var: file
> - name: Rename file  
> shell: cp “{{file.stdout}” "

You forgot a curly here

---

<div class="post-metadata">

**Author:** ![Sadanand\_Alegaonkar](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@Sadanand\_Alegaonkar](https://forum.ansible.com/u/Sadanand_Alegaonkar)\
**Post date:** [May 25, 2020, 2:52am UTC](https://forum.ansible.com/t/unable-to-generate-backup-file-with-registered-variable-in-ansible/33012/3 "2020-05-25T02:52:03Z")

</div>

Hello Dick,

Thanks for giving reply,  
Yes corrected this error but the purpose is not getting fulfilled.

I was trying to rename old file in the file system. it is giving below error.

getting “file.results[“stdout”]”: “VARIABLE IS NOT DEFINED!” error

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [May 25, 2020, 5:16am UTC](https://forum.ansible.com/t/unable-to-generate-backup-file-with-registered-variable-in-ansible/33012/4 "2020-05-25T05:16:06Z")

</div>

> Hello Dick,
> 
> Thanks for giving reply,  
> Yes corrected this error but the purpose is not getting fulfilled.
> 
> I was trying to rename old file in the file system. it is giving below error.
> 
> getting "file.results["stdout"]": "VARIABLE IS NOT DEFINED!" error

Hello Sadanand,

it would be much better to use existing applications on the server itself, e.g. logrotate or one  
of the dozens of backup programs for Linux.

Instead of using the shell, I would recommend "find" and "copy" modules.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Sadanand\_Alegaonkar](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@Sadanand\_Alegaonkar](https://forum.ansible.com/u/Sadanand_Alegaonkar)\
**Post date:** [May 25, 2020, 8:42am UTC](https://forum.ansible.com/t/unable-to-generate-backup-file-with-registered-variable-in-ansible/33012/5 "2020-05-25T08:42:02Z")

</div>

hello Stefan,

I have to add more task in the playbook, that’s why i going with this methods.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 25, 2020, 9:07am UTC](https://forum.ansible.com/t/unable-to-generate-backup-file-with-registered-variable-in-ansible/33012/6 "2020-05-25T09:07:56Z")

</div>

I don't see why you have to?  
Duplicating logrotate functionality through ansible through shell  
scripts that need then need to be run manually sounds like a lot of  
extra, error prone work.

"What an engineer can do is rarely what he should do"

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [May 25, 2020, 9:20am UTC](https://forum.ansible.com/t/unable-to-generate-backup-file-with-registered-variable-in-ansible/33012/7 "2020-05-25T09:20:52Z")

</div>

> hello Stefan,
> 
> I have to add more task in the playbook, that's why i going with this methods.

That is hardly a good reason, especially without further explanation.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Sadanand\_Alegaonkar](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@Sadanand\_Alegaonkar](https://forum.ansible.com/u/Sadanand_Alegaonkar)\
**Post date:** [May 25, 2020, 9:36am UTC](https://forum.ansible.com/t/unable-to-generate-backup-file-with-registered-variable-in-ansible/33012/8 "2020-05-25T09:36:16Z")

</div>

Hello Stefan,

Yes log rotation is good option i will this further…

I am working on file deletion use case. In that i am having different mount points directory, before deletion i need to take backup of old file and delete  
them .

I added mount name in the inventory.

Below is the Playbook…  
And also i am trying to get output only for registered variables, gives error like variable not defined  
name: Rename listener Trace file and Delete  
hosts: dbhost  
gather\_facts: no  
tasks:

- name: list file  
shell: cd “{{item}}”; find \*.log  
with\_items: “{{mount\_points}}”  
register: file
- debug:  
var: file.results[“stdout”]

Output:  
“file.results[“stdout”]”: “VARIABLE IS NOT DEFINED!: ‘list object’ has no attribute ‘stdout’”

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [May 25, 2020, 10:33am UTC](https://forum.ansible.com/t/unable-to-generate-backup-file-with-registered-variable-in-ansible/33012/9 "2020-05-25T10:33:12Z")

</div>

> Hello Stefan,
> 
> Yes log rotation is good option i will this further..
> 
> I am working on file deletion use case. In that i am having different mount points directory, before deletion i need to  
> take backup of old file and delete  
> them .

There are still tons of archive and backup programs that could do that for you. But if you insist to do it your way,  
please use at least proper Ansible modules:

- name: Find log files in /var/log  
&nbsp;&nbsp;find:  
&nbsp;&nbsp;&nbsp;&nbsp;file\_type: 'file'  
&nbsp;&nbsp;&nbsp;&nbsp;paths: '/var/log/'  
&nbsp;&nbsp;&nbsp;&nbsp;patterns: '\*.log'  
&nbsp;&nbsp;register: find\_logs  
- name: Create backup files  
&nbsp;&nbsp;copy:  
&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ item.path }}"  
&nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ item.path }}.bkp"  
&nbsp;&nbsp;&nbsp;&nbsp;owner: "{{ item.pw\_name }}"  
&nbsp;&nbsp;&nbsp;&nbsp;group: "{{ item.gr\_name }}"  
&nbsp;&nbsp;&nbsp;&nbsp;mode: "{{ item.mode }}"  
&nbsp;&nbsp;&nbsp;&nbsp;remote\_src: true  
&nbsp;&nbsp;with\_items: "{{ find\_logs.files }}"  
- name: Remove original file  
&nbsp;&nbsp;file:  
&nbsp;&nbsp;&nbsp;&nbsp;state: absent  
&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ item.path }}"  
&nbsp;&nbsp;&nbsp;with\_items: "{{ find\_logs.files }}"

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Sadanand\_Alegaonkar](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@Sadanand\_Alegaonkar](https://forum.ansible.com/u/Sadanand_Alegaonkar)\
**Post date:** [May 25, 2020, 12:16pm UTC](https://forum.ansible.com/t/unable-to-generate-backup-file-with-registered-variable-in-ansible/33012/10 "2020-05-25T12:16:29Z")

</div>

hello Stefen,

I really appreciate your effort to understand my concern.

I am specifying mount\_points variable in Inventory file and try to keep copy…

DC-N5K-01 ansible\_host=\*\*\*\*\*\* ansible\_ssh=\*\*\*\* mount\_points=[“/var/log”]

\*- name: list file  
shell: cd “{{item}}”; find \*.log  
with\_items: “{{mount\_points}}”  
register: file

- debug:  
var: file.results[“stdout”]

- name: rename file  
shell: cp file.results[“stdout”] file.results[“stdout”] .bkp
