[ansible学习笔记]fetch模块

时间:2020-12-25 14:42:08

摘自官方文档:

  • This module works like copy, but in reverse. It is used for fetching files from remote machines and storing them locally in a file tree, organized by hostname. Note that this module is written to transfer log files that might not be present, so a missing remote file won’t be an error unless fail_on_missing is set to ‘yes’.

选项:

parameter required default choices comments
dest
yes

A directory to save the file into. For example, if the dest directory is /backup a src file named /etc/profile on host host.example.com, would be saved into /backup/host.example.com/etc/profile

fail_on_missing
no no
  • yes

  • no

When set to 'yes', the task will fail if the source file is missing.

flat
no

Allows you to override the default behavior of appending hostname/path/to/file to the destination. If dest ends with '/', it will use the basename of the source file, similar to the copy module. Obviously this is only handy if the filenames are unique.

src
yes

The file on the remote system to fetch. This must be a file, not a directory. Recursive fetching may be supported in a later release.

validate_checksum

(added in 1.4)

no yes
  • yes

  • no

Verify that the source and destination checksums match after the files are fetched.


aliases: validate_md5

样例:

[root@reedoracle reed]# cat roles/common/tasks/main.yml

---

- name: fetch

fetch:

src: /etc/hosts

dest: /tmp/fetchdir

[root@reedoracle reed]# cat site.yml

---

- hosts: app

tasks:

- name: mkdir -p /tmp/fetchdir

command: mkdir -p /tmp/fetchdir

roles:

- common

[root@reedoracle reed]# ansible-playbook site.yml

SSH password:

PLAY [app] *********************************************************************

TASK [setup] *******************************************************************

ok: [rhel56-192.168.209.133]

ok: [rhel61-192.168.209.142]

TASK [common : fetch] **********************************************************

changed: [rhel56-192.168.209.133]

changed: [rhel61-192.168.209.142]

TASK [mkdir -p /tmp/fetchdir] **************************************************

changed: [rhel56-192.168.209.133]

[WARNING]: Consider using file module with state=directory rather than running mkdir

changed: [rhel61-192.168.209.142]

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

rhel56-192.168.209.133     : ok=3    changed=2    unreachable=0    failed=0

rhel61-192.168.209.142     : ok=3    changed=2    unreachable=0    failed=0

[root@reedoracle reed]# ll /tmp/fetchdir/{rhel56-192.168.209.133,rhel61-192.168.209.142}/etc/

/tmp/fetchdir/rhel56-192.168.209.133/etc/:

total 4

-rw-r--r-- 1 root root 187 Feb 16 18:31 hosts

/tmp/fetchdir/rhel61-192.168.209.142/etc/:

total 4

-rw-r--r-- 1 root root 158 Feb 16 18:31 hosts


本文出自 “[reed@卢伟开~]#rm -rf /” 博客,请务必保留此出处http://luweikai.blog.51cto.com/1705672/1898821