赞
踩
目录
Ansible 的 unarchive
模块用于解压缩和提取文件。该模块支持多种压缩格式,如.tar,
.tar.gz,
.zip
等。unarchive
模块可以将压缩文件解压到指定的目标目录,非常方便地在远程主机上分发和安装包文件。
src
:
dest
:
remote_src
:
yes
,则将 src
参数指定的文件视为远程文件。如果为 no
,则将其视为本地文件。no
remote_src_dest
:
yes
,则将 dest
参数指定的路径视为远程路径。如果为 no
,则将其视为本地路径。no
extra_opts
:
copy
:
yes
,则将文件复制到 dest
目录,而不是在原地解压缩。no
creates
:
extract
:
ansible <hostname or group> -m unarchive -a "src=<source_archive_path> dest=<destination_directory_path> [optional_arguments]" [options]
ansible all -m unarchive -a "src=/path/to/archive.tar.gz dest=/path/to/destination" --become
此命令会将 /path/to/archive.tar.gz
解压到 /path/to/destination
目录。--become
选项用于以特权执行。
ansible all -m unarchive -a "src=/path/to/archive.zip dest=/path/to/destination mode=0644" --become
此命令会将 /path/to/archive.zip
解压到 /path/to/destination
目录,并将解压后的文件权限设置为 0644
。
ansible all -m unarchive -a "src=http://example.com/archive.tar.gz dest=/path/to/destination" --become
此命令会从 http://example.com/archive.tar.gz
下载压缩包并解压到 /path/to/destination
目录。
ansible all -m unarchive -a "src=/path/to/archive.tar.gz dest=/path/to/destination extra_opts=--overwrite" --become
此命令会将 /path/to/archive.tar.gz
解压到 /path/to/destination
目录,并强制覆盖现有文件。
ansible all -m unarchive -a "src=/path/to/archive.tar.gz dest=/path/to/destination keep_newer=yes" --become
此命令会将 /path/to/archive.tar.gz
解压到 /path/to/destination
目录,但是会保留比压缩包内更新的文件。
ansible target_host -m unarchive -a "src=/path/to/archive.tar.gz dest=/path/to/destination" --become
此命令会在 target_host
主机上,将 /path/to/archive.tar.gz
解压到 /path/to/destination
目录。
- ---
- - name: Unarchive from control machine to remote
- hosts: all
- tasks:
- - name: Extract file to remote machine
- unarchive:
- src: /path/to/file.tar.gz
- dest: /path/to/destination/
- ---
- - name: Unarchive from remote source
- hosts: all
- tasks:
- - name: Extract file that is already on remote machine
- unarchive:
- src: /path/to/file.tar.gz
- dest: /path/to/destination/
- remote_src: yes
- ---
- - name: Unarchive a zip file
- hosts: all
- tasks:
- - name: Extract zip file to remote machine
- unarchive:
- src: /path/to/file.zip
- dest: /path/to/destination/
- ---
- - name: Unarchive with custom file permissions
- hosts: all
- tasks:
- - name: Extract file with specific permissions
- unarchive:
- src: /path/to/file.tar.gz
- dest: /path/to/destination/
- mode: '0755'
- ---
- - name: Unarchive from a remote URL
- hosts: all
- tasks:
- - name: Download and extract file from URL
- unarchive:
- src: http://example.com/file.tar.gz
- dest: /path/to/destination/
creates
参数防止重复解压- ---
- - name: Unarchive skipping if file already exists
- hosts: all
- tasks:
- - name: Unarchive only if specific file does not exist
- unarchive:
- src: /path/to/file.tar.gz
- dest: /path/to/destination/
- creates: /path/to/destination/extracted_file
- ---
- - name: Unarchive with extra options
- hosts: all
- tasks:
- - name: Extract file with extra options
- unarchive:
- src: /path/to/file.tar.gz
- dest: /path/to/destination/
- extra_opts: ['--strip-components=1']
- ---
- - name: Unarchive multiple files
- hosts: all
- tasks:
- - name: Unarchive first file
- unarchive:
- src: /path/to/first_file.tar.gz
- dest: /path/to/first_destination/
-
- - name: Unarchive second file
- unarchive:
- src: /path/to/second_file.zip
- dest: /path/to/second_destination/
全面展示各种参数的使用方法:
- ---
- - name: Comprehensive unarchive example
- hosts: all
- tasks:
- - name: Unarchive file with various options
- unarchive:
- src: /path/to/file.tar.gz
- dest: /path/to/destination/
- copy: yes
- mode: '0755'
- creates: /path/to/destination/already_extracted_file
- extra_opts: ['--strip-components=1']
- remote_src: yes
- keep_newer: yes
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。