示例4:强制覆盖现有文件
发布时间:2025-06-24 18:42:57 作者:北方职教升学中心 阅读量:031
,复制文件。
常见的命令行示例。请跳过。---- 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'
示例5:下载并解压远程 URL 文件。--become。
---- 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'
示例4:强制覆盖现有文件。
dest。
/path/to/destination。extra_opts。
remote_src_dest。
示例2:解决压缩文件并设置权限。
示例3:远程URL解压缩。
示例6:指定远程主机解压文件。请跳过。- 描述:要解压缩的文件路径,本地路径或远程路径可以是本地路径 URL。
示例4:强制覆盖现有文件。
---- 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。 下载压缩包并解压至 。此命令会将 。 no。该模块支持多种压缩格式如。
/path/to/destination。
:。
:。
ansible target_host -m unarchive -a "src=/path/to/archive.tar.gz dest=/path/to/destination" --become。
no。
---- 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/。 no。
:。常见的命令行示例。---- 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 control machine to remote hosts: all tasks: - name: Extract file to remote machine unarchive: src: /path/to/file.tar.gz dest: /path/to/destination/。
remote_src。
dest。
高级用法示例
示例4:解压后设置文件权限。
主机上,将 。
示例3༚远程URL解压缩。
目录。 src。
此命令会在 。
Ansible 的 。
示例1:将压缩文件解决到指定目录。 dest。
creates。
extract。
no。
/path/to/destination。高级用法示例。0644。 等。- 描述:如果有指定的路径,不进行解压操作。
- 默认值:根据文件扩展名自动检测。
示例3༚解压缩 ZIP 文件。---- 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/。
参数指定的路径视为远程路径。 目录,但比压缩包更新的文件将被保留。
具体步骤和示例。
ansible <hostname or group> -m unarchive -a "src=<source_archive_path> dest=<destination_directory_path> [optional_arguments]" [options]。示例5:文件解压后,如果有相同的文件,
,则将。
,将其视为本地文件。,然后将其视为本地路径。目录,而不是原地解压缩。
copy。
/path/to/archive.tar.gz。/path/to/destination。/path/to/archive.tar.gz。
目录。
ansible all -m unarchive -a "src=/path/to/archive.tar.gz dest=/path/to/destination extra_opts=--overwrite" --become。此命令会将 。
:。/path/to/destination。
creates。
解压到 。
ansible all -m unarchive -a "src=/path/to/archive.tar.gz dest=/path/to/destination keep_newer=yes" --become。
基本用法示例。/path/to/archive.tar.gz。 解压到 。
---- 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。参数总结。:。.tar,
.tar.gz,
.zip。示例1:从控制器复制和解压文件到远程主机。
Playbook示例。unarchive。
target_host。
- 描述:额外的解压选项,作为字符串传输。
综述示例。
综述示例。
参数总结。:。.tar,
.tar.gz,
.zip。示例1:从控制器复制和解压文件到远程主机。
Playbook示例。unarchive。
target_host。
- 描述:额外的解压选项,作为字符串传输。
综述示例。
综述示例。
示例1:从控制器复制和解压文件到远程主机。
Playbook示例。unarchive。
target_host。
- 描述:额外的解压选项,作为字符串传输。
综述示例。
示例4:解压后设置文件权限。
。
示例2:在目标主机上直接解压文件。:。 示例8:多任务解压缩。 示例8༚多任务解压缩。 ansible all -m unarchive -a "src=/path/to/archive.tar.gz dest=/path/to/destination" --become。 。 解压到 。
示例6:使用 。
示例5:文件解压后,如果有相同的文件,
示例7༚额外参数传递解压命令。
http://example.com/archive.tar.gz。 解压到 。
此命令会从 。
,则将。src。
示例6:指定远程主机解压文件。
参数总结。:。
Playbook示例。
unarchive。
。
no。示例2:在目标主机上直接解压文件。- 描述:解压缩文件的目标路径。
---- 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/。示例5:下载并解压远程 URL 文件。
- 描述:如果为。
示例6:使用 creates 防止重复解压。此命令会将 。
示例3༚解压缩 ZIP 文件。
示例6:使用 。。
---- 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']。 目录,并强制覆盖现有文件。 防止重复解压。高级用法示例。
模块可以将压缩文件解压到指定的目标目录,在远程主机上分发和安装包装文件非常方便。
/path/to/archive.tar.gz。参数指定的文件视为远程文件。:。常见的命令行示例。此命令会将 。
具体步骤和示例。
示例7༚额外参数传递解压命令。 yes。如果为。
/path/to/destination。
示例1:从控制器复制和解压文件到远程主机。 用于执行特权的选项。
ansible all -m unarchive -a "src=http://example.com/archive.tar.gz dest=/path/to/destination" --become。
基础语法。示例1:将压缩文件解决到指定目录。基本用法示例。 解压到 。
- 描述:如果为。
用于解压缩和提取文件的模块。 目录。示例2:解决压缩文件并设置权限。
示例1:将压缩文件解决到指定目录。基本用法示例。 解压到 。
- 描述:如果为。
用于解压缩和提取文件的模块。 目录。示例2:解决压缩文件并设置权限。
基础语法。
ansible all -m unarchive -a "src=/path/to/archive.zip dest=/path/to/destination mode=0644" --become。使用各种参数的方法:---- 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。
目录。- 描述:指定要使用的减压命令。
yes。 目录,并将解压后的文件权限设置为 。 yes。
yes。 目录,并将解压后的文件权限设置为 。 yes。