如何将 Ansible Collection 中的角色标记为已弃用?

huangapple go评论94阅读模式
英文:

How to mark an Ansible Role in a Collection as deprecated?

问题

我在Ansible Galaxy上管理一个包含多个角色的Ansible集合,并希望将其中一些角色标记为弃用。我该如何操作?

在Ansible文档中,只提供了一个用于模块的解决方案。

这个方法能够适用于角色吗?

英文:

I manage an Ansible Collection on Ansible Galaxy with multiple roles in it and want to mark some of those roles as deprecated. How can I do this?

In the Ansible documentation there is only a solution for modules.

Can this be adopted to the roles?

答案1

得分: 2

以下是翻译好的部分:

没有我所知的这种框架。但是,你可以轻松地自己创建一个。使用配置参数DEPRECATION_WARNINGS。例如,声明默认值

shell> cat roles/foo/defaults/main/deprecation.yml
foo_deprecation_warnings: "{{ lookup('ansible.builtin.config', 'DEPRECATION_WARNINGS')}}"
foo_deprecation:
  removal_version: 2.0.0
  warning_text: 阅读发布说明以进行迁移

,创建一个任务来显示弃用警告

shell> cat roles/foo/tasks/deprecation.yml
- name: 弃用警告
  debug:
    msg: |
      [DEPRECATION WARNING]: 该角色将在版本 {{ foo_deprecation.removal_version }} 中从集合中移除。 {{ foo_deprecation.warning_text }}。
      可以通过在ansible.cfg中设置deprecation_warnings=False来禁用弃用警告。      

,并有条件地包含它

shell> cat roles/foo/tasks/main.yml
- include_tasks: deprecation.yml
  when:
    - foo_deprecation
    - foo_deprecation_warnings
  run_once: true

- debug:
    msg: 角色 foo 正在运行 ...

当你运行角色时

shell> cat pb.yml
- hosts: localhost
  roles:
    - foo

你将看到弃用警告

shell> ansible-playbook pb.yml

PLAY [localhost] ******************************************************************************

TASK [foo : include_tasks] ********************************************************************
included: /export/scratch/tmp7/test-428/roles/foo/tasks/deprecation.yml for localhost

TASK [foo : 弃用警告] ***********************************************************************
ok: [localhost] =>
  msg: |-
    [DEPRECATION WARNING]: 该角色将在版本 2.0.0 中从集合中移除。阅读发布说明以进行迁移。
    可以通过在ansible.cfg中设置deprecation_warnings=False来禁用弃用警告。    

TASK [foo : debug] ****************************************************************************
ok: [localhost] =>
  msg: 角色 foo 正在运行 ...

PLAY RECAP ************************************************************************************
localhost: ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

你可以禁用弃用警告

shell> ANSIBLE_DEPRECATION_WARNINGS=false ansible-playbook pb.yml

PLAY [localhost] ******************************************************************************

TASK [foo : include_tasks] ********************************************************************
skipping: [localhost]

TASK [foo : debug] ****************************************************************************
ok: [localhost] =>
  msg: 角色 foo 正在运行 ...

PLAY RECAP ************************************************************************************
localhost: ok=1    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

如果你不想让角色显示弃用警告,可以声明一个空字典

foo_deprecation: 

或者不包含任务 deprecation.yml

英文:

There is no such framework AFAIK. But, you can easily create one on your own. Use the configuration parameter DEPRECATION_WARNINGS. For example, declare defaults

shell> cat roles/foo/defaults/main/deprecation.yml
foo_deprecation_warnings: "{{ lookup('ansible.builtin.config', 'DEPRECATION_WARNINGS')}}"
foo_deprecation:
  removal_version: 2.0.0
  warning_text: Read the release notes on how to migrate

, create a task to display the deprecation warning

shell> cat roles/foo/tasks/deprecation.yml
- name: Deprecation warning
  debug:
    msg: |
      [DEPRECATION WARNING]: This role will be removed from the collection in release {{ foo_deprecation.removal_version }}. {{ foo_deprecation.warning_text }}.
      Deprecation warnings can be disabled by setting deprecation_warnings=False in
      ansible.cfg.      

, and include it conditionally

shell> cat roles/foo/tasks/main.yml
- include_tasks: deprecation.yml
  when:
    - foo_deprecation
    - foo_deprecation_warnings
  run_once: true

- debug:
    msg: Role foo is running ...

When you run the role

shell> cat pb.yml
- hosts: localhost
  roles:
    - foo

you'll see the deprecation warning

<sup>

shell&gt; ansible-playbook pb.yml

PLAY [localhost] ******************************************************************************

TASK [foo : include_tasks] ********************************************************************
included: /export/scratch/tmp7/test-428/roles/foo/tasks/deprecation.yml for localhost

TASK [foo : Deprecation warning] **************************************************************
ok: [localhost] =&gt; 
  msg: |-
    [DEPRECATION WARNING]: This role will be removed from the collection in release 2.0.0. Read the release notes on how to migrate.
    Deprecation warnings can be disabled by setting deprecation_warnings=False in
    ansible.cfg.    

TASK [foo : debug] ****************************************************************************
ok: [localhost] =&gt; 
  msg: Role foo is running ...

PLAY RECAP ************************************************************************************
localhost: ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

</sup>

<hr>

You can disable the deprecation warnings

<sup>

shell&gt; ANSIBLE_DEPRECATION_WARNINGS=false ansible-playbook pb.yml

PLAY [localhost] ******************************************************************************

TASK [foo : include_tasks] ********************************************************************
skipping: [localhost]

TASK [foo : debug] ****************************************************************************
ok: [localhost] =&gt; 
  msg: Role foo is running ...

PLAY RECAP ************************************************************************************
localhost: ok=1    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

</sup>

<hr>

Declare an empty dictionary if you don't want a role to display a deprecation warning

foo_deprecation: 

or don't include the task deprecation.yml.

答案2

得分: 0

对于提供内容的Ansible Galaxy,有一个可用的选项来Deprecate Content

您可以从“我的内容”页面将您拥有的Collections和Role存储库标记为弃用。...

只需按照文档操作。


由于Roles用于

...将您的内容分组为角色,您可以轻松重用它们并与其他用户共享。

并且只有源代码、脚本、YAML文件,没有针对内容本身的弃用警告消息。

要解决此问题,可以实现pre_tasks以及特定的消息和行为。例如,

  pre_tasks:

  - name: 检查 Ansible 用户
    fail:
      msg: 不要在 root 用户下执行!
    when: ansible_user == 'root'

结合条件,它可以用于使用自定义消息failassert表达式为true,或者从playbook中打印WARNING

由于没有模块可以直接打印WARNING消息,需要通过Filter Plugin来实现,它可以轻松进行开发,如给定链接中所示,或者作为自定义模块warn。需要注意的是,这种方法会为Role创建进一步的依赖关系,需要在解决之前处理。

对此的一种解决方法可以是:

  pre_tasks:

  - name: 弃用警告
    fail:
      msg: 使用v2代替!
    when: VERSION == 'v1'
    ignore_errors: true

其他问答

英文:

For Ansible Galaxy as provider of the content there is an option available to Deprecate Content

> You can deprecate Collections and Role repositories that you own from the ‘My Content’ page. ...

Just follow the documentation.


Since Roles are for

> ... group your content in roles, you can easily reuse them and share them with other users.

and just source code, scripts, YAML files, there is nothing like a deprecation warning message for the content itself.

To work around one could implement pre_tasks together with specific messages and behavior. In example like

  pre_tasks:

  - name: Check Ansible User
    fail:
      msg: Do not execute under root!
    when: ansible_user == &#39;root&#39;

Together with Conditionals it can be used to fail with Custom Message, assert that given expressions are true or in order to print WARNINGs from playbook.

Since there is no module to print WARNING messages out-of-box, it needs to be implemented via a Filter Plugin which can be easily Developed and as shown within the given link or as an own Custom Module warn. One may take note that such approach will create further dependencies for a Role and which would need to be resolved before too.

A workaround for that could be simply

  pre_tasks:

  - name: DEPRECATION WARNING
    fail:
      msg: Use v2 instead!
    when: VERSION == &#39;v1&#39;
    ignore_errors: true

Other Q&A

huangapple
  • 本文由 发表于 2023年7月18日 00:42:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706529.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定