Try like this:


# cat site.yml 

---

- hosts: all

  vars:

    file_list:

     - file1.txt 

     - file2.txt 

     - file3.txt 

     - file4.txt 


  tasks:

    - name: Copy file to target node

      copy:

        src: "/tmp/myfiles/{{ item }}"

        dest: "/tmp/target/{{ item }}"

      with_items: "{{ file_list }}"



# ls -1 /tmp/myfiles/

file1.txt

file2.txt

file3.txt

file4.txt



# ls -1 /tmp/target/

file1.txt

file2.txt

file3.txt

file4.txt



# ansible-playbook -i hosts site.yml -u root



PLAY [all] 
**********************************************************************************************************************************************************


TASK [Gathering Facts] 
**********************************************************************************************************************************************

ok: [localhost]


TASK [Copy file to target node] 
*************************************************************************************************************************************

ok: [localhost] => (item=file1.txt)

ok: [localhost] => (item=file2.txt)

ok: [localhost] => (item=file3.txt)

ok: [localhost] => (item=file4.txt)


PLAY RECAP 
**********************************************************************************************************************************************************

localhost                  : ok=2    changed=0    unreachable=0    failed=0  
 




Thanks
Mohan L

On Monday, October 29, 2018 at 10:25:37 PM UTC+5:30, [email protected] 
wrote:
>
> Hi,
>
> I am trying to copy multiple files to different locations using 
> with_items. Could someone please help me with this
>
> *vars: main.yml*
>
> wars:
> file1.war
> file2.war
> file3.war
> file4.war
>
> dest_path:
> /tmp/{wars}
>
>
> *tasks- main.yml*
> ---
> - hosts: all
>   name: copy file
>     copy: src={{ item.src }} dest={{ item.dest }}
>     with_items:
>       - { src: '{{ wars }}', dest: '{{ dest_path }}' }
>
> My idea is file1.war should go to /tmp/file1 folder etc
>

-- 
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/fc4560de-0532-4cd5-8c8e-b9a116557565%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to