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

如何用Ansible将字典内列表转为字典列表并循环创建复合内容视图

解决方案:用Ansible创建/更新Satellite复合内容视图

直接用Ansible循环+过滤器就能实现需求,以下是完整的Playbook示例:

- name: 管理Satellite复合内容视图
  hosts: localhost
  gather_facts: false
  vars:
    composite_content_views:
      - name: 'ccv-number1'
        content_views:
          - 'cv-number1'
          - 'cv-number2'
      - name: 'ccv-number2'
        content_views:
          - 'cv-number3'
          - 'cv-number4'
          - 'cv-number5'
    satellite_server: "your-satellite.example.com"
    satellite_user: "admin"
    satellite_password: "your-password"
    satellite_organization: "your-org"

  tasks:
    - name: 创建或更新复合内容视图
      redhat.satellite.content_view:
        server_url: "{{ satellite_server }}"
        username: "{{ satellite_user }}"
        password: "{{ satellite_password }}"
        organization: "{{ satellite_organization }}"
        name: "{{ item.name }}"
        type: composite
        composite: "{{ item.content_views | map('community.general.dict_kv', 'content_view') | map('combine', {'latest': true}) | list }}"
        state: present
      loop: "{{ composite_content_views }}"

关键细节说明:

  • 遍历条目:通过loop: "{{ composite_content_views }}"逐个处理每个复合内容视图配置
  • 格式转换:用两层map过滤器完成列表结构转换:
    1. map('community.general.dict_kv', 'content_view'):把单个字符串格式的内容视图名称,转为{content_view: "cv-name"}的字典结构
    2. map('combine', {'latest': true}):给每个字典添加latest: true的键值对
    3. 最后用list将过滤器输出转为标准列表,匹配模块composite参数的格式要求
  • 幂等性保障:模块本身支持幂等逻辑,当state: present时,会自动对比Satellite现有配置,仅在内容视图组成发生变化时执行更新操作

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 13:01:19