首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在一次传递中填充变量

如何在一次传递中填充变量
EN

Server Fault用户
提问于 2021-02-15 06:11:36
回答 1查看 45关注 0票数 0

我有一个简单的Ansible角色,它创建一个Foreman激活键,但是它只具有最后一个值的“订阅”,而不是两者都有?

代码语言:javascript
运行
复制
# cat roles/hammer/tasks/subscription.yml
---

- name: Add Subscription key ID to Activation Key
  katello_activation_key:
    username: "{{ hammer.username|default('NotSet') }}"
    password: "{{ hammer.password|default('NotSet') }}"
    server_url: "https://{{ system_host_name }}"
    name: "{{ item.0.name }}"
    organization: "{{ hammer.organisation.name }}"
    lifecycle_environment: "{{ item.0.lifecycle }}"
    content_view: '{{ item.0.cview }}'
    subscriptions:
      - name: "{{ item.1 }}"
  #   - name: "{{ item.1.name2 }}"
    auto_attach: False
    release_version: Initial
  tags: hammer
...

我的vars文件包含:

代码语言:javascript
运行
复制
act_key:
  - name: CentOS 7 content Development Key
    desc: CentOS 7 content Development Key
    release: Initial
    cview: CentOS 7 content
    lifecycle: Development
  #  subscription: ['CentOS-7','CentOS-7-EPEL']
    subscription:
      - CentOS-7
      - CentOS-7-EPEL

  - name: CentOS 7 content Production Key
    desc: CentOS 7 content Production Key
    cview: CentOS 7 content
    release: Initial
    lifecycle: Production
  #  subscription: ['CentOS-7','CentOS-7-EPEL']
    subscription:
      - CentOS-7
      - CentOS-7-EPEL

  - name: CentOS 8 content Development Key
    desc: CentOS 8 content Development Key
    cview: CentOS 8 content
    release: Initial
    lifecycle: Development
  #  subscription: ['CentOS-8','CentOS-8-EPEL']
    subscription:
      - CentOS-8
      - CentOS-8-EPEL

  - name: CentOS 8 content Production Key
    desc: CentOS 8 content Production Key
    cview: CentOS 8 content  
    release: Initial
    lifecycle: Production
  #  subscription: ['CentOS-8','CentOS-8-EPEL']
    subscription:
      - CentOS-8
      - CentOS-8-EPEL

我试图在一次传递中添加““CentOS-7”、“CentOS-7-EPEL”:with_subelements”,就像我使用"with_subelements“循环一样--只添加最后一项”CentOS7-Epel“或"CentOS-8-EPEL”(而不是两者兼而有之)。

有人能建议改变我的变量文件或播放使katello_activation_key在一次传递中添加两个变量的方法吗?

EN

回答 1

Server Fault用户

发布于 2021-02-15 14:52:55

首先,act_key中的字典列表包含一个键subscription,该值是一个列表。所以你不需要在多个列表上迭代

代码语言:javascript
运行
复制
- name: Add Subscription key ID to Activation Key
  katello_activation_key:
    ..
    name: "{{ item.name }}"
    organization: "{{ hammer.organisation.name }}"
    lifecycle_environment: "{{ item.lifecycle }}"
    content_view: '{{ item.cview }}'
    subscriptions: "{{ item.subscription }}"
    ...
  loop: "{{ act_key }}"

第二,katello_activation_key.subscriptions不允许字符串列表(但也不允许单键name的dict列表),因此您可以使用字典items2dict创建名称列表的映射/dict。但是,为了更好地阅读,我更倾向于更改act_key,以便在订阅中包含相同的格式,这样´katello_activation_key`就需要。

票数 0
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/1053667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档