ceuser@deyu:~/ansible_pb$ ansible-doc file | grep -A88 ^EX
EXAMPLES:
- name: Change file ownership, group and permissions
ansible.builtin.file:
path: /etc/foo.conf
owner: foo # 檔案擁有者
group: foo # 檔案所屬群組
mode: '0644' # 檔案屬性
- name: Give insecure permissions to an existing file
ansible.builtin.file:
path: /work
owner: root
group: root
mode: '1777'
- name: Create a symbolic link
ansible.builtin.file:
src: /file/to/link/to
dest: /path/to/symlink
owner: foo
group: foo
state: link # 建立軟連結檔
- name: Create two hard links
ansible.builtin.file:
src: '/tmp/{{ item.src }}'
dest: '{{ item.dest }}'
state: hard
loop:
- { src: x, dest: y }
- { src: z, dest: k }
- name: Touch a file, using symbolic modes to set the permissions (equivalent to 0644)
ansible.builtin.file:
path: /etc/foo.conf
state: touch
mode: u=rw,g=r,o=r
- name: Touch the same file, but add/remove some permissions
ansible.builtin.file:
path: /etc/foo.conf
state: touch
mode: u+rw,g-wx,o-rwx
- name: Touch again the same file, but do not change times this makes the task idempotent
ansible.builtin.file:
path: /etc/foo.conf
state: touch
mode: u+rw,g-wx,o-rwx
modification_time: preserve
access_time: preserve
- name: Create a directory if it does not exist
ansible.builtin.file:
path: /etc/some_directory
state: directory # 建立目錄
mode: '0755'
- name: Update modification and access time of given file
ansible.builtin.file:
path: /etc/some_file
state: file
modification_time: now
access_time: now
- name: Set access time based on seconds from epoch value
ansible.builtin.file:
path: /etc/another_file
state: file
access_time: '{{ "%Y%m%d%H%M.%S" | strftime(stat_var.stat.atime) }}'
- name: Recursively change ownership of a directory
ansible.builtin.file:
path: /etc/foo
state: directory
recurse: yes
owner: foo
group: foo
- name: Remove file (delete file)
ansible.builtin.file:
path: /etc/foo.txt
state: absent # 刪除檔案
- name: Recursively remove directory
ansible.builtin.file:
path: /etc/foo
state: absent # 刪除目錄
ceuser@deyu:~/ansible_pb$ ansible dev -m file -a "path=/home/ceuser/adhoc.txt \
> mode=660 owner=ceuser group=ceuser"
kvm11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"gid": 1000,
"group": "ceuser",
"mode": "0660",
"owner": "ceuser",
"path": "/home/ceuser/adhoc.txt",
"secontext": "unconfined_u:object_r:user_home_t:s0",
"size": 21,
"state": "file",
"uid": 1000
}
ceuser@deyu:~/ansible_pb$ ansible dev -m shell -a "ls -l /home/ceuser/adhoc.txt" kvm11 | CHANGED | rc=0 >> -rw-rw----. 1 ceuser ceuser 21 Feb 5 11:03 /home/ceuser/adhoc.txt