技术
成就梦想!

通过Ansible fetch批量收集服务器log等日志文件

通过Ansible的fetch模块可以批量将服务器文件复制到Ansible服务器上面,如果我们收集多个文件收集文件名不固定的文件,只是单纯使用fetch模块就无法做到,因为ansible无法使用通配符匹配。

解决办法:

可以使用ansible剧本,find模块与fetch模块结合使用。

---
- hosts: any_server

  tasks:
    - name: find_file
      find:
        paths: /tmp/
        patterns: "*bmc.log"
        recurse: no
      register: file_name

    - name: copy_file
      fetch:
        src: "{{ item.path }}"
        dest: /root/bmc_log/
        flat: yes
      with_items: "{{ file_name.files }}"

剧本说明:首先通过find模块去搜索需要的文件,保存该文件路径,并将参数传递到fetch模块中,上传文件到ansible服务器。

hosts: any_server  是指要执行的主机组;
paths: /tmp/ 是指要搜索的文件的路径;
patterns: "*bmc.log"   是指要搜索的文件,使用通配符进行匹配;
dest: /root/bmc_log/   是指要上传到Ansible的服务器路径(上传文件到路径的话最后面“/”不能少);

image

参考资料:

参考资料1

参考资料2

赞(10)
未经允许不得转载:未来往事 » 通过Ansible fetch批量收集服务器log等日志文件

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址