Ansible內(nèi)置模塊之copy
發(fā)布作者:微思網(wǎng)絡(luò) 發(fā)布時(shí)間:2024-09-18 瀏覽量:0次
注意: src:如果路徑是一個(gè)目錄,它將遞歸復(fù)制。在這種情況下,如果路徑使用/來結(jié)尾,則只復(fù)制目錄里的內(nèi)容,如果沒有使用/來結(jié)尾,則包含目錄在內(nèi)的整個(gè)內(nèi)容全部復(fù)制,類似于 rsync。 # 將本地文件復(fù)制到遠(yuǎn)程主機(jī) 選項(xiàng) 必須 類型 默認(rèn)值 說明 src 否 str 無(wú) 源文件的路徑。支持本地或遠(yuǎn)程路徑。如存在 content 提供,此選項(xiàng)可忽略 content 否 str 無(wú) 直接指定文件內(nèi)容,而不是從 src 讀取 dest 是 str 無(wú) 目標(biāo)路徑 mode 否 str 無(wú) 目標(biāo)文件的權(quán)限,如 0644、0755 等 owner 否 str 無(wú) 目標(biāo)文件的所有者 group 否 str 無(wú) 目標(biāo)文件的屬組 backup 否 bool no 如果目標(biāo)文件存在,是否創(chuàng)建備份 force 否 bool yes 如果目標(biāo)文件存在,是否強(qiáng)制覆蓋 validate 否 str 無(wú) 在復(fù)制之前驗(yàn)證目標(biāo)文件的命令??梢园嘉环?%s,表示目標(biāo)文件路徑 remote_src 否 bool no 如果為 yes,則表示 src 是遠(yuǎn)程路徑 selevel 否 str 無(wú) 驗(yàn)證文件的 SHA256 校驗(yàn)和 selevel 否 str 無(wú) 設(shè)置文件的 SELinux 安全級(jí)別 serole 否 str 無(wú) 設(shè)置文件的 SELinux 角色 setype 否 str 無(wú) 設(shè)置文件的 SELinux 類型 seuser 否 str 無(wú) 設(shè)置文件的 SELinux 用戶
- name: Copy a file to a remote host
ansible.builtin.copy:
src: /path/to/local/file
dest: /path/to/remote/file
# 直接指定文件內(nèi)容
- name: Copy content to a remote file
ansible.builtin.copy:
content: |
This is the content of the file.
dest: /path/to/remote/file
# 設(shè)置文件權(quán)限和所有者
- name: Copy a file and set permissions and owner
ansible.builtin.copy:
src: /path/to/local/file
dest: /path/to/remote/file
mode: '0644'
owner: myuser
group: mygroup
# 在復(fù)制之前驗(yàn)證文件
- name: Copy a file and validate before copying
ansible.builtin.copy:
src: /path/to/local/file
dest: /path/to/remote/file
validate: 'mycommand %s'
# 從遠(yuǎn)程路徑復(fù)制文件
- name: Copy a file from a remote source
ansible.builtin.copy:
src: /path/to/remote/source/file
dest: /path/to/remote/destination/file
remote_src: yes
# 創(chuàng)建備份并復(fù)制文件
- name: Copy a file and create a backup if the file exists
ansible.builtin.copy:
src: /path/to/local/file
dest: /path/to/remote/file
backup: yes
# 強(qiáng)制覆蓋目標(biāo)文件
- name: Copy a file and force overwrite
ansible.builtin.copy:
src: /path/to/local/file
dest: /path/to/remote/file
force: yes