您好,登录后才能下订单哦!
这篇文章主要讲解了“fetch模块怎么批量取多台服务器多个文件”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“fetch模块怎么批量取多台服务器多个文件”吧!
临时有个需求:几十台机器的安全日志拷贝到一台指定服务器上。
考虑1. 最好是BS实现,如果是CS结构,每台机器都要部署agent
考虑2:每台服务器的安全日志文件命名是一样的,如果直接放到同一个目录下,重名导致覆盖
由此想到ansible的fetch模块,ansible是BS架构,fetch模块支持原始目录拷贝。但是直接用fetch模块不可行,因为支持单个文件。比如下面的写法会报错
ansible all -m fetch -a "src=/var/log/secure* dest=/tmp/,mode=0777"
ansible-playbook可以实现(但是目前没有找到取文件的同时改权限,可能因为mode不在fetch模块下面)。
下面是官方举例介绍(https://docs.ansible.com/ansible/latest/modules/fetch_module.html#examples ):
- name: Store file into /tmp/fetched/host.example.com/tmp/somefile fetch: src: /tmp/somefile dest: /tmp/fetched - name: Specifying a path directly fetch: src: /tmp/somefile dest: /tmp/prefix-{{ inventory_hostname }} flat: yes - name: Specifying a destination path fetch: src: /tmp/uniquefile dest: /tmp/special/ flat: yes - name: Storing in a path relative to the playbook fetch: src: /tmp/uniquefile dest: special/prefix-{{ inventory_hostname }} flat: yes
如果flat为yes,不按照src的目录来创建目录。flat为no就创建和src一样的目录。下面是实际使用案例:
[root@node1 ~]# cat secure_fetch.yml --- - name: 取secure日志 become: yes become_method: su hosts: test gather_facts: no tasks: - name: find log find: paths: /var/log/ patterns: "secure*" #recurse参数 : 默认情况下,只会在指定的目录中查找文件,也就是说,如果目录中还包含目录,ansible 并不会递归的进入子目录查找对应文件,如果想要递归的查找文件,需要使用 recurse 参数,当 recurse 参数设置为 yes 时,表示在指定目录中递归的查找文件。 recurse: no #register是钩子 register: file_fetch - name: fetch log fetch: src: "{{ item.path }}" dest: /home/securelog/ flat: no with_items: "{{ file_fetch.files }}"
[root@node1 ~]# ansible-playbook secure_fetch.yml PLAY [取secure日志] ****************************************************************************************************************************** TASK [find log] ******************************************************************************************************************************* ok: [192.168.44.13] ok: [192.168.44.12] TASK [fetch log] ****************************************************************************************************************************** failed: [192.168.44.13] (item={u'rusr': True, u'uid': 0, u'rgrp': False, u'xoth': False, u'islnk': False, u'woth': False, u'nlink': 1, u'issock': False, u'mtime': 1530416260.2022357, u'gr_name': u'root', u'path': u'/var/log/secure-20180701', u'xusr': False, u'atime': 1548943948.2019362, u'inode': 33866032, u'isgid': False, u'size': 44050, u'isdir': False, u'ctime': 1530418922.064081, u'roth': False, u'isblk': False, u'xgrp': False, u'isuid': False, u'dev': 64768, u'wgrp': False, u'isreg': True, u'isfifo': False, u'mode': u'0600', u'pw_name': u'root', u'gid': 0, u'ischr': False, u'wusr': True}) => {"changed": false, "file": "/home/securelog", "item": {"atime": 1548943948.2019362, "ctime": 1530418922.064081, "dev": 64768, "gid": 0, "gr_name": "root", "inode": 33866032, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mode": "0600", "mtime": 1530416260.2022357, "nlink": 1, "path": "/var/log/secure-20180701", "pw_name": "root", "rgrp": false, "roth": false, "rusr": true, "size": 44050, "uid": 0, "wgrp": false, "woth": false, "wusr": true, "xgrp": false, "xoth": false, "xusr": false}, "msg": "dest is an existing directory, use a trailing slash if you want to fetch src into that directory"} failed: [192.168.44.12] (item={u'rusr': True, u'uid': 0, u'rgrp': False, u'xoth': False, u'islnk': False, u'woth': False, u'nlink': 1, u'issock': False, u'mtime': 1536474972.9611943, u'gr_name': u'root', u'path': u'/var/log/secure-20180909', u'xusr': False, u'atime': 1549221167.0555239, u'inode': 33599300, u'isgid': False, u'size': 12806, u'isdir': False, u'ctime': 1536477361.7184083, u'roth': False, u'isblk': False, u'xgrp': False, u'isuid': False, u'dev': 64768, u'wgrp': False, u'isreg': True, u'isfifo': False, u'mode': u'0600', u'pw_name': u'root', u'gid': 0, u'ischr': False, u'wusr': True}) => {"changed": false, "file": "/home/securelog", "item": {"atime": 1549221167.0555239, "ctime": 1536477361.7184083, "dev": 64768, "gid": 0, "gr_name": "root", "inode": 33599300, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mode": "0600", "mtime": 1536474972.9611943, "nlink": 1, "path": "/var/log/secure-20180909", "pw_name": "root", "rgrp": false, "roth": false, "rusr": true, "size": 12806, "uid": 0, "wgrp": false, "woth": false, "wusr": true, "xgrp": false, "xoth": false, "xusr": false}, "msg": "dest is an existing directory, use a trailing slash if you want to fetch src into that directory"} failed: [192.168.44.13] (item={u'rusr': True, u'uid': 0, u'rgrp': False, u'xoth': False, u'islnk': False, u'woth': False, u'nlink': 1, u'issock': False, u'mtime': 1535096342.787248, u'gr_name': u'root', u'path': u'/var/log/secure-20180824', u'xusr': False, u'atime': 1548943948.6932404, u'inode': 33617866, u'isgid': False, u'size': 26386, u'isdir': False, u'ctime': 1535096402.271486, u'roth': False, u'isblk': False, u'xgrp': False, u'isuid': False, u'dev': 64768, u'wgrp': False, u'isreg': True, u'isfifo': False, u'mode': u'0600', u'pw_name': u'root', u'gid': 0, u'ischr': False, u'wusr': True}) => {"changed": false, "file": "/home/securelog", "item": {"atime": 1548943948.6932404, "ctime": 1535096402.271486, "dev": 64768, "gid": 0, "gr_name": "root", "inode": 33617866, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mode": "0600", "mtime": 1535096342.787248, "nlink": 1, "path": "/var/log/secure-20180824", "pw_name": "root", "rgrp": false, "roth": false, "rusr": true, "size": 26386, "uid": 0, "wgrp": false, "woth": false, "wusr": true, "xgrp": false, "xoth": false, "xusr": false}, "msg": "dest is an existing directory, use a trailing slash if you want to fetch src into that directory"} failed: [192.168.44.12] (item={u'rusr': True, u'uid': 0, u'rgrp': False, u'xoth': False, u'islnk': False, u'woth': False, u'nlink': 1, u'issock': False, u'mtime': 1542956975.197216, u'gr_name': u'root', u'path': u'/var/log/secure-20181123', u'xusr': False, u'atime': 1549221167.5528452, u'inode': 34774533, u'isgid': False, u'size': 9538, u'isdir': False, u'ctime': 1542957797.6701643, u'roth': False, u'isblk': False, u'xgrp': False, u'isuid': False, u'dev': 64768, u'wgrp': False, u'isreg': True, u'isfifo': False, u'mode': u'0600', u'pw_name': u'root', u'gid': 0, u'ischr': False, u'wusr': True}) => {"changed": false, "file": "/home/securelog", "item": {"atime": 1549221167.5528452, "ctime": 1542957797.6701643, "dev": 64768, "gid": 0, "gr_name": "root", "inode": 34774533, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mode": "0600", "mtime": 1542956975.197216, "nlink": 1, "path": "/var/log/secure-20181123", "pw_name": "root", "rgrp": false, "roth": false, "rusr": true, "size": 9538, "uid": 0, "wgrp": false, "woth": false, "wusr": true, "xgrp": false, "xoth": false, "xusr": false}, "msg": "dest is an existing directory, use a trailing slash if you want to fetch src into that directory"} failed: [192.168.44.12] (item={u'rusr': True, u'uid': 0, u'rgrp': False, u'xoth': False, u'islnk': False, u'woth': False, u'nlink': 1, u'issock': False, u'mtime': 1548927982.669854, u'gr_name': u'root', u'path': u'/var/log/secure-20190131', u'xusr': False, u'atime': 1549221168.0080547, u'inode': 34060138, u'isgid': False, u'size': 8339, u'isdir': False, u'ctime': 1548928021.9009967, u'roth': False, u'isblk': False, u'xgrp': False, u'isuid': False, u'dev': 64768, u'wgrp': False, u'isreg': True, u'isfifo': False, u'mode': u'0600', u'pw_name': u'root', u'gid': 0, u'ischr': False, u'wusr': True}) => {"changed": false, "file": "/home/securelog", "item": {"atime": 1549221168.0080547, "ctime": 1548928021.9009967, "dev": 64768, "gid": 0, "gr_name": "root", "inode": 34060138, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mode": "0600", "mtime": 1548927982.669854, "nlink": 1, "path": "/var/log/secure-20190131", "pw_name": "root", "rgrp": false, "roth": false, "rusr": true, "size": 8339, "uid": 0, "wgrp": false, "woth": false, "wusr": true, "xgrp": false, "xoth": false, "xusr": false}, "msg": "dest is an existing directory, use a trailing slash if you want to fetch src into that directory"} failed: [192.168.44.13] (item={u'rusr': True, u'uid': 0, u'rgrp': False, u'xoth': False, u'islnk': False, u'woth': False, u'nlink': 1, u'issock': False, u'mtime': 1542956975.1572466, u'gr_name': u'root', u'path': u'/var/log/secure-20181123', u'xusr': False, u'atime': 1548943949.150454, u'inode': 33604368, u'isgid': False, u'size': 18745, u'isdir': False, u'ctime': 1542959102.695395, u'roth': False, u'isblk': False, u'xgrp': False, u'isuid': False, u'dev': 64768, u'wgrp': False, u'isreg': True, u'isfifo': False, u'mode': u'0600', u'pw_name': u'root', u'gid': 0, u'ischr': False, u'wusr': True}) => {"changed": false, "file": "/home/securelog", "item": {"atime": 1548943949.150454, "ctime": 1542959102.695395, "dev": 64768, "gid": 0, "gr_name": "root", "inode": 33604368, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mode": "0600", "mtime": 1542956975.1572466, "nlink": 1, "path": "/var/log/secure-20181123", "pw_name": "root", "rgrp": false, "roth": false, "rusr": true, "size": 18745, "uid": 0, "wgrp": false, "woth": false, "wusr": true, "xgrp": false, "xoth": false, "xusr": false}, "msg": "dest is an existing directory, use a trailing slash if you want to fetch src into that directory"} failed: [192.168.44.13] (item={u'rusr': True, u'uid': 0, u'rgrp': False, u'xoth': False, u'islnk': False, u'woth': False, u'nlink': 1, u'issock': False, u'mtime': 1548925584.7210317, u'gr_name': u'root', u'path': u'/var/log/secure-20190131', u'xusr': False, u'atime': 1548943949.6768515, u'inode': 33576835, u'isgid': False, u'size': 4448, u'isdir': False, u'ctime': 1548926882.3378956, u'roth': False, u'isblk': False, u'xgrp': False, u'isuid': False, u'dev': 64768, u'wgrp': False, u'isreg': True, u'isfifo': False, u'mode': u'0600', u'pw_name': u'root', u'gid': 0, u'ischr': False, u'wusr': True}) => {"changed": false, "file": "/home/securelog", "item": {"atime": 1548943949.6768515, "ctime": 1548926882.3378956, "dev": 64768, "gid": 0, "gr_name": "root", "inode": 33576835, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mode": "0600", "mtime": 1548925584.7210317, "nlink": 1, "path": "/var/log/secure-20190131", "pw_name": "root", "rgrp": false, "roth": false, "rusr": true, "size": 4448, "uid": 0, "wgrp": false, "woth": false, "wusr": true, "xgrp": false, "xoth": false, "xusr": false}, "msg": "dest is an existing directory, use a trailing slash if you want to fetch src into that directory"} failed: [192.168.44.12] (item={u'rusr': True, u'uid': 0, u'rgrp': False, u'xoth': False, u'islnk': False, u'woth': False, u'nlink': 1, u'issock': False, u'mtime': 1549190406.9081187, u'gr_name': u'root', u'path': u'/var/log/secure-20190203', u'xusr': False, u'atime': 1549221168.5655358, u'inode': 34060148, u'isgid': False, u'size': 18456, u'isdir': False, u'ctime': 1549192442.462235, u'roth': False, u'isblk': False, u'xgrp': False, u'isuid': False, u'dev': 64768, u'wgrp': False, u'isreg': True, u'isfifo': False, u'mode': u'0600', u'pw_name': u'root', u'gid': 0, u'ischr': False, u'wusr': True}) => {"changed": false, "file": "/home/securelog", "item": {"atime": 1549221168.5655358, "ctime": 1549192442.462235, "dev": 64768, "gid": 0, "gr_name": "root", "inode": 34060148, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mode": "0600", "mtime": 1549190406.9081187, "nlink": 1, "path": "/var/log/secure-20190203", "pw_name": "root", "rgrp": false, "roth": false, "rusr": true, "size": 18456, "uid": 0, "wgrp": false, "woth": false, "wusr": true, "xgrp": false, "xoth": false, "xusr": false}, "msg": "dest is an existing directory, use a trailing slash if you want to fetch src into that directory"} failed: [192.168.44.13] (item={u'rusr': True, u'uid': 0, u'rgrp': False, u'xoth': False, u'islnk': False, u'woth': False, u'nlink': 1, u'issock': False, u'mtime': 1548944203.0243826, u'gr_name': u'root', u'path': u'/var/log/secure', u'xusr': False, u'atime': 1548943950.1240385, u'inode': 33599301, u'isgid': False, u'size': 16857, u'isdir': False, u'ctime': 1548944203.0243826, u'roth': False, u'isblk': False, u'xgrp': False, u'isuid': False, u'dev': 64768, u'wgrp': False, u'isreg': True, u'isfifo': False, u'mode': u'0600', u'pw_name': u'root', u'gid': 0, u'ischr': False, u'wusr': True}) => {"changed": false, "file": "/home/securelog", "item": {"atime": 1548943950.1240385, "ctime": 1548944203.0243826, "dev": 64768, "gid": 0, "gr_name": "root", "inode": 33599301, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mode": "0600", "mtime": 1548944203.0243826, "nlink": 1, "path": "/var/log/secure", "pw_name": "root", "rgrp": false, "roth": false, "rusr": true, "size": 16857, "uid": 0, "wgrp": false, "woth": false, "wusr": true, "xgrp": false, "xoth": false, "xusr": false}, "msg": "dest is an existing directory, use a trailing slash if you want to fetch src into that directory"} failed: [192.168.44.12] (item={u'rusr': True, u'uid': 0, u'rgrp': False, u'xoth': False, u'islnk': False, u'woth': False, u'nlink': 1, u'issock': False, u'mtime': 1549221421.9336913, u'gr_name': u'root', u'path': u'/var/log/secure', u'xusr': False, u'atime': 1549221169.0608518, u'inode': 34060157, u'isgid': False, u'size': 12077, u'isdir': False, u'ctime': 1549221421.9336913, u'roth': False, u'isblk': False, u'xgrp': False, u'isuid': False, u'dev': 64768, u'wgrp': False, u'isreg': True, u'isfifo': False, u'mode': u'0600', u'pw_name': u'root', u'gid': 0, u'ischr': False, u'wusr': True}) => {"changed": false, "file": "/home/securelog", "item": {"atime": 1549221169.0608518, "ctime": 1549221421.9336913, "dev": 64768, "gid": 0, "gr_name": "root", "inode": 34060157, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mode": "0600", "mtime": 1549221421.9336913, "nlink": 1, "path": "/var/log/secure", "pw_name": "root", "rgrp": false, "roth": false, "rusr": true, "size": 12077, "uid": 0, "wgrp": false, "woth": false, "wusr": true, "xgrp": false, "xoth": false, "xusr": false}, "msg": "dest is an existing directory, use a trailing slash if you want to fetch src into that directory"} to retry, use: --limit @/root/secure_fetch.retry PLAY RECAP ************************************************************************************************************************************ 192.168.44.12 : ok=1 changed=0 unreachable=0 failed=1 192.168.44.13 : ok=1 changed=0 unreachable=0 failed=1
我只创建了/home/securelog/目录,由于flat为no,执行playbook时自动在该目录下创建和源一样的/var/log/目录
[root@node1 ~]# tree /home/securelog/ /home/securelog/ ├── 192.168.44.12 │ └── var │ └── log │ ├── secure │ ├── secure-20180909 │ ├── secure-20181123 │ ├── secure-20190131 │ └── secure-20190203 └── 192.168.44.13 └── var └── log ├── secure ├── secure-20180701 ├── secure-20180824 ├── secure-20181123 └── secure-20190131 6 directories, 10 files
感谢各位的阅读,以上就是“fetch模块怎么批量取多台服务器多个文件”的内容了,经过本文的学习后,相信大家对fetch模块怎么批量取多台服务器多个文件这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。