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 




On Tuesday, January 17, 2023 at 1:26:27 PM UTC-5 [email protected] wrote:

> Does your "register: file_stat" needs to be moved up to the file task?
>
> Walter
> --
> Walter Rowe, Division Chief
> Infrastructure Services, OISM
> Mobile: 202.355.4123 <(202)%20355-4123>
>
> On Jan 17, 2023, at 1:13 PM, Matthew Franco <[email protected]> wrote:
>
> 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:
>
> ---
> - name: Download and install RPMs from a local directory
>   hosts: all
>   vars_files:
>     - rpms.yml
>   tasks:
>   - name: Create directory if it does not exist
>     file:
>       path: /home/pvs/Build/data
>       state: directory
>       mode: 0755
>   - name: Download RPMs
>     yum:
>       name: "{{ item }}"
>       state: present
>       enablerepo: "*"
>       dest: /home/pvs/Build/data/
>     with_items: "{{ rpms }}"
>     when: file_stat.stat.exists == False
>     register: file_stat
>   - name: Install RPMs from local directory
>     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
>   - name: check if package installation failed
>     debug:
>       msg: "RPM {{ item.name }} failed with error {{ item.rc }}"
>     with_items: "{{ yum_result.results }}"
>     when: item.rc != 0
>
>
>
>
> This Ansible script loads the list of RPMs to download from a separate 
> file called "rpms.yml" using the vars_files directive. This file should be 
> in the same directory as the playbook and should be in YAML format.
> The file should contain a list of RPMs names like this:
>
> rpms:
>   - file1
>   - file2
>   - file3
>
>
>
>
>
> Background on my script:
> This Ansible script first uses the yum command to download the RPMs to a 
> local directory, it will also check if the file already exists in the 
> directory before downloading it, if it already exists it will skip the 
> downloading task. Once all the files are downloaded, it will use the yum 
> command again to install the RPMs from the local directory.
> Please make sure to provide the correct path to the directory where you 
> want to store the RPMs, and the correct file names in the rpms.yml file.
>
>
> any help would be appreciated.
> Thank you
>
>
>
>
> -- 
> 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 [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/ansible-project/5f4a4ebb-0b39-4faf-bb2d-b9e99b0a13cdn%40googlegroups.com
>  
> <https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2F5f4a4ebb-0b39-4faf-bb2d-b9e99b0a13cdn%2540googlegroups.com%3Futm_medium%3Demail%26utm_source%3Dfooter&data=05%7C01%7Cwalter.rowe%40nist.gov%7C08e179f9dd494bace8f808daf8b6d736%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638095761438814493%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=Rq%2B4owRVfaszrNHevaEBYtzXJnVFZ9nn2oj6dBVuJUU%3D&reserved=0>
> .
>
>
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/c981cf50-4ab1-4922-b20a-e1ec4c7bd05fn%40googlegroups.com.

Reply via email to