您好,登錄后才能下訂單哦!
這篇文章主要講解了“fetch模塊怎么批量取多臺(tái)服務(wù)器多個(gè)文件”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“fetch模塊怎么批量取多臺(tái)服務(wù)器多個(gè)文件”吧!
臨時(shí)有個(gè)需求:幾十臺(tái)機(jī)器的安全日志拷貝到一臺(tái)指定服務(wù)器上。
考慮1. 最好是BS實(shí)現(xiàn),如果是CS結(jié)構(gòu),每臺(tái)機(jī)器都要部署agent
考慮2:每臺(tái)服務(wù)器的安全日志文件命名是一樣的,如果直接放到同一個(gè)目錄下,重名導(dǎo)致覆蓋
由此想到ansible的fetch模塊,ansible是BS架構(gòu),fetch模塊支持原始目錄拷貝。但是直接用fetch模塊不可行,因?yàn)橹С謫蝹€(gè)文件。比如下面的寫(xiě)法會(huì)報(bào)錯(cuò)
ansible all -m fetch -a "src=/var/log/secure* dest=/tmp/,mode=0777"
ansible-playbook可以實(shí)現(xiàn)(但是目前沒(méi)有找到取文件的同時(shí)改權(quán)限,可能因?yàn)閙ode不在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的目錄來(lái)創(chuàng)建目錄。flat為no就創(chuàng)建和src一樣的目錄。下面是實(shí)際使用案例:
[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參數(shù) : 默認(rèn)情況下,只會(huì)在指定的目錄中查找文件,也就是說(shuō),如果目錄中還包含目錄,ansible 并不會(huì)遞歸的進(jìn)入子目錄查找對(duì)應(yīng)文件,如果想要遞歸的查找文件,需要使用 recurse 參數(shù),當(dāng) recurse 參數(shù)設(shè)置為 yes 時(shí),表示在指定目錄中遞歸的查找文件。 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
我只創(chuàng)建了/home/securelog/目錄,由于flat為no,執(zhí)行playbook時(shí)自動(dòng)在該目錄下創(chuàng)建和源一樣的/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模塊怎么批量取多臺(tái)服務(wù)器多個(gè)文件”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)fetch模塊怎么批量取多臺(tái)服務(wù)器多個(gè)文件這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。