檔案管理 - file 模組

  1. file 模組可以做檔案管理,完整文件可用 ansible-doc 查詢 file 模組文件。
    [deyu1@kvm19  ansible]$ ansible-doc file
    
  2. file 模組檔案管理範例:
    1. 修改檔案的屬性
      -m file -a "dest=~/foo mode=600"
      
    2. 修改檔案的擁有者、所屬群組
      -m file -a "dest=~/foo owner=user1 group=shgrp"
      
    3. 建立目錄,並設定目錄屬性、擁有者、所屬群組。
      -m file -a "dest=~/c mode=755 owner=user1 group=shgrp state=directory"
      
    4. 刪除目錄
      -m file -a "dest=~/c state=absent"
      
  3. 使用 file 模組修改檔案 /home/deyu1/adhoc.txt 的屬性成 660,擁有者及群組皆為 deyu1。
    [deyu1@kvm19 ansible]$ ansible prod -m file -a "dest=/home/deyu1/adhoc.txt \
    mode=660 owner=deyu1 group=deyu1"
    kvm13 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "gid": 1000,
        "group": "deyu1",
        "mode": "0660",
        "owner": "deyu1",
        "path": "/home/deyu1/adhoc.txt",
        "secontext": "unconfined_u:object_r:user_home_t:s0",
        "size": 21,
        "state": "file",
        "uid": 1000
    }
    kvm14 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "gid": 1000,
        "group": "deyu1",
        "mode": "0660",
        "owner": "deyu1",
        "path": "/home/deyu1/adhoc.txt",
        "secontext": "unconfined_u:object_r:user_home_t:s0",
        "size": 21,
        "state": "file",
        "uid": 1000
    }
    
  4. 使用 shell 模組查看 prod 群組裡所有主機,adhoc.txt 的屬性已改成 660,擁有者及群組也都改成 deyu1 了。
    [deyu1@kvm19 ansible]$ ansible prod -m shell -a "ls -l /home/deyu1/adhoc.txt"
    kvm13 | CHANGED | rc=0 >>
    -rw-rw----. 1 deyu1 deyu1 21 Mar 24 06:37 /home/deyu1/adhoc.txt
    
    kvm14 | CHANGED | rc=0 >>
    -rw-rw----. 1 deyu1 deyu1 21 Mar 24 06:37 /home/deyu1/adhoc.txt