Ansible——unarchive模块

打印 上一主题 下一主题

主题 935|帖子 935|积分 2805

目次
参数总结
底子语法
常见的命令行示例
示例1:解压缩文件到指定目次
示例2:解压缩文件并设置权限
示例3:长途URL解压缩
示例4:逼迫覆盖现有文件
具体步骤和示例
示例5:只要文件解压后,如果存在相同文件则跳过
示例6:指定长途主机解压文件
Playbook示例
根本用法示例
示例1:从控制机复制并解压文件到长途主机
示例2:直接在目的主机上解压文件
示例3:解压缩 ZIP 文件
高级用法示例
示例4:设置解压后的文件权限
示例5:下载并解压长途 URL 文件
示例6:利用 creates 参数防止重复解压
示例7:传递解压命令的额外参数
示例8:多任务解压缩
综述示例


 
Ansible 的 unarchive 模块用于解压缩和提取文件。该模块支持多种压缩格式,如.tar,.tar.gz,.zip 等。unarchive 模块可以将压缩文件解压到指定的目的目次,非常方便地在长途主机上分发和安装包文件。
参数总结


  • src:

    • 描述:要解压缩的文件路径,可以是当地路径或长途 URL。
    • 类型:字符串
    • 必须:是

  • dest:

    • 描述:解压缩文件的目的路径。
    • 类型:字符串
    • 默认值:当前工作目次

  • remote_src:

    • 描述:如果为 yes,则将 src 参数指定的文件视为长途文件。如果为 no,则将其视为当地文件。
    • 类型:布尔值
    • 默认值:no

  • remote_src_dest:

    • 描述:如果为 yes,则将 dest 参数指定的路径视为长途路径。如果为 no,则将其视为当地路径。
    • 类型:布尔值
    • 默认值:no

  • extra_opts:

    • 描述:额外的解压缩选项,作为字符串传递。
    • 类型:字符串
    • 默认值:无

  • copy:

    • 描述:如果为 yes,则将文件复制到 dest 目次,而不是在原地解压缩。
    • 类型:布尔值
    • 默认值:no

  • creates:

    • 描述:如果指定路径存在,则不执行解压操作。
    • 类型:字符串
    • 默认值:无

  • extract:

    • 描述:指定要利用的解压缩命令。
    • 类型:字符串
    • 默认值:根据文件扩展名自动检测

 
 
底子语法

  1. ansible <hostname or group> -m unarchive -a "src=<source_archive_path> dest=<destination_directory_path> [optional_arguments]" [options]
复制代码

常见的命令行示例


示例1:解压缩文件到指定目次

  1. 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 选项用于以特权执行。

示例2:解压缩文件并设置权限

  1. 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。

示例3:长途URL解压缩

  1. 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 目次。

示例4:逼迫覆盖现有文件

  1. 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 目次,并逼迫覆盖现有文件。

具体步骤和示例


示例5:只要文件解压后,如果存在相同文件则跳过

  1. 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 目次,但是会保存比压缩包内更新的文件。

示例6:指定长途主机解压文件

  1. 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 目次。


Playbook示例


根本用法示例


示例1:从控制机复制并解压文件到长途主机

  1. ---
  2. - name: Unarchive from control machine to remote
  3.   hosts: all
  4.   tasks:
  5.     - name: Extract file to remote machine
  6.       unarchive:
  7.         src: /path/to/file.tar.gz
  8.         dest: /path/to/destination/
复制代码

示例2:直接在目的主机上解压文件

  1. ---
  2. - name: Unarchive from remote source
  3.   hosts: all
  4.   tasks:
  5.     - name: Extract file that is already on remote machine
  6.       unarchive:
  7.         src: /path/to/file.tar.gz
  8.         dest: /path/to/destination/
  9.         remote_src: yes
复制代码

示例3:解压缩 ZIP 文件

  1. ---
  2. - name: Unarchive a zip file
  3.   hosts: all
  4.   tasks:
  5.     - name: Extract zip file to remote machine
  6.       unarchive:
  7.         src: /path/to/file.zip
  8.         dest: /path/to/destination/
复制代码

高级用法示例


示例4:设置解压后的文件权限

  1. ---
  2. - name: Unarchive with custom file permissions
  3.   hosts: all
  4.   tasks:
  5.     - name: Extract file with specific permissions
  6.       unarchive:
  7.         src: /path/to/file.tar.gz
  8.         dest: /path/to/destination/
  9.         mode: '0755'
复制代码

示例5:下载并解压长途 URL 文件

  1. ---
  2. - name: Unarchive from a remote URL
  3.   hosts: all
  4.   tasks:
  5.     - name: Download and extract file from URL
  6.       unarchive:
  7.         src: http://example.com/file.tar.gz
  8.         dest: /path/to/destination/
复制代码

示例6:利用 creates 参数防止重复解压

  1. ---
  2. - name: Unarchive skipping if file already exists
  3.   hosts: all
  4.   tasks:
  5.     - name: Unarchive only if specific file does not exist
  6.       unarchive:
  7.         src: /path/to/file.tar.gz
  8.         dest: /path/to/destination/
  9.         creates: /path/to/destination/extracted_file
复制代码
示例7:传递解压命令的额外参数

  1. ---
  2. - name: Unarchive with extra options
  3.   hosts: all
  4.   tasks:
  5.     - name: Extract file with extra options
  6.       unarchive:
  7.         src: /path/to/file.tar.gz
  8.         dest: /path/to/destination/
  9.         extra_opts: ['--strip-components=1']
复制代码

示例8:多任务解压缩

  1. ---
  2. - name: Unarchive multiple files
  3.   hosts: all
  4.   tasks:
  5.     - name: Unarchive first file
  6.       unarchive:
  7.         src: /path/to/first_file.tar.gz
  8.         dest: /path/to/first_destination/
  9.     - name: Unarchive second file
  10.       unarchive:
  11.         src: /path/to/second_file.zip
  12.         dest: /path/to/second_destination/
复制代码

综述示例


全面展示各种参数的利用方法:
  1. ---
  2. - name: Comprehensive unarchive example
  3.   hosts: all
  4.   tasks:
  5.     - name: Unarchive file with various options
  6.       unarchive:
  7.         src: /path/to/file.tar.gz
  8.         dest: /path/to/destination/
  9.         copy: yes
  10.         mode: '0755'
  11.         creates: /path/to/destination/already_extracted_file
  12.         extra_opts: ['--strip-components=1']
  13.         remote_src: yes
  14.         keep_newer: yes
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

温锦文欧普厨电及净水器总代理

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表