You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ansible:如何为archive模块添加tar的额外参数?

解决Ansible archive模块传递tar额外参数的问题

方法1:使用社区版community.general.archive模块

Ansible官方archive模块未提供类似unarchive的extra_opts参数,但社区维护的community.general.archive模块支持该参数,可直接传递-u/--update和--owner选项。

操作步骤:

  1. 安装community.general集合(首次使用需执行):
ansible-galaxy collection install community.general
  1. Playbook任务示例:
- name: 向现有归档添加文件并指定所有者
  community.general.archive:
    path: /path/to/target/files
    dest: /path/to/existing/archive.tar
    format: tar
    extra_opts:
      - "--update"
      - "--owner=your-desired-owner"  # 替换为实际需要的所有者名称/ID

该方法完全遵循Ansible正统用法,不会触发shell模块的警告。

方法2:规范shell任务实现幂等性(规避警告)

若无法安装社区集合,可通过让shell任务满足幂等性要求来减少警告(Ansible的警告多因任务不符合幂等设计):

- name: 获取现有归档的修改时间
  stat:
    path: /path/to/existing/archive.tar
  register: archive_info

- name: 获取待添加文件的最新修改时间
  find:
    paths: /path/to/target/files
    recurse: yes
  register: target_files

- name: 更新归档并设置所有者
  shell: tar --update --owner=your-desired-owner -rf /path/to/existing/archive.tar /path/to/target/files
  when: target_files.files | map(attribute='mtime') | max > archive_info.stat.mtime

通过对比文件与归档的修改时间,确保仅在需要更新时执行tar命令,符合Ansible的幂等性原则,从而避免不必要的警告。

内容的提问来源于stack exchange,提问作者3nigma_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 18:42:51