Copy multiple files to different locations on remote server

87 views
Skip to first unread message

anushake...@gmail.com

unread,
Oct 29, 2018, 12:55:37 PM10/29/18
to Ansible Project
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

Mohan L

unread,
Oct 29, 2018, 1:32:39 PM10/29/18
to Ansible Project

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

Mohan L

unread,
Oct 29, 2018, 2:02:04 PM10/29/18
to Ansible Project

do you want to copy multiple target like this?

# cat site.yml 

---

- hosts: all

  vars:

    file_list:

      - { src: '/tmp/myfiles/file1.txt', dst: '/tmp/target1/file1.txt' }

      - { src: '/tmp/myfiles/file2.txt', dst: '/tmp/target2/file2.txt' }

      - { src: '/tmp/myfiles/file3.txt', dst: '/tmp/target3/file3.txt' }


  tasks:

    - name: Copy file to target node

      copy:

        src: "{{ item.src }}"

        dest: "{{ item.dst }}"

      with_items: "{{ file_list }}"




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



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


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

ok: [localhost]


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

ok: [localhost] => (item={u'src': u'/tmp/myfiles/file1.txt', u'dst': u'/tmp/target1/file1.txt'})

ok: [localhost] => (item={u'src': u'/tmp/myfiles/file2.txt', u'dst': u'/tmp/target2/file2.txt'})

ok: [localhost] => (item={u'src': u'/tmp/myfiles/file3.txt', u'dst': u'/tmp/target3/file3.txt'})


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

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


On Monday, October 29, 2018 at 10:25:37 PM UTC+5:30, anushake...@gmail.com wrote:

anushake...@gmail.com

unread,
Oct 30, 2018, 8:00:00 AM10/30/18
to Ansible Project
Thank you Mohan that worked for me

Keshipeddy Anusha

unread,
Nov 1, 2018, 12:18:27 AM11/1/18
to Ansible Project
How to replace the existing file on remote server ....should we use any specific module for that? I tried using force=yes but it didn't work for me.

On Tue, Oct 30, 2018, 5:30 PM <anushake...@gmail.com wrote:
Thank you Mohan that worked for me

--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4950ba71-0f0d-4630-8c1c-475fc93f8fc2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mohan L

unread,
Nov 1, 2018, 10:37:21 AM11/1/18
to Ansible Project

What is happening when you try to use force: yes? 

force 
bool
    Choices:
  • no
  • yes ←
the default is yes, which will replace the remote file when contents are different than the source. If no, the file will only be transferred if the destination does not exist.

aliases: thirsty

Keshipeddy Anusha

unread,
Nov 1, 2018, 10:41:53 AM11/1/18
to ansible...@googlegroups.com
Okay got it. Is there any specific module that we can use to replace a complete file in remote server? 

Thank you for your help.

Thanks,
Anusha

Mohan L

unread,
Nov 2, 2018, 4:50:09 AM11/2/18
to Ansible Project
copy module with force yes automatically replace it. Otherwise you may need to delete it before copy task. 

Keshipeddy Anusha

unread,
Nov 2, 2018, 4:51:33 AM11/2/18
to Ansible Project
No change Mohan
It is not replacing the file

Mohan L

unread,
Nov 2, 2018, 5:25:09 AM11/2/18
to Ansible Project
Try with command or shell module with cp -f.

Frank Thommen

unread,
Nov 2, 2018, 6:25:23 AM11/2/18
to ansible...@googlegroups.com
This is not the same:

* copy module copies from the controller (where you run the
ansible-playbook command) to the client
* cp -f copies a file locally from client to itself

frank



On 11/02/2018 10:25 AM, Mohan L wrote:
> Try with command or shell module with cp -f.
>
> On Friday, November 2, 2018 at 2:21:33 PM UTC+5:30, Keshipeddy Anusha wrote:
>
> No change Mohan
> It is not replacing the file
>
> On Fri, Nov 2, 2018, 2:20 PM Mohan L <thefo...@gmail.com
> <javascript:> wrote:
>
> copy module with force yes automatically replace it. Otherwise
> you may need to delete it before copy task.
>
> On Thursday, November 1, 2018 at 8:11:53 PM UTC+5:30, Keshipeddy
> Anusha wrote:
>
> Okay got it. Is there any specific module that we can use to
> replace a complete file in remote server?
>
> Thank you for your help.
>
> Thanks,
> Anusha
>
> On Thu, Nov 1, 2018, 8:07 PM Mohan L <thefo...@gmail.com wrote:
>
>
> What is happening when you try to use force: yes?
>
> *force*
> bool
>
>
> * *Choices:*no
> *
> *yes* ←
>
>
> the default is |yes|, which will replace the remote file
> when contents are different than the source. If |no|,
> the file will only be transferred if the destination
> does not exist.
>
> aliases: thirsty
>
>
> On Thursday, November 1, 2018 at 9:48:27 AM UTC+5:30,
> Keshipeddy Anusha wrote:
>
> How to replace the existing file on remote server
> ....should we use any specific module for that? I
> tried using force=yes but it didn't work for me.
>
> On Tue, Oct 30, 2018, 5:30 PM <anushake...@gmail.com
> wrote:
>
> Thank you Mohan that worked for me
>
> --
> 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 post to this group, send email to
> ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/4950ba71-0f0d-4630-8c1c-475fc93f8fc2%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/4950ba71-0f0d-4630-8c1c-475fc93f8fc2%40googlegroups.com>.
> For more options, visit
> https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
> --
> 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 post to this group, send email to
> ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/01ef6833-b052-43ff-881e-70ad14e427e6%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/01ef6833-b052-43ff-881e-70ad14e427e6%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit
> https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
> --
> 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
> <javascript:>.
> To post to this group, send email to ansible...@googlegroups.com
> <javascript:>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/b6898b5a-9204-4c27-bded-506194a56cfd%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/b6898b5a-9204-4c27-bded-506194a56cfd%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
> --
> 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
> <mailto:ansible-proje...@googlegroups.com>.
> To post to this group, send email to ansible...@googlegroups.com
> <mailto:ansible...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/f963a9e3-56c8-4ebb-a3c7-8b7cbe5b812d%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/f963a9e3-56c8-4ebb-a3c7-8b7cbe5b812d%40googlegroups.com?utm_medium=email&utm_source=footer>.

Frank Thommen

unread,
Nov 2, 2018, 6:28:18 AM11/2/18
to ansible...@googlegroups.com
Then your remote file is most probably identical to the one you are
trying to copy ;-)

Did you check that your local file (src) is for sure different from the
remote one (dest)? Run `ansible-playbook` with '-vvv' to see, which
local files ansible selects (this can be confusing when you are using
roles) and compare with remote file.

frank


On 11/02/2018 09:51 AM, Keshipeddy Anusha wrote:
> No change Mohan
> It is not replacing the file
>
> On Fri, Nov 2, 2018, 2:20 PM Mohan L <thefo...@gmail.com
> <mailto:thefo...@gmail.com> wrote:
>
> copy module with force yes automatically replace it. Otherwise you
> may need to delete it before copy task.
>
> On Thursday, November 1, 2018 at 8:11:53 PM UTC+5:30, Keshipeddy
> Anusha wrote:
>
> Okay got it. Is there any specific module that we can use to
> replace a complete file in remote server?
>
> Thank you for your help.
>
> Thanks,
> Anusha
>
> On Thu, Nov 1, 2018, 8:07 PM Mohan L <thefo...@gmail.com wrote:
>
>
> What is happening when you try to use force: yes?
>
> *force*
> bool
>
>
> * *Choices:*no
> *
> *yes* ←
>
>
> the default is |yes|, which will replace the remote file
> when contents are different than the source. If |no|, the
> <https://groups.google.com/d/msgid/ansible-project/01ef6833-b052-43ff-881e-70ad14e427e6%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.
>
> --
> 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
> <mailto:ansible-proje...@googlegroups.com>.
> To post to this group, send email to
> ansible...@googlegroups.com
> <mailto:ansible...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/b6898b5a-9204-4c27-bded-506194a56cfd%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/b6898b5a-9204-4c27-bded-506194a56cfd%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.
>
> --
> 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
> <mailto:ansible-proje...@googlegroups.com>.
> To post to this group, send email to ansible...@googlegroups.com
> <mailto:ansible...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAEME5dR5Xi7SU%2BPRV11GohZgQ2%2BDM6%2B2hPu%2B-ehJOGc7DXkk%3Dg%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAEME5dR5Xi7SU%2BPRV11GohZgQ2%2BDM6%2B2hPu%2B-ehJOGc7DXkk%3Dg%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Mohan L

unread,
Nov 2, 2018, 6:40:48 AM11/2/18
to Ansible Project
Oh! yeah. I missed that part. 

Keshipeddy Anusha

unread,
Nov 2, 2018, 7:37:43 AM11/2/18
to Ansible Project
I am trying with below playbook and getting errors

---
- hosts: all
  tasks: 
      - name : replacing files
        Command:
         args: 
             src: /tmp/file1
              dest: /temp


--
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 post to this group, send email to ansible...@googlegroups.com.

Keshipeddy Anusha

unread,
Nov 5, 2018, 6:41:15 AM11/5/18
to Ansible Project
Could someone please help me in writing shell commands
Reply all
Reply to author
Forward
0 new messages