我有一个简单的 Ansible 角色,它创建了一个 Foreman 激活密钥,但是它只“订阅”最后一个值,而不是两者?
# 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 文件包含:
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
我正在尝试一次性添加“ subscription: ['CentOS-7','CentOS-7-EPEL'] ”,就好像我使用“with_subelements”循环一样,只添加了最后一项“CentOS-7-EPEL”或“CentOS-8-EPEL”(而不是两者)。
有人能建议一种方法来改变我的变量文件或播放以使 katello_activation_key 在一次传递中添加两个变量吗?
答案1
首先,字典列表act_key
包含一个键subscription
,并且该值是一个列表。因此您不需要遍历多个列表
- 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
) - 因此您可以使用字典或者items2dict创建名称列表的映射/字典。但act_key
为了便于阅读,我更愿意更改订阅中包含的相同格式,然后才是“katello_activation_key”需要的格式。