我的目标是找到一种方法来结合下面的两个陈述,但我还不够。对于本例,无论 rhel 版本如何,我都会安装 Package1 和 Package2。但是,根据操作系统版本,Package3 将是不同的包。看起来多余,应该有一种方法可以结合起来,但这是最好的方法吗?绝对欢迎任何反馈。
- name: Install Packages
yum:
name: "{{ item }}"
state: latest
loop:
- package1
- package2
- package3A (Only for RHEL8 versions)
when: "{{ ansible_distribution_version }} >= 8.0"
- name: Install Packages
yum:
name: "{{ item }}"
state: latest
loop:
- package1
- package2
- package3B (Only for RHEL7 versions)
when: "{{ ansible_distribution_version }} < 8.0"
答案1
- 声明变量
packages:
- package1
- package2
- "{{ package3 }}"
package3: >-
{%- if ansible_distribution_version is version('8.0', '>=') -%}
package3A
{%- elif ansible_distribution_version is version('8.0', '<') -%}
package3B
{%- endif -%}
b) 将选项放入字典并使用变量ansible_distribution_major_version
packages:
- package1
- package2
- "{{ package3[ansible_distribution_major_version|int]|
default(package3.default) }}"
package3:
default: package3A
7: package3B
6: package3B
5: package3B
- 安装列表中的软件包
- name: Install Packages
yum:
name: "{{ item }}"
state: latest
loop: "{{ packages }}"