ansible fetch 批量下载服务器文件

今天使用 ansible 进行批量巡检操作。

思路是写一个 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
标签:Ansible 发布于:2019-10-30 02:50:34