以 Ansible 角色管理不受支持的发行版的最佳方式是什么?



一个 Ansible 角色支持 Debian Stretch 和 Buster。

它无法在 Jessie 或旧版本上完成这项工作。

告诉用户该角色不能在给定的旧版本上使用的最佳方式是什么?

  • main.yml文件中不执行任何操作(使用when:声明控制发行版版本(
  • 让角色使用失败模块显式失败
  • 不要检查支持的发行版版本,让任务自行失败

开发人员应将支持/测试的版本放在自述文件中。然后,用户应始终阅读自述文件。然后,应该使用常识。 但我们都知道事实并非如此。

您可以配置太旧的主机跳到该角色,以确保主机不会为该角色执行任何命令。但要走的方法是构建另一个角色,或更新该角色,让该剧本支持该操作系统版本。

此方法是最不需要的:Do not check for a supported distro version and let tasks fail themselves。因为当您沿着这条路走下去时,一些不受支持的任务会在主机上执行,然后您无法再保证系统的状态。总之;你会制造一团糟。

为了防止噩梦,确实,让游戏失败:

- name: fail when using older version
fail:
msg: "You fail because reason, woohoo"
when: ansible_distribution is Ubuntu and ansible_distribution_version is 10.04

问:"以Ansible 角色管理不受支持的发行版的最佳方法是什么?

答:当平台和版本不受支持时,最好结束主机或播放。在大多数情况下,这意味着这样的平台和版本尚未经过测试。由用户向元数据添加新的平台和版本,对其进行测试并选择性地为开发做出贡献。

在角色中,可以从角色的文件meta/main.yml读取变量galaxy_info,并测试支持的平台和版本。

$ cat roles/role_1/meta/main.yml
galaxy_info:
author: your name
description: your role description
company: your company (optional)
license: license (GPL-2.0-or-later, MIT, etc)
min_ansible_version: 2.9
platforms:
- name: Ubuntu
versions:
- bionic
- cosmic
- disco
- eoan
galaxy_tags: []
dependencies: []

例如,以下角色中的任务

$ cat roles/role_1/tasks/main.yml
---
- name: Print OS and distro Ansible variables collected by setup
debug:
msg:
- "ansible_os_family: {{ ansible_os_family }}"
- "ansible_distribution: {{ ansible_distribution }}"
- "ansible_distribution_major_version: {{ ansible_distribution_major_version }}"
- "ansible_distribution_version: {{ ansible_distribution_version }}"
- "ansible_distribution_release: {{ ansible_distribution_release }}"
- name: Include roles' meta data
include_vars:
file: "{{ role_path }}/meta/main.yml"
- name: Test the distribution is supported. End the host if not.
set_fact:
supported_distributions: "{{ galaxy_info.platforms|json_query('[].name') }}"
- debug:
var: supported_distributions
- block:
- debug:
msg: "{{ ansible_distribution }} not supported. End of host."
- meta: end_host
when: ansible_distribution not in supported_distributions
- name: Test the release is supported. End the host if not.
set_fact:
supported_releases: "{{ (galaxy_info.platforms|
selectattr('name', 'match', ansible_distribution)|
list|first).versions }}"
- debug:
var: supported_releases
- block:
- debug:
msg: "{{ ansible_distribution_release}} not supported. End of host."
- meta: end_host
when: ansible_distribution_release not in supported_releases
- name: The distribution and release is supported. Continue play.
debug:
msg: "{{ ansible_distribution }} {{ ansible_distribution_release }} is supported. Continue play."

与剧本

- hosts: localhost
gather_facts: true
roles:
- role_1

"msg": [
"ansible_os_family: Debian", 
"ansible_distribution: Ubuntu", 
"ansible_distribution_major_version: 19", 
"ansible_distribution_version: 19.04", 
"ansible_distribution_release: disco"
]
"supported_distributions": [
"Ubuntu"
]
"supported_releases": [
"bionic", 
"cosmic", 
"disco", 
"eoan"
]
"msg": "Ubuntu disco is supported. Continue play."

最新更新