Ansible——unarchive模块

目录

参数总结

基础语法

常见的命令行示例

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

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

示例3:远程URL解压缩

示例4:强制覆盖现有文件

具体步骤和示例

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

示例6:指定远程主机解压文件

Playbook示例

基本用法示例

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

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

[示例3:解压缩 ZIP 文件](#示例3:解压缩 ZIP 文件)

高级用法示例

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

[示例5:下载并解压远程 URL 文件](#示例5:下载并解压远程 URL 文件)

[示例6:使用 creates 参数防止重复解压](#示例6:使用 creates 参数防止重复解压)

示例7:传递解压命令的额外参数

示例8:多任务解压缩

综述示例


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

参数总结

  1. src:

    • 描述:要解压缩的文件路径,可以是本地路径或远程 URL。
    • 类型:字符串
    • 必需:是
  2. dest:

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

    • 描述:如果为 yes,则将 src 参数指定的文件视为远程文件。如果为 no,则将其视为本地文件。
    • 类型:布尔值
    • 默认值:no
  4. remote_src_dest:

    • 描述:如果为 yes,则将 dest 参数指定的路径视为远程路径。如果为 no,则将其视为本地路径。
    • 类型:布尔值
    • 默认值:no
  5. extra_opts:

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

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

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

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

基础语法

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

常见的命令行示例

示例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:解压缩文件并设置权限
复制代码
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解压缩
复制代码
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:强制覆盖现有文件
复制代码
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:只要文件解压后,如果存在相同文件则跳过
复制代码
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:指定远程主机解压文件
复制代码
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:从控制机复制并解压文件到远程主机
复制代码
---
- 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/
示例2:直接在目标主机上解压文件
复制代码
---
- 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
示例3:解压缩 ZIP 文件
复制代码
---
- 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/
高级用法示例
示例4:设置解压后的文件权限
复制代码
---
- 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 文件
复制代码
---
- 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/
示例6:使用 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
示例7:传递解压命令的额外参数
复制代码
---
- 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']
示例8:多任务解压缩
复制代码
---
- 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
相关推荐
孙克旭_6 小时前
PXE_Kickstart_无人值守自动化安装系统
linux·运维·自动化
皓月盈江7 小时前
Linux电脑本机使用小皮面板集成环境开发调试WEB项目
linux·php·web开发·phpstudy·小皮面板·集成环境·www.xp.cn
深井冰水7 小时前
mac M2能安装的虚拟机和linux系统系统
linux·macos
leoufung8 小时前
内核内存锁定机制与用户空间内存锁定的交互分析
linux·kernel
忧虑的乌龟蛋9 小时前
嵌入式Linux I2C驱动开发详解
linux·驱动开发·嵌入式·iic·i2c·读数据·写数据
I_Scholar10 小时前
OPENSSL-1.1.1的使用及注意事项
linux·ssl
Johny_Zhao10 小时前
K8S+nginx+MYSQL+TOMCAT高可用架构企业自建网站
linux·网络·mysql·nginx·网络安全·信息安全·tomcat·云计算·shell·yum源·系统运维·itsm
稳联技术10 小时前
Ethercat转Profinet网关如何用“协议翻译术“打通自动化产线任督二脉
linux·服务器·网络
烟雨迷11 小时前
Linux环境基础开发工具的使用(yum、vim、gcc、g++、gdb、make/Makefile)
linux·服务器·学习·编辑器·vim
Bruk.Liu11 小时前
Linux 上安装RabbitMQ
linux·服务器·rabbitmq