unable to generate backup file with registered variable in ansible

19 views
Skip to first unread message

Sadanand Alegaonkar

unread,
May 24, 2020, 2:46:43 AM5/24/20
to Ansible Project
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
---
 - 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}" "{{file.stdout}}"_bkp
      # with_items: "{{file.stdout}}"
       register: file1
     - debug:
         var: file1


EXPECTED RESULTS
File created with bkp extension

ACTUAL RESULTS
ERROR! failed at splitting arguments, either an unbalanced jinja2 block or quotes: cp "{{file.stdout}" "{{file.stdout}}"_bkp

The error appears to be in '/etc/ansible/database/pb_DBrename_deletefile.yml': line 12, column 8, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

         var: file
     - name: Rename file
       ^ here

Dick Visser

unread,
May 24, 2020, 12:52:55 PM5/24/20
to ansible...@googlegroups.com
On Sun, 24 May 2020 at 08:46, Sadanand Alegaonkar <sadananda...@gmail.com> wrote:
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 
You forgot a curly here 


{{file.stdout}}"_bkp
      # with_items: "{{file.stdout}}"
       register: file1
     - debug:
         var: file1


EXPECTED RESULTS
File created with bkp extension

ACTUAL RESULTS
ERROR! failed at splitting arguments, either an unbalanced jinja2 block or quotes: cp "{{file.stdout}" "{{file.stdout}}"_bkp

The error appears to be in '/etc/ansible/database/pb_DBrename_deletefile.yml': line 12, column 8, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

         var: file
     - name: Rename file
       ^ here

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a27726ee-fe4f-4adc-a58a-8467e633341a%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Sadanand Alegaonkar

unread,
May 24, 2020, 10:52:40 PM5/24/20
to ansible...@googlegroups.com
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  



--
Thanks and Regards
 Sadanand V.Alegaonkar
Cell No:9975632660

Stefan Hornburg (Racke)

unread,
May 25, 2020, 1:16:41 AM5/25/20
to ansible...@googlegroups.com
On 5/25/20 4:52 AM, Sadanand Alegaonkar wrote:
> 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
Racke
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> <https://groups.google.com/d/msgid/ansible-project/a27726ee-fe4f-4adc-a58a-8467e633341a%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> --
> Sent from a mobile device - please excuse the brevity, spelling and punctuation.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAL8fbwOaJOfb1Ai3uOXKQP2iUP_5gFCeoWk4iayJbiwL6fZRjw%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAL8fbwOaJOfb1Ai3uOXKQP2iUP_5gFCeoWk4iayJbiwL6fZRjw%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
>
>
> --
> Thanks and Regards
>  Sadanand V.Alegaonkar
> Cell No:9975632660
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CA%2Bo9MBTXKZM14f253-nnRa96um6hvuVdTBEyBF8Ye9KZ-wgwug%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CA%2Bo9MBTXKZM14f253-nnRa96um6hvuVdTBEyBF8Ye9KZ-wgwug%40mail.gmail.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Sadanand Alegaonkar

unread,
May 25, 2020, 4:42:55 AM5/25/20
to ansible...@googlegroups.com
hello Stefan,

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

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/28eaae98-fe4a-834c-565d-32c91e059da6%40linuxia.de.

Dick Visser

unread,
May 25, 2020, 5:08:29 AM5/25/20
to ansible...@googlegroups.com
On Mon, 25 May 2020 at 10:42, Sadanand Alegaonkar
<sadananda...@gmail.com> wrote:
>
> hello Stefan,
>
> I have to add more task in the playbook, that's why i going with this methods.

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"



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Stefan Hornburg (Racke)

unread,
May 25, 2020, 5:21:25 AM5/25/20
to ansible...@googlegroups.com
On 5/25/20 10:42 AM, Sadanand Alegaonkar wrote:
> 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
Racke

>
> On Mon, 25 May 2020 at 10:46, Stefan Hornburg (Racke) <ra...@linuxia.de <mailto:ra...@linuxia.de>> wrote:
>
> On 5/25/20 4:52 AM, Sadanand Alegaonkar wrote:
> > 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
>         Racke
>
> >
> > On Sun, 24 May 2020 at 22:22, Dick Visser <dick....@geant.org <mailto:dick....@geant.org>
> <mailto:dick....@geant.org <mailto:dick....@geant.org>>> wrote:
> >
> >
> >
> >     On Sun, 24 May 2020 at 08:46, Sadanand Alegaonkar <sadananda...@gmail.com
> <mailto:sadananda...@gmail.com>
> >         ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <mailto:ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>>.
> >         To view this discussion on the web visit
> >         https://groups.google.com/d/msgid/ansible-project/a27726ee-fe4f-4adc-a58a-8467e633341a%40googlegroups.com
> >       
>  <https://groups.google.com/d/msgid/ansible-project/a27726ee-fe4f-4adc-a58a-8467e633341a%40googlegroups.com?utm_medium=email&utm_source=footer>.
> >
> >     --
> >     Sent from a mobile device - please excuse the brevity, spelling and punctuation.
> >
> >     --
> >     You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> >     To unsubscribe from this group and stop receiving emails from it, send an email to
> >     ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <mailto:ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>>.
> >     To view this discussion on the web visit
> >   
>  https://groups.google.com/d/msgid/ansible-project/CAL8fbwOaJOfb1Ai3uOXKQP2iUP_5gFCeoWk4iayJbiwL6fZRjw%40mail.gmail.com
> >   
>  <https://groups.google.com/d/msgid/ansible-project/CAL8fbwOaJOfb1Ai3uOXKQP2iUP_5gFCeoWk4iayJbiwL6fZRjw%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> >
> >
> >
> > --
> > Thanks and Regards
> >  Sadanand V.Alegaonkar
> > Cell No:9975632660
> >
> > --
> > You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to
> > ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <mailto:ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>>.
> > To view this discussion on the web visit
> >
> https://groups.google.com/d/msgid/ansible-project/CA%2Bo9MBTXKZM14f253-nnRa96um6hvuVdTBEyBF8Ye9KZ-wgwug%40mail.gmail.com
> >
> <https://groups.google.com/d/msgid/ansible-project/CA%2Bo9MBTXKZM14f253-nnRa96um6hvuVdTBEyBF8Ye9KZ-wgwug%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
>
> --
> Ecommerce and Linux consulting + Perl and web application programming.
> Debian and Sympa administration. Provisioning with Ansible.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/28eaae98-fe4a-834c-565d-32c91e059da6%40linuxia.de.
>
>
>
> --
> Thanks and Regards
>  Sadanand V.Alegaonkar
> Cell No:9975632660
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CA%2Bo9MBRRNSoUN3SQfr_JOrKpNwZY_vc0GyuWa2eB1%3D-6ZxZWhQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CA%2Bo9MBRRNSoUN3SQfr_JOrKpNwZY_vc0GyuWa2eB1%3D-6ZxZWhQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.
signature.asc

Sadanand Alegaonkar

unread,
May 25, 2020, 5:37:08 AM5/25/20
to ansible...@googlegroups.com
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'"  


To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e4fe0416-206a-f144-0db8-a2fc0a050e45%40linuxia.de.

Stefan Hornburg (Racke)

unread,
May 25, 2020, 6:34:00 AM5/25/20
to ansible...@googlegroups.com
On 5/25/20 11:36 AM, Sadanand Alegaonkar wrote:
> 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
find:
file_type: 'file'
paths: '/var/log/'
patterns: '*.log'
register: find_logs
- name: Create backup files
copy:
src: "{{ item.path }}"
dest: "{{ item.path }}.bkp"
owner: "{{ item.pw_name }}"
group: "{{ item.gr_name }}"
mode: "{{ item.mode }}"
remote_src: true
with_items: "{{ find_logs.files }}"
- name: Remove original file
file:
state: absent
path: "{{ item.path }}"
with_items: "{{ find_logs.files }}"

Regards
Racke

> 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:
> <mailto:ansible-project%2Bunsu...@googlegroups.com <mailto:ansible-project%252Buns...@googlegroups.com>>
> >     <mailto:ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <mailto:ansible-project%2Bunsu...@googlegroups.com <mailto:ansible-project%252Buns...@googlegroups.com>>>.
> >     >         To view this discussion on the web visit
> >     >       
>  https://groups.google.com/d/msgid/ansible-project/a27726ee-fe4f-4adc-a58a-8467e633341a%40googlegroups.com
> >     >       
> >   
>   <https://groups.google.com/d/msgid/ansible-project/a27726ee-fe4f-4adc-a58a-8467e633341a%40googlegroups.com?utm_medium=email&utm_source=footer>.
> >     >
> >     >     --
> >     >     Sent from a mobile device - please excuse the brevity, spelling and punctuation.
> >     >
> >     >     --
> >     >     You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> >     >     To unsubscribe from this group and stop receiving emails from it, send an email to
> >     >     ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <mailto:ansible-project%2Bunsu...@googlegroups.com <mailto:ansible-project%252Buns...@googlegroups.com>>
> >     <mailto:ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <mailto:ansible-project%2Bunsu...@googlegroups.com <mailto:ansible-project%252Buns...@googlegroups.com>>>.
> >     >     To view this discussion on the web visit
> >     >   
> >   
>   https://groups.google.com/d/msgid/ansible-project/CAL8fbwOaJOfb1Ai3uOXKQP2iUP_5gFCeoWk4iayJbiwL6fZRjw%40mail.gmail.com
> >     >   
> >   
>   <https://groups.google.com/d/msgid/ansible-project/CAL8fbwOaJOfb1Ai3uOXKQP2iUP_5gFCeoWk4iayJbiwL6fZRjw%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> >     >
> >     >
> >     >
> >     > --
> >     > Thanks and Regards
> >     >  Sadanand V.Alegaonkar
> >     > Cell No:9975632660
> >     >
> >     > --
> >     > You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> >     > To unsubscribe from this group and stop receiving emails from it, send an email to
> >     > ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <mailto:ansible-project%2Bunsu...@googlegroups.com <mailto:ansible-project%252Buns...@googlegroups.com>>
> >     <mailto:ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <mailto:ansible-project%2Bunsu...@googlegroups.com <mailto:ansible-project%252Buns...@googlegroups.com>>>.
> >     > To view this discussion on the web visit
> >     >
> >   
>  https://groups.google.com/d/msgid/ansible-project/CA%2Bo9MBTXKZM14f253-nnRa96um6hvuVdTBEyBF8Ye9KZ-wgwug%40mail.gmail.com
> >     >
> >   
>  <https://groups.google.com/d/msgid/ansible-project/CA%2Bo9MBTXKZM14f253-nnRa96um6hvuVdTBEyBF8Ye9KZ-wgwug%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> >
> >
> >     --
> >     Ecommerce and Linux consulting + Perl and web application programming.
> >     Debian and Sympa administration. Provisioning with Ansible.
> >
> >     --
> >     You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> >     To unsubscribe from this group and stop receiving emails from it, send an email to
> >     ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <mailto:ansible-project%2Bunsu...@googlegroups.com <mailto:ansible-project%252Buns...@googlegroups.com>>.
> >     To view this discussion on the web visit
> >     https://groups.google.com/d/msgid/ansible-project/28eaae98-fe4a-834c-565d-32c91e059da6%40linuxia.de.
> >
> >
> >
> > --
> > Thanks and Regards
> >  Sadanand V.Alegaonkar
> > Cell No:9975632660
> >
> > --
> > You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to
> https://groups.google.com/d/msgid/ansible-project/CA%2Bo9MBRRNSoUN3SQfr_JOrKpNwZY_vc0GyuWa2eB1%3D-6ZxZWhQ%40mail.gmail.com
> >
> <https://groups.google.com/d/msgid/ansible-project/CA%2Bo9MBRRNSoUN3SQfr_JOrKpNwZY_vc0GyuWa2eB1%3D-6ZxZWhQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
>
> --
> Ecommerce and Linux consulting + Perl and web application programming.
> Debian and Sympa administration. Provisioning with Ansible.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/e4fe0416-206a-f144-0db8-a2fc0a050e45%40linuxia.de.
>
>
>
> --
> Thanks and Regards
>  Sadanand V.Alegaonkar
> Cell No:9975632660
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CA%2Bo9MBSrtH1VoJd-f%3DJPb0nAnsgDQbVOFixS93OVA6-CMBfmOg%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CA%2Bo9MBSrtH1VoJd-f%3DJPb0nAnsgDQbVOFixS93OVA6-CMBfmOg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
signature.asc

Sadanand Alegaonkar

unread,
May 25, 2020, 8:17:28 AM5/25/20
to ansible...@googlegroups.com
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   




To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a4b16231-d2ea-9268-1df6-a780f683722f%40linuxia.de.
Reply all
Reply to author
Forward
0 new messages