Error "The conditional check 'file_stat.stat.exists == False' failed. The error was: error while evaluating conditional (file_stat.stat.exists == False): 'file_stat' is undefined

with what I am doing shouldn’t file_stat get registered as part of the loop? What am I doing wrong?

I get the following error when trying to run my script below:

PLAY [Download and install RPMs from a local directory] ************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [Create directory if it does not exist] ***********************************
ok: [localhost]

TASK [Download RPMs] ***********************************************************
fatal: [localhost]: FAILED! => {“msg”: “The conditional check ‘file_stat.stat.exists == False’ failed. The error was: error while evaluating conditional (file_stat.stat.exists == False): ‘file_stat’ is undefined\n\nThe error appears to be in ‘/home/pvs/Build/playbooks/DownloadRPM.yml’: line 17, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n mode: 0755\n - name: Download RPMs\n ^ here\n”}

My script:

Does your “register: file_stat” needs to be moved up to the file task?

Walter

I added the following task verifying file exists, and show the results right below it, but I do not know how to reference the results using a "when: " for when the file does not exist. the results shows: " ‘stat’: {‘charset’: ‘binary’, ‘uid’: 0, ‘exists’: True, "
but stat is never found from the error

  • name: check if file exists
    local_action: stat path=“/home/pvs/Build/data/{{ item }}.x86_64.rpm”
    with_items: “{{ rpms }}”
    register: file_stat

  • name: Verifying if file exists
    debug: msg=“File {{ item }} exist”
    with_items: “{{ file_stat.results }}”

PLAY [Download and install RPMs from a local directory] ***********************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Create directory if it does not exist] **********************************************************************************************************************************************************************
ok: [localhost]

TASK [check if file exists] ***************************************************************************************************************************************************************************************
ok: [localhost → localhost] => (item=Common-1.0.0-37)
ok: [localhost → localhost] => (item=Database-1.0.0-122)

TASK [Verifying if file exists] ***********************************************************************************************************************************************************************************
ok: [localhost] => (item={‘invocation’: {‘module_args’: {‘checksum_algorithm’: ‘sha1’, ‘get_checksum’: True, ‘follow’: False, ‘path’: ‘/home/pvs/Build/data/Common-1.0.0-37.x86_64.rpm’, ‘get_md5’: False, ‘get_mime’: True, ‘get_attributes’: True}}, ‘stat’: {‘charset’: ‘binary’, ‘uid’: 0, ‘exists’: True, ‘attr_flags’: ‘’, ‘woth’: False, ‘isreg’: True, ‘device_type’: 0, ‘mtime’: 1585590258.0, ‘block_size’: 4096, ‘inode’: 5856469, ‘isgid’: False, ‘size’: 205977384, ‘executable’: False, ‘isuid’: False, ‘readable’: True, ‘version’: ‘385713817’, ‘pw_name’: ‘root’, ‘gid’: 0, ‘ischr’: False, ‘wusr’: True, ‘writeable’: True, ‘mimetype’: ‘application/x-rpm’, ‘blocks’: 402312, ‘xoth’: False, ‘islnk’: False, ‘nlink’: 1, ‘issock’: False, ‘rgrp’: True, ‘gr_name’: ‘root’, ‘path’: ‘/home/pvs/Build/data/Common-1.0.0-37.x86_64.rpm’, ‘xusr’: False, ‘atime’: 1673993504.3784745, ‘isdir’: False, ‘ctime’: 1673993447.9204745, ‘isblk’: False, ‘wgrp’: False, ‘checksum’: ‘5738560f3137fb2f62dd9a179ed343daef01c24b’, ‘dev’: 64770, ‘roth’: True, ‘isfifo’: False, ‘mode’: ‘0644’, ‘xgrp’: False, ‘rusr’: True, ‘attributes’: }, ‘changed’: False, ‘failed’: False, ‘item’: ‘Common-1.0.0-37’, ‘ansible_loop_var’: ‘item’}) => {
“msg”: “File {‘invocation’: {‘module_args’: {‘checksum_algorithm’: ‘sha1’, ‘get_checksum’: True, ‘follow’: False, ‘path’: ‘/home/pvs/Build/data/Common-1.0.0-37.x86_64.rpm’, ‘get_md5’: False, ‘get_mime’: True, ‘get_attributes’: True}}, ‘stat’: {‘charset’: ‘binary’, ‘uid’: 0, ‘exists’: True, ‘attr_flags’: ‘’, ‘woth’: False, ‘isreg’: True, ‘device_type’: 0, ‘mtime’: 1585590258.0, ‘block_size’: 4096, ‘inode’: 5856469, ‘isgid’: False, ‘size’: 205977384, ‘executable’: False, ‘isuid’: False, ‘readable’: True, ‘version’: ‘385713817’, ‘pw_name’: ‘root’, ‘gid’: 0, ‘ischr’: False, ‘wusr’: True, ‘writeable’: True, ‘mimetype’: ‘application/x-rpm’, ‘blocks’: 402312, ‘xoth’: False, ‘islnk’: False, ‘nlink’: 1, ‘issock’: False, ‘rgrp’: True, ‘gr_name’: ‘root’, ‘path’: ‘/home/pvs/Build/data/Common-1.0.0-37.x86_64.rpm’, ‘xusr’: False, ‘atime’: 1673993504.3784745, ‘isdir’: False, ‘ctime’: 1673993447.9204745, ‘isblk’: False, ‘wgrp’: False, ‘checksum’: ‘5738560f3137fb2f62dd9a179ed343daef01c24b’, ‘dev’: 64770, ‘roth’: True, ‘isfifo’: False, ‘mode’: ‘0644’, ‘xgrp’: False, ‘rusr’: True, ‘attributes’: }, ‘changed’: False, ‘failed’: False, ‘item’: ‘Common-1.0.0-37’, ‘ansible_loop_var’: ‘item’} exist”
}
ok: [localhost] => (item={‘invocation’: {‘module_args’: {‘checksum_algorithm’: ‘sha1’, ‘get_checksum’: True, ‘follow’: False, ‘path’: ‘/home/pvs/Build/data/Database-1.0.0-122.x86_64.rpm’, ‘get_md5’: False, ‘get_mime’: True, ‘get_attributes’: True}}, ‘stat’: {‘charset’: ‘binary’, ‘uid’: 0, ‘exists’: True, ‘attr_flags’: ‘’, ‘woth’: False, ‘isreg’: True, ‘device_type’: 0, ‘mtime’: 1612995841.0, ‘block_size’: 4096, ‘inode’: 1629, ‘isgid’: False, ‘size’: 12408, ‘executable’: False, ‘isuid’: False, ‘readable’: True, ‘version’: ‘235891764’, ‘pw_name’: ‘root’, ‘gid’: 0, ‘ischr’: False, ‘wusr’: True, ‘writeable’: True, ‘mimetype’: ‘application/x-rpm’, ‘blocks’: 40, ‘xoth’: False, ‘islnk’: False, ‘nlink’: 1, ‘issock’: False, ‘rgrp’: True, ‘gr_name’: ‘root’, ‘path’: ‘/home/pvs/Build/data/Database-1.0.0-122.x86_64.rpm’, ‘xusr’: False, ‘atime’: 1674050294.847356, ‘isdir’: False, ‘ctime’: 1673994773.5614717, ‘isblk’: False, ‘wgrp’: False, ‘checksum’: ‘56730380852174a19f81253383a540689399ea9b’, ‘dev’: 64770, ‘roth’: True, ‘isfifo’: False, ‘mode’: ‘0644’, ‘xgrp’: False, ‘rusr’: True, ‘attributes’: }, ‘changed’: False, ‘failed’: False, ‘item’: ‘Database-1.0.0-122’, ‘ansible_loop_var’: ‘item’}) => {
“msg”: “File {‘invocation’: {‘module_args’: {‘checksum_algorithm’: ‘sha1’, ‘get_checksum’: True, ‘follow’: False, ‘path’: ‘/home/pvs/Build/data/Database-1.0.0-122.x86_64.rpm’, ‘get_md5’: False, ‘get_mime’: True, ‘get_attributes’: True}}, ‘stat’: {‘charset’: ‘binary’, ‘uid’: 0, ‘exists’: True, ‘attr_flags’: ‘’, ‘woth’: False, ‘isreg’: True, ‘device_type’: 0, ‘mtime’: 1612995841.0, ‘block_size’: 4096, ‘inode’: 1629, ‘isgid’: False, ‘size’: 12408, ‘executable’: False, ‘isuid’: False, ‘readable’: True, ‘version’: ‘235891764’, ‘pw_name’: ‘root’, ‘gid’: 0, ‘ischr’: False, ‘wusr’: True, ‘writeable’: True, ‘mimetype’: ‘application/x-rpm’, ‘blocks’: 40, ‘xoth’: False, ‘islnk’: False, ‘nlink’: 1, ‘issock’: False, ‘rgrp’: True, ‘gr_name’: ‘root’, ‘path’: ‘/home/pvs/Build/data/Database-1.0.0-122.x86_64.rpm’, ‘xusr’: False, ‘atime’: 1674050294.847356, ‘isdir’: False, ‘ctime’: 1673994773.5614717, ‘isblk’: False, ‘wgrp’: False, ‘checksum’: ‘56730380852174a19f81253383a540689399ea9b’, ‘dev’: 64770, ‘roth’: True, ‘isfifo’: False, ‘mode’: ‘0644’, ‘xgrp’: False, ‘rusr’: True, ‘attributes’: }, ‘changed’: False, ‘failed’: False, ‘item’: ‘Database-1.0.0-122’, ‘ansible_loop_var’: ‘item’} exist”
}

TASK [Download RPMs] **********************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {“msg”: “The conditional check ‘item.stat.exists == True’ failed. The error was: error while evaluating conditional (item.stat.exists == True): ‘ansible.utils.unsafe_proxy.AnsibleUnsafeText object’ has no attribute ‘stat’\n\nThe error appears to be in ‘/home/pvs/Build/playbooks/DownloadRPM.yml’: line 31, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Download RPMs\n ^ here\n”}

PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

Have you looked at the module docs? They show you what “results” is supposed to look like. If I may I’d like to suggest a slightly different approach. The Ansible philosophy is for each task to declare a state and let Ansible ensure the machine is in that state when it is done with the task.

Let’s break your problem down into three states:

  1. a local directory needs to exist
  2. RPMs need to be in that local directory
  3. RPMs in that local directory need to be installed

Given those three states you have three tasks:

  • name: make sure /home/pvs/Build/data exists
    file:
    path: /home/pvs/Build/data
    state: directory
    mode: 0755

The task above looks fine. It accomplishes state #1 in a single task.

  • name: Make sure RPMs are in /home/pvs/Build/data
    yum:
    name: “{{ item }}”
    state: present
    enablerepo: “*”
    dest: /home/pvs/Build/data/
    with_items: “{{ rpms }}”

The task above doesn’t look great. I am not convinced it will download the packages to /home/pvs/Build/data. The yum module does not have a “dest” parameter. It has a download_dir parameter. The download_dir parameter is only effective when used in conjunction with download_only. The next question I have is why do you want these packages downloaded to an alternate local directory? Are they in a repo you have enabled? If so why download them vs simply installing them FROM THAT REPO? This would let you accomplish states 2 and 3 in a single step and eliminate state 1 entirely.

  • name: RPMs are installed from /home/pvs/Build/data
    yum:
    name: /home/pvs/Build/data/{{ item }} .x86_64.rpm
    state: present
    installroot: /home/pvs/Build/data
    enablerepo: “*”
    allowdowngrade: yes
    with_items: “{{ rpms }}”
    register: yum_result

Given what I wrote about task / state 2 it seems task 3 may be unnecessary.

Walter

Thank you for the help.
sorry I had corrected the yum download portion (see below), but did not update here. the below solves the download only part.
I need to be able to download it to a directory as I am not always going to be online to access repositories. I will be online at one point to get the files, but in the future may not as I switch between builds. I just do not know how to structure the when statement for downloading only when the file is not there.

  • name: Download RPMs
    yum:
    name: “{{ rpms }}”
    state: present
    enablerepo: “*”
    download_dir: /home/pvs/Build/data/
    download_only: true
    when: file_stat.stat.exists == False

Ansible checks before it makes changes.

You declare a configuration state. If the machine is in that state, Ansible makes no change.

Don’t check if the package has been downloaded. Let Ansible worry about that.

It is fair to worry about whether the download folder exists. For that you only need to name the folder in the file task.

file:
path: /home/pvs/Build/data
state: directory
mode: 0755

register: file_stat

Is /home/pvs/Build/data a local folder on the control host where you run Ansible, or on each remote machine?

Walter

file:
path: /home/pvs/Build/data
state: directory
mode: 0755

register: file_stat

Also notice that “register” aligns with “file” and not with the params of “file”.

Walter

the folder is on each host machine. The folder may already be there for a different set of builds, so just having the folder there does not mean the files have been downloaded.

this script will end up being run locally on each system. It will essentially be on a bunch of test boxes, that people will use the script to switch between build environments.

I have seen when download task is in there and there is no network connection the script errors out. I am hoping to avoid that.

My overall intention was to have an ansible script that runs locally that grabs a list of rpms from a file, checks if the files are in the local directory, if the files are not in the local directory downloads them, then installs the builds from a local directory. Normally the system will not be on a network that can reach repositories. It will only be on that network if they need to download.

thanks I saw my indent on the when was wrong, but it still gets the same error.

I assume I am not using Ansible as intended as I am very much a novice. I apperciate your patience.

thanks for the help. I took a different approach we can close this thread.