Using Ansible Playbook to Copy Files from Remote Server

Copy files from a remote server to a local computer. This can be accomplished using the Ansible fetch module. This is useful when you want to copy certain log files from a remote server to your local machine.

By default, a directory named after each host you are connecting to will be created in the target directory (local computer). The extracted files will be copied here. If the file does not exist on the remote server, no error will be raised by default.

1. Configuration File

[ee@cc ~]# more copy_fetch.yaml

– hosts: x.x.x.x

remote_user: root

gather_facts: false

tasks:

– name : Copy files from remote server

fetch:

src: /root/grafana-9.0.2-1.x86_64.rpm

dest: /usr/local/apps/

mode: 0644

flat: yes

fail_on_missing: yes

Using Ansible Playbook to Copy Files from Remote Server

(1). If using the flat parameter, and the filename is not unique, the existing file will be replaced each time the file is fetched.

(2). If you want to raise an error in case the source file is missing, set the fail_on_missing parameter to yes. An error will be raised if the remote file does not exist.

2. Test Execution[ee@cc ~]# ansible-playbook -C copy_fetch.yaml[ee@cc ~]# ansible-playbook copy_fetch.yamlUsing Ansible Playbook to Copy Files from Remote Server

3. Verification

[ee@cc ~]# cd /usr/local/apps/

[ee@cc apps]# ll

total 77640

-rw-r–r– 1 root root 79502154 Apr 9 16:05 grafana-9.0.2-1.x86_64.rpm

[ee@cc apps]# ll

total 77640

-rw-r–r– 1 root root 79502154 Apr 9 16:05 grafana-9.0.2-1.x86_64.rpm

———————-end———————

Recommended Reading

1. Practical – k8s Monitoring MySQL

2. Practical – Prometheus Monitoring Domain Certificate Expiration

3. Practical – Prometheus Monitoring MySQL

4. Practical – Prometheus Monitoring Redis

5. Nginx Dynamic IP Blocking with Lua + Redis

6. Comprehensive Interpretation and Practical Exercises of Jenkins

7. E-commerce Project Kubernetes Deployment and Performance Optimization Practice

8. Essential Shell Scripts for Work (2)

9. Essential Shell Scripts for Work

10. Essential Shell Scripts for Work (3)

If this article has been helpful to you, please like & view & share, this is very important for me to continue sharing & creating quality articles. Thank you 🙏🏻, if you need learning materials, please add WX:DaDangJia-01

Please like → light up “view” → share

Leave a Comment