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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章