模組文件

  1. 查詢 file 模組文件。
    [deyu1@kvm19 ansible]$ ansible-doc file
    
  2. 查詢 file 模組文件的範例,變更檔案屬性。
    [deyu1@kvm19 ansible]$ ansible-doc file | grep ^EX -A7
    EXAMPLES:
    
    - name: Change file ownership, group and permissions
      file:
        path: /etc/foo.conf
        owner: foo
        group: foo
        mode: '0644'
    
  3. 新增屬性 1777 的檔案 /work。
    - name: Create an insecure file
      file:
        path: /work
        owner: root
        group: root
        mode: '1777'
    
  4. 建立軟連結
    - name: Create a symbolic link
      file:
        src: /file/to/link/to
        dest: /path/to/symlink
        owner: foo
        group: foo
        state: link
    
  5. 建立兩個硬連結
    - name: Create two hard links
      file:
        src: '/tmp/{{ item.src }}'
        dest: '{{ item.dest }}'
        state: link
      loop:
        - { src: x, dest: y }
        - { src: z, dest: k }
    
  6. touch 一個檔案,並使用符號方式設定屬性。
    - name: Touch a file, using symbolic modes to set the permissions (equivalent to 0644)
      file:
        path: /etc/foo.conf
        state: touch
        mode: u=rw,g=r,o=r
    
  7. touch 一個檔案,並增加或移除屬性。
    - name: Touch the same file, but add/remove some permissions
      file:
        path: /etc/foo.conf
        state: touch
        mode: u+rw,g-wx,o-rwx
    
  8. touch 一個檔案,不變更修改及存取的時間。
    - name: Touch again the same file, but dont change times this makes the task idempotent
      file:
        path: /etc/foo.conf
        state: touch
        mode: u+rw,g-wx,o-rwx
        modification_time: preserve
        access_time: preserve
    
  9. 如果目錄不存在,則建立目錄。
    - name: Create a directory if it does not exist
      file:
        path: /etc/some_directory
        state: directory
        mode: '0755'
    
  10. 更新修改及存取的時間。
    - name: Update modification and access time of given file
      file:
        path: /etc/some_file
        state: file
        modification_time: now
        access_time: now
    
  11. 設定存取時間。
    - name: Set access time based on seconds from epoch value
      file:
        path: /etc/another_file
        state: file
        access_time: '{{ "%Y%m%d%H%M.%S" | strftime(stat_var.stat.atime) }}'
    
  12. 變更目錄 (含次目錄及檔案) 的擁有者及群組。
    - name: Recursively change ownership of a directory
      file:
        path: /etc/foo
        state: directory
        recurse: yes
        owner: foo
        group: foo