Ansible內(nèi)置模塊之 unarchive
發(fā)布作者:微思網(wǎng)絡(luò) 發(fā)布時間:2024-12-19 瀏覽量:0次
ansible.builtin.unarchive 為ansible內(nèi)置模塊之一, 可以用于解壓縮文件到目標(biāo)路徑。它支持多種壓縮格式,包括 tar、zip 等。
選項 必須 類型 默認值 說明 src 是 str 無 源壓縮文件的路徑 dest 是 str 無 遠程受控主機解壓縮文件的目標(biāo)路徑 remote_src 否 bool no 如果為 yes,表示 src 是遠程路徑 creates 否 str 無 如果路徑存在,則跳過解壓縮操作 list_files 否 bool no 如果為 yes,列出壓縮包中的文件,而不是解壓縮 extra_opts 否 list 無 傳遞給解壓縮命令的額外選項 mode 否 str 無 解壓縮文件的權(quán)限,如 0644、0755 等 owner 否 str 無 解壓縮文件的所有者 group 否 str 無 解壓縮文件的屬組 注意: extra_opts: 額外選項. [type: list] · –transform 變形,可以理解為調(diào)用類似 sed 功能替換文本 · s/^xxx/yyy/ 將開頭的 xxx 文本替換為 yyy · –strip-components=1 消去壓縮文件中的第一級目錄 # 將 本地的foo.tgz 提取到受控主機/var/lib/foo 中
- name: Extract foo.tgz into /var/lib/foo
ansible.builtin.unarchive:
src: foo.tgz
dest: /var/lib/foo
# 解壓存檔已經(jīng)在遠程計算機上的文件
- name: Unarchive a file that is already on the remote machine
ansible.builtin.unarchive:
src: /tmp/foo.zip
dest: /usr/local/bin
remote_src: yes
# 列出壓縮包中的文件
- name: List files in an archive
ansible.builtin.unarchive:
src: /path/to/file.zip
dest: /path/to/destination/
list_files: yes
# 設(shè)置解壓縮文件的權(quán)限和所有者
- name: Unarchive a file and set permissions and owner
ansible.builtin.unarchive:
src: /path/to/file.zip
dest: /path/to/destination/
mode: '0755'
owner: myuser
group: mygroup
# 解壓縮需要下載的文件(在2.0中添加)
- name: Unarchive a file that needs to be downloaded (added in 2.0)
ansible.builtin.unarchive:
src: https://example.com/example.zip
dest: /usr/local/bin
remote_src: yes
# 使用額外選項解壓縮文件
- name: Unarchive a file with extra options
ansible.builtin.unarchive:
src: /tmp/foo.zip
dest: /usr/local/bin
extra_opts:
- --transform
- s/^xxx/yyy/
# 使用額外選項解壓縮文件2
- name: Unarchive a file with extra options
ansible.builtin.unarchive:
src: /path/to/file.tar.gz
dest: /path/to/destination/
extra_opts:
- --strip-components=1