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
On Wednesday, January 18, 2023 at 1:10:39 PM UTC-5 [email protected] wrote:
> 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
> --
> Walter Rowe, Division Chief
> Infrastructure Services, OISM
> Mobile: 202.355.4123 <(202)%20355-4123>
>
> On Jan 18, 2023, at 12:37 PM, Matthew Franco <[email protected]> wrote:
>
>
> 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
>> <https://gcc02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fitem.name%2F&data=05%7C01%7Cwalter.rowe%40nist.gov%7Ce29c9c9087a94f240dc908daf97b117f%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638096604220741953%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=CnLXkkcM0FOL%2BCvxSlknVbhRzxKWKEOdoazydC2uN9U%3D&reserved=0>
>>
>> }} 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%7Ce29c9c9087a94f240dc908daf97b117f%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638096604220741953%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=0Nnqc%2FdUYq8sZvN6GIZYVC88OUGRSeF%2BNq8cSkvnXNM%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
>
> <https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2Fc981cf50-4ab1-4922-b20a-e1ec4c7bd05fn%2540googlegroups.com%3Futm_medium%3Demail%26utm_source%3Dfooter&data=05%7C01%7Cwalter.rowe%40nist.gov%7Ce29c9c9087a94f240dc908daf97b117f%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638096604220741953%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=svN%2Fa8EYxmDpx3l0lsxA1Dm2cLUJyJE%2FJLIL3RQxWqY%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/e47aaf66-5e91-4fb0-8ee2-facd0529c238n%40googlegroups.com.