Ansible to rename a file if it exists

+1 vote

I need to rename /jp/Test to /jp/test only if /jp/Test exists , otherwise i dont need to perform this task. if Both exists i need to merge both into /jp/test

 hosts: test
  gather_facts: false
  vars:
    hostsfiles:
      - /jp/test
      - /jp/Test
    

tasks:
    - name: Check if file exists
      stat:
        path: "{{ item}}"
      with_items: "{{ hostsfiles }}"
      register: jpresult

    - name: test
      shell: mv "{{item.2.stat.path}}" /jp/test
      with_items:
        - "{{ jpresult.results }}"
      when: item.1.stat.exists == false and item.2.stat.exists == true
Dec 14, 2020 in Ansible by jp
• 150 points

recategorized Dec 14, 2020 by jp 4,021 views

1 answer to this question.

0 votes
Hi@Jp,

What output you are getting? Your playbook seems Ok. Are you getting any permission issues?
answered Dec 14, 2020 by MD
• 95,460 points
fatal: [test]: FAILED! => {
    "msg": "The conditional check 'item.1.stat.exists == false and item.2.stat.exists == true' failed. The error was: error while evaluating conditional (item.1.stat.exists == false and item.2.stat.exists == true): dict object has no element 1\n\nThe error appears to be in '/opt/ansible-roles/test.yml': line 20, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: test\n      ^ here\n"
}
Hi@Jp,

I think you are missing the quotes while comparing with when. You can go through the official document of Ansible and check the examples.

https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html
Hi @MD that didnt help
Hi@Jp,

First, debug your task 1 in the Ansible playbook and paste the output here.

Hi @ MD, Here i was checking for three files and the is the debug output.

ok: [test] => {
    "msg": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "changed": false,
                "failed": false,
                "invocation": {
                    "module_args": {
                        "checksum_algorithm": "sha1",
                        "follow": false,
                        "get_attributes": true,
                        "get_checksum": true,
                        "get_md5": null,
                        "get_mime": true,
                        "path": "/jp/test"
                    }
                },
                "item": "/jp/test",
                "stat": {
                    "atime": 1608096568.5612903,
                    "attr_flags": "e",
                    "attributes": [
                        "extents"
                    ],
                    "block_size": 4096,
                    "blocks": 8,
                    "charset": "us-ascii",
                    "checksum": "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3",
                    "ctime": 1607946570.5903072,
                    "dev": 2053,
                    "device_type": 0,
                    "executable": false,
                    "exists": true,
                    "gid": 0,
                    "gr_name": "root",
                    "inode": 112461946,
                    "isblk": false,
                    "ischr": false,
                    "isdir": false,
                    "isfifo": false,
                    "isgid": false,
                    "islnk": false,
                     "isreg": true,
                    "issock": false,
                    "isuid": false,
                    "mimetype": "text/plain",
                    "mode": "0644",
                    "mtime": 1607946569.8612385,
                    "nlink": 1,
                    "path": "/jp/test",
                    "pw_name": "root",
                    "readable": true,
                    "rgrp": true,
                    "roth": true,
                    "rusr": true,
                    "size": 4,
                    "uid": 0,
                    "version": "44126412",
                    "wgrp": false,
                    "woth": false,
                    "writeable": true,
                    "wusr": true,
                    "xgrp": false,
                    "xoth": false,
                    "xusr": false
                }
            },
            {
                "ansible_loop_var": "item",
                "changed": false,
                "failed": false,
                "invocation": {
                    "module_args": {
                        "checksum_algorithm": "sha1",
                        "follow": false,
                        "get_attributes": true,
                        "get_checksum": true,
                        "get_md5": null,
                        "get_mime": true,
                        "path": "/jp/Test"
                    }
                },
                "item": "/jp/Test",
                "stat": {
                    "exists": false
                }
            },
             {
                "ansible_loop_var": "item",
                "changed": false,
                "failed": false,
                "invocation": {
                    "module_args": {
                        "checksum_algorithm": "sha1",
                        "follow": false,
                        "get_attributes": true,
                        "get_checksum": true,
                        "get_md5": null,
                        "get_mime": true,
                        "path": "/jp/testc"
                    }
                },
                "item": "/jp/testc",
                "stat": {
                    "atime": 1608096569.918419,
                    "attr_flags": "e",
                    "attributes": [
                        "extents"
                    ],
                    "block_size": 4096,
                    "blocks": 8,
                    "charset": "us-ascii",
                    "checksum": "038d66a4f2732867598c4b8c23264abb52b176a1",
                    "ctime": 1607602348.390485,
                    "dev": 2053,
                    "device_type": 0,
                    "executable": false,
                    "exists": true,
                    "gid": 0,
                    "gr_name": "root",
                    "inode": 112462076,
                    "isblk": false,
                    "ischr": false,
                    "isdir": false,
                    "isfifo": false,
                    "isgid": false,
                    "islnk": false,
                    "isreg": true,
                    "issock": false,
                    "isuid": false,
                    "mimetype": "text/plain",
                    "mode": "0644",
                     "mtime": 1598905699.5482395,
                    "nlink": 1,
                    "path": "/jp/testc",
                    "pw_name": "root",
                    "readable": true,
                    "rgrp": true,
                    "roth": true,
                    "rusr": true,
                    "size": 2453,
                    "uid": 0,
                    "version": "184",
                    "wgrp": false,
                    "woth": false,
                    "writeable": true,
                    "wusr": true,
                    "xgrp": false,
                    "xoth": false,
                    "xusr": false
                }
            }
        ]
    }
}

Hi,

I have checked your output. In the second task, you have used one condition. In that, you have mentioned item.1 and item.2. But in the output, you can see that it is not having any counter. It will iterate automatically and check the condition. If possible remove the no. and try once.

webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP