Skip to content


copying files among hosts with ansible

To copy one file from one machine to a set of hosts? Under the ahd label, there is a group of hosts in /etc/ansible/hosts.

[ahd]
host1
host2
host3

There is a host called sysmgttl1 with a file /etc/testWMD, we want to copy to every hosts in ansibel ahd group.
This task is easily accomplished with a playbook with the following content:

- hosts: ahd
  tasks:
    - name: Transfer file from sysmgttl1 to hosts in the ahd group
      synchronize:
        src: /etc/testWMD
        dest: /etc/testWMD
      delegate_to: sysmgttl1

Our playbook is called ehd_sync

# ansible-playbook ehd_sync
 ____________
< PLAY [ahd] >
 ------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
 ______________
< TASK [setup] >
 --------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
ok: [host1]
ok: [host2]
ok: [host3]
______________________________________________
< TASK [Transfer file from ServerA to ServerB] >
 ----------------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

changed: [host1 -> sysmgttl1]
changed: [host3 -> sysmgttl1]
changed: [host2 -> sysmgttl1]
 ____________
< PLAY RECAP >
 ------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

host1          : ok=2    changed=1    unreachable=0    failed=0
host2          : ok=2    changed=1    unreachable=0    failed=0
host3          : ok=2    changed=1    unreachable=0    failed=0

To check/validate:

# ansible -a "ls -l /etc/testWMD" aha
host1 | SUCCESS | rc=0 >>
-rw-r--r-- 1 root root 0 Oct  7 14:17 /etc/testWMD
host2 | SUCCESS | rc=0 >>
-rw-r--r-- 1 root root 0 Oct  7 14:17 /etc/testWMD
host3 | SUCCESS | rc=0 >>
-rw-r--r-- 1 root root 0 Oct  7 14:17 /etc/testWMD

Posted in Linux.

Tagged with , , , .


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.

WordPress Anti Spam by WP-SpamShield



Copyright © 2016 - 2017 Waldemar Mark Duszyk. All Rights Reserved. Created by Blog Copyright.