天天看點

通過Ansible進行的檔案管理

1、常用的檔案管理子產品

blockinfile:将将文本塊添加到現有檔案;

copy:将檔案複制到受管主機;

fetch:從受管主機拷貝檔案到控制節點;

file:設定檔案屬性;

lineinfile:確定特定位于某個檔案;

stat:檢索檔案狀态資訊;

synchronize:rsync指令的打包程式。

2、file子產品介紹

2.1 子產品的一些關鍵字

##file裡的關鍵字
...  
- tasks:
    - name: file
      file:
        path:       ##管理檔案的路徑
        owner:      ##設定擁有者
        group:      ##設定擁有組
        mode:       ##權限
        state:      ##檔案狀态:touch(建立)、absent(删除)
        setype:     ## 臨時設定selinux
        checksum_algorithm: md5  ##檢測檔案的md5校驗和
           

2.2 用例說明

##file參數示例
  1 ---
  2 - name: Test
  3   hosts: weba
  4   tasks:
  5     - name: Touch a file
  6       file:  
  7         path: /mnt/file     ##檔案存在時,修改檔案,不存在時建立檔案
  8         owner: linux
  9         group: linux
 10         mode: 0600
 11         state: touch
 12     - name: Set Selinux
 13       file:
 14         path: /mnt/file
 15         setype: samba_share_t
 16     - name: Verify the status     
 17       stat:      
 18         path: /mnt/file        
 19         checksum_algorithm: md5       
 20       register: result        
 21     - debug:
 22         msg: "The checksum is {{ result.stat.checksum }}"

           

【注】永久設定selinux

##永久設定selinux 
 ... 
 16     - name: set selinux
 17       sefcontext:       ##永久設定參數
 18         target: /mnt/file
 19         setype: samba_share_t
 20         state: present
......
           

3、file和template的差別

file主要是對檔案的管理,屬性的配置等多個問題;template主要是通過調用模闆,對受控主機進管理,雖然都是在受控主機産生檔案并進行相應操作,但是側重點是不同的。template更側重于檔案管理中的copy。

4、其他子產品

4.1 synchronzie    同步控制節點和受管主機之間的檔案

##synchronize

- name:
  synchonize:
  src:file     ##目前play目錄下的file同步到/mnt/file
  dest: /mnt/file
           

4.2 fetch  從受管主機拷貝到目前主機

##fetch

- name: fetch
  fetch:
    src: /mnt/file
    dest: /mnt/
    flat: yes    ##flat表示是否将dest以/結尾看成檔案,
                 ##上述事将/mnt/看成檔案,将file直接儲存下來
           

繼續閱讀