gpt4 book ai didi

ansible - 了解剧本中角色的可靠标记

转载 作者:行者123 更新时间:2023-12-04 01:55:18 25 4
gpt4 key购买 nike

对剧本中的角色标记是否正常工作感到困惑,如果是,那么背后的哲学。

剧本

- hosts: Test-c7-1
roles:
- role: test.tag
tags: tag2

角色/任务
---
- debug:
msg: "task - tag1 set"
tags: tag1

- debug:
msg: "task - tag2 set"
tags: tag2

- debug:
msg: "task - always set"
tags: always

- debug:
msg: "task - NO TAG"

当传入匹配的标签时,角色就会运行,任务列表中的所有内容都会执行,而不管标签如何。

匹配标签:
ansible-playbook playbook/tagtester.yml --tags "tag2"

PLAY [Test-c7-1] ********************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************************
ok: [Test-c7-1]

TASK [test.tag : debug] *************************************************************************************************************************************************************
ok: [Test-c7-1] => {
"msg": "task - tag1 set"
}

TASK [test.tag : debug] *************************************************************************************************************************************************************
ok: [Test-c7-1] => {
"msg": "task - tag2 set"
}

TASK [test.tag : debug] *************************************************************************************************************************************************************
ok: [Test-c7-1] => {
"msg": "task - always set"
}

TASK [test.tag : debug] *************************************************************************************************************************************************************
ok: [Test-c7-1] => {
"msg": "task - NO TAG"
}

PLAY RECAP **************************************************************************************************************************************************************************
Test-c7-1 : ok=5 changed=0 unreachable=0 failed=0

当传递不匹配的标签时,角色仍然运行,但(不匹配的)标签被传递给任务,并且只有带有该标签(或标记为“总是”)的任务才会被执行。

不匹配的标签:
 ansible-playbook playbook/tagtester.yml --tags "tag1"

PLAY [Test-c7-1] ********************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************************
ok: [Test-c7-1]

TASK [test.tag : debug] *************************************************************************************************************************************************************
ok: [Test-c7-1] => {
"msg": "task - tag1 set"
}

TASK [test.tag : debug] *************************************************************************************************************************************************************
ok: [Test-c7-1] => {
"msg": "task - always set"
}

PLAY RECAP **************************************************************************************************************************************************************************
Test-c7-1 : ok=3 changed=0 unreachable=0 failed=0

这是预期的行为吗?
并且,有没有办法在剧本中标记角色(以及任务 - include_role)以仅根据执行时传入的标记过滤该角色本身是否被执行?

最佳答案

Is this expected behavior?



是的。

And, is there a way to tag a role (as well as a task - include_role) in a playbook to only filter whether that role itself is executed or not based on that tag passed in at execution?



从 Ansible 2.4 版本开始,有三种使用角色的方式:
  • roles declaration at play level ,
  • import_role module , 和
  • include_role module .

  • import_roleroles声明,Ansible 连接声明中指定的标签并执行任务。
    include_role像一个独立的任务一样工作,即它本身观察标签。

    备注

    - 同样的规则适用于 when状况。

    - Ansible 2.4 之前 include_roleimport_role 一样工作现在。

    - 引用 Differences Between Static and Dynamic

    关于ansible - 了解剧本中角色的可靠标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50976217/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com