unable to generate backup file with registered variable in ansible

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

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

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 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

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"

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

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’”

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

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