ansible fetch 批量下载服务器文件
2018-01-16 18:16
501 查看
今天使用 ansible 进行批量巡检操作。
思路是写一个 Playbooks,将巡检脚本上传到所有服务器 /tmp 目录下,然后执行,并取回输出的文件。输出的文件路径为:/tmp/log/ip.txt 。ip 为本机 ip 。
Playbooks 内容如下:
上传文件使用 copy 模块,执行文件用 shell 模块都没问题。
取回文件出了问题,每台服务器的文件名都是不一样的。
取回文件使用 fetch 模块。测试了如下语句,行不通:
疯子哥让我去看官方文档。
使用这个就可以从所有服务器上下载文件。解释一下:
发现问题了吗?对,这个脚本要求你的文件名必须包含 inventory_hostname ,
如果没有怎么办呢?使用下面的脚本:
解释一下:
首先调用 find,paths 即你存放文件的路径。 patterns 即你要跟的关键字,这里是 ,即通配符,匹配所有文件。你可以写为 .txt ,匹配所有 txt 文件。
第二行调用 fetch ,ansible 的 Fetches a file from remote nodes ,
src 即上面的find 查到出来的结果。
执行结果如下:
思路是写一个 Playbooks,将巡检脚本上传到所有服务器 /tmp 目录下,然后执行,并取回输出的文件。输出的文件路径为:/tmp/log/ip.txt 。ip 为本机 ip 。
Playbooks 内容如下:
--- - hosts: test remote_user: toptea tasks: - name: transfer file to server copy: src=/root/xunjian.sh dest=/tmp/xunjian.sh mode=755 - name: zhixing become: yes become_method: su shell: /bin/bash -x /tmp/pswd.sh
上传文件使用 copy 模块,执行文件用 shell 模块都没问题。
取回文件出了问题,每台服务器的文件名都是不一样的。
取回文件使用 fetch 模块。测试了如下语句,行不通:
ansible all -m fetch -a "src=/tmp/log/* dest=/tmp/"
疯子哥让我去看官方文档。
http://docs.ansible.com/ansible/latest/fetch_module.html#examples
fetch: src: /tmp/{{ inventory_hostname }}.txt dest: /tmp/ss-{{ inventory_hostname }} flat: yes
使用这个就可以从所有服务器上下载文件。解释一下:
//fetch 是调用这个模块 fetch: //src 是远程服务器的路径,这里的 inventory_hostname 就是填在 /etc/ansible/hosts 文件里面的内容。比如说 hosts 文件你填的是 192.168.1.3 // 那这里的 {{inventory_hostname}}.txt 就是 192.168.1.3.txt src: /tmp/{{ inventory_hostname }}.txt dest: /tmp/ss-{{ inventory_hostname }} flat: yes
发现问题了吗?对,这个脚本要求你的文件名必须包含 inventory_hostname ,
如果没有怎么办呢?使用下面的脚本:
tasks: - name: fucking find: paths: /tmp/log/ patterns: "*" recurse: no register: file_2_fetch - name: fuck your bitch fetch: src: "{{ item.path }}" dest: /tmp/ flat: yes with_items: "{{ file_2_fetch.files }}"
解释一下:
首先调用 find,paths 即你存放文件的路径。 patterns 即你要跟的关键字,这里是 ,即通配符,匹配所有文件。你可以写为 .txt ,匹配所有 txt 文件。
第二行调用 fetch ,ansible 的 Fetches a file from remote nodes ,
src 即上面的find 查到出来的结果。
执行结果如下:
[root@master ~]# ansible-playbook main.yaml PLAY [test] ************************************************************************************ TASK [Gathering Facts] ************************************************************************* ok: [192.168.153.22] TASK [fucking] ********************************************************************************* ok: [192.168.153.22] TASK [fuck your bitch] ************************************************************************* ok: [192.168.153.22] => (item={u'uid': 0, u'woth': False, u'mtime': 1516180038.2560008, u'inode': 34964981, u'isgid': False, u'size': 0, u'isuid': False, u'isreg': True, u'gid': 0, u'ischr': False, u'wusr': True, u'xoth': False, u'islnk': False, u'nlink': 1, u'issock': False, u'rgrp': True, u'path': u'/tmp/log/192.168.153.22.txt', u'xusr': False, u'atime': 1516181632.1700034, u'isdir': False, u'ctime': 1516181291.6150029, u'isblk': False, u'wgrp': False, u'xgrp': False, u'dev': 64768, u'roth': True, u'isfifo': False, u'mode': u'0644', u'rusr': True}) ok: [192.168.153.22] => (item={u'uid': 0, u'woth': False, u'mtime': 1516182493.8110049, u'inode': 1762530, u'isgid': False, u'size': 0, u'isuid': False, u'isreg': True, u'gid': 0, u'ischr': False, u'wusr': True, u'xoth': False, u'islnk': False, u'nlink': 1, u'issock': False, u'rgrp': True, u'path': u'/tmp/log/1.txt', u'xusr': False, u'atime': 1516182504.3540049, u'isdir': False, u'ctime': 1516182493.8110049, u'isblk': False, u'wgrp': False, u'xgrp': False, u'dev': 64768, u'roth': True, u'isfifo': False, u'mode': u'0644', u'rusr': True}) changed: [192.168.153.22] => (item={u'uid': 0, u'woth': False, u'mtime': 1516182519.4070048, u'inode': 1762531, u'isgid': False, u'size': 0, u'isuid': False, u'isreg': True, u'gid': 0, u'ischr': False, u'wusr': True, u'xoth': False, u'islnk': False, u'nlink': 1, u'issock': False, u'rgrp': True, u'path': u'/tmp/log/2.pdf', u'xusr': False, u'atime': 1516182519.4070048, u'isdir': False, u'ctime': 1516182519.4070048, u'isblk': False, u'wgrp': False, u'xgrp': False, u'dev': 64768, u'roth': True, u'isfifo': False, u'mode': u'0644', u'rusr': True}) PLAY RECAP ************************************************************************************* 192.168.153.22 : ok=3 changed=1 unreachable=0 failed=0 [root@master ~]# ls /tmp/ 192.168.153.22.txt 1.txt 2.pdf
相关文章推荐
- 在MVC中实现和网站不同服务器的批量文件下载以及NPOI下载数据到Excel的简单学习
- 实现从oss(阿里云)服务器以附件形式下载文件(含批量下载)
- java实现阿里OSS服务器多文件打包ZIP下载,批量下载
- Java 从服务器上批量打包(zip)下载文件到本地
- 实现从oss(阿里云)服务器以附件形式下载文件(含批量下载)
- FTP批量下载文件到SAP服务器
- 实现从oss(阿里云)服务器批量下载文件
- FTP服务器文件的批量下载
- 用Wget工具批量下载远程FTP服务器上的文件
- scp从多台机器上批量下载文件到本机(升级版)
- php excel下载模板方法2,放excel文件在服务器上,直接拉取
- xshell上传文件到服务器和下载文件到本地
- Linux系统安装rz/sz实现本地和服务器文件上传下载
- ASP.NET实现从服务器下载文件2
- Windows 服务器使用FTP出现“当前的安全设置不允许从该位置下载文件" 警告
- 用压缩流jsp实现从Tomcat服务器下载多个文件
- java实现服务器文件打包zip并下载的示例(边打包边下载)
- 批量上传SAP服务器文件至FTP服务器
- WinFrom通过WebClient向Web服务器上传下载文件
- 如何使IIS服务器支持.apk文件下载 - 步骤截图 - MIME类型设置