Ansible内置模块之 unarchive
发布作者:微思网络 发布时间:2024-12-19 浏览量:0次
ansible.builtin.unarchive 为ansible内置模块之一, 可以用于解压缩文件到目标路径。它支持多种压缩格式,包括 tar、zip 等。
选项 必须 类型 默认值 说明 src 是 str 无 源压缩文件的路径 dest 是 str 无 远程受控主机解压缩文件的目标路径 remote_src 否 bool no 如果为 yes,表示 src 是远程路径 creates 否 str 无 如果路径存在,则跳过解压缩操作 list_files 否 bool no 如果为 yes,列出压缩包中的文件,而不是解压缩 extra_opts 否 list 无 传递给解压缩命令的额外选项 mode 否 str 无 解压缩文件的权限,如 0644、0755 等 owner 否 str 无 解压缩文件的所有者 group 否 str 无 解压缩文件的属组 注意: extra_opts: 额外选项. [type: list] · –transform 变形,可以理解为调用类似 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
# 解压存档已经在远程计算机上的文件
- 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
# 设置解压缩文件的权限和所有者
- 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