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

如何在Ansible中将循环项或变量作为模块的参数键?

实现AWX配置循环导出的正确方式

你原来的写法无法生效,因为awx.awx.export模块并不支持动态指定如projects: []这类参数名,它的标准参数是resources,用于接收要导出的资源类型列表。以下是两种可行的实现方案:

方案1:循环逐个导出单一资源类型

如果需要为每种资源单独执行导出任务,可按以下写法实现:

- name: Export {{ item }} from AWX
  awx.awx.export:
    resources:
      - "{{ item }}"
    controller_host: "{{ awx_host }}"
    controller_username: "{{ awx_username }}"
    controller_password: "{{ awx_password }}"
    # 可选:为每种资源生成独立的导出文件
    dest: "./awx_export_{{ item }}.tar.gz"
  loop:
    - projects
    - inventory
    - applications
    - credentials

方案2:一次性导出所有指定资源类型

如果不需要拆分导出,想一次性打包所有目标资源,直接将资源列表传入resources参数即可,无需循环:

- name: Export all specified resources from AWX
  awx.awx.export:
    resources:
      - projects
      - inventory
      - applications
      - credentials
    controller_host: "{{ awx_host }}"
    controller_username: "{{ awx_username }}"
    controller_password: "{{ awx_password }}"
    dest: "./awx_full_export.tar.gz"

关键说明

  • resources是awx.awx.export模块的官方指定参数,支持所有AWX可导出的资源类型。
  • 若需自定义导出文件路径,添加dest参数即可;循环场景下可通过{{ item }}区分不同资源的导出文件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 23:28:10