Download Multiple files from same location but with different names

70 views
Skip to first unread message

Amit Kulkarni

unread,
Jun 9, 2023, 3:23:52 PM6/9/23
to Ansible Project
Hello All,

I have the below playbook, I wanted to download the files from gitlab on same path with different names. I have below playbook, 
As can seen below source path is same but file name is different, I need to have src path defined and should mention only file names in loop or may be i can read these names from different file itself.

Appreciate any help with this
---
  - name: Download the files from GITLAB for Patching
    hosts: app
    tasks:
      - get_url:
          url: "{{ item }}"
          dest: /busdata/qwm/mna1/geodev12/folders/GX/PATCH/
          url_username:
          url_password:
        register: download
        until: download is succeeded
        delay: 3
        loop:

Abhijeet Kasurde

unread,
Jun 9, 2023, 3:38:17 PM6/9/23
to ansible...@googlegroups.com
Use

dest: "/busdata/qwm/mna1/geodev12/folders/GX/PATCH/{{ item | basename }}"

--
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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAON3ZR2d9hJ7PHdRW1ToRHQhXL9gm-Qa08aw0jb11vj2vWQH2A%40mail.gmail.com.


--
Thanks,
Abhijeet Kasurde

Amit Kulkarni

unread,
Jun 9, 2023, 4:21:11 PM6/9/23
to ansible...@googlegroups.com, akas...@redhat.com
Hello Abhijit,

Thanks for your help, I came up with another one, if you could provide your input that would be great.
Please let me know is this looking ok, what I am trying to do is t

---
 - name: "Download the patches for sage"
   hosts: app
   vars:
     my_dest: /busdata/qwm/mna1/geodev12/folders/GX/PATCH/
   tasks:
     - get_url:
        url: "{{ my_url }}/{{ my_file }}"
        dest: "{{ my_dest }}/{{ my_file }}"

        register: download
        until: download is succeeded
        delay: 3

        my_file: "{{ item }}"
        loop:
         - SRC_WMS_V12_04_0017.dat
         - SRC_WMS_V12_04_0018_txt.dat
         - SRC_WMS_V12_04_0019.dat
         - SRC_WMS_V12_04_0020.dat




Abhijeet Kasurde

unread,
Jun 9, 2023, 6:29:05 PM6/9/23
to Amit Kulkarni, ansible...@googlegroups.com
Little syntax correction -

---
 - name: "Download the patches for sage"
   hosts: app
   vars:
     my_dest: /busdata/qwm/mna1/geodev12/folders/GX/PATCH/
     my_url: "https://raw.gitlabusercontent.michelin.com/na-sc/na-log/wms_nca/wms_na_patch/sagepatch/"
   tasks:
     - get_url:
        url: "{{ my_url }}/{{ item }}"
        dest: "{{ my_dest }}/{{ item }}"

      register: download
      until: download is succeeded
      delay: 3
      loop:
         - SRC_WMS_V12_04_0017.dat
         - SRC_WMS_V12_04_0018_txt.dat
         - SRC_WMS_V12_04_0019.dat
         - SRC_WMS_V12_04_0020.dat
--
Thanks,
Abhijeet Kasurde

Amit Kulkarni

unread,
Jun 10, 2023, 3:32:23 PM6/10/23
to Ansible Project
.Hello All,

Can someone please help with below. 

I need to read the filename from an external file in ansible playbook, if someone has done something like this before please let me know.

In the loop there are DAT files which will be changing every time, so I need to keep those in an external file and read it in the main playbook.

Appreciate your help.

---
 - name: "Download the patches for sage"
   hosts: app
   vars:
     my_dest: /busdata/qwm/mna1/geodev12/folders/GX/PATCH/
   tasks:
     - get_url:
        url: "{{ my_url }}/{{ item }}"
        dest: "{{ my_dest }}/{{ item }}"
        register: download
        until: download is succeeded
        delay: 3
        my_file: "{{ item }}"
        loop:
         - SRC_WMS_V12_04_0017.dat
         - SRC_WMS_V12_04_0018_txt.dat
         - SRC_WMS_V12_04_0019.dat
         - SRC_WMS_V12_04_0020.dat
       

On Fri, Jun 9, 2023 at 8:22 PM Amit Kulkarni <amit1...@gmail.com> wrote:
Thanks Abhijeet for your help

Is there any way I can move the loop out of the playbook and make it read from the main playbook.

These are patch files and will be changing for each patching cycle.

Let me know  if this is possible.

Regards
Amit




Amit Kulkarni

unread,
Jun 10, 2023, 6:34:25 PM6/10/23
to Ansible Project, Abhijeet Kasurde

Hello All,

I am trying to work my way through this playbook, here is what I got so far, appreciate your input and let me know if this is correct..

Problem now is patches.txt will also be in same in repos as the actual files, so I am not sure how to provide the path to that file

Appreciate all your inputs here

---
 - name: "Download the patches for sage"
   hosts: app
   vars:
     my_dest: /busdata/qwm/mna1/geodev12/folders/GX/PATCH/
   tasks:
     - get_url:
        url: "{{ my_url }}/{{ item }}"
        url_username:
        url_password:
        dest: "{{ my_dest }}/{{ item }}"
        register: download
        until: download is succeeded
        delay: 3
        debug:
          var: item
          with_lines: cat "./path/pateches.txt"

Regards
Amit

Todd Lewis

unread,
Jun 10, 2023, 7:26:37 PM6/10/23
to ansible...@googlegroups.com, uto...@gmail.com
I think you're looking for something like this:
---
- name: Download the patches for sage
  hosts: app
  vars:
    my_dest: /busdata/qwm/mna1/geodev12/folders/GX/PATCH/
    my_url: https://raw.gitlabusercontent.michelin.com/na-sc/na-log/wms_nca/wms_na_patch/sagepatch/
  tasks:
    - name: Download files from my_url
      ansible.builtin.get_url:

        url: "{{ my_url }}/{{ item }}"
        url_username:
        url_password:
        dest: "{{ my_dest }}/{{ item }}"
      register: download
      until: download is succeeded
      delay: 3
      loop: "{{ lookup('ansible.builtin.file', 'patches.txt') | split('\n') }}"
If your "patches.txt" file is part of your source repo anyway, you could list the patches in a "group_vars/app.yml" file and use the containing variable directly. There are lots of other ways to do it as well.

Amit Kulkarni

unread,
Jun 10, 2023, 7:48:59 PM6/10/23
to Ansible Project, uto...@gmail.com
Thanks Todd.

If i keep the text file on remote server will  the  above work?.
I can provide the full path for the file in lookup and it will get picked  up one by one

Regards
Amit



Todd Lewis

unread,
Jun 10, 2023, 9:34:05 PM6/10/23
to Ansible Project, uto...@gmail.com
No. Lookups happen on the Ansible controller.

If the file is on the remote host(s), then prior to the task where you need the data, do an ansible.builtin.command task which `cat`s the file and registers the result. Then on the "Download the patches" task, say
    loop: "{{ registered_cat.stdout_lines }}"
Each item will then be one line from the file.

Amit Kulkarni

unread,
Jun 10, 2023, 9:52:52 PM6/10/23
to Ansible Project
Thanks Todd.

Let me try  it and shall keep you posted.

Regards
Amit

Reply all
Reply to author
Forward
0 new messages