Ansible 理解剧本中角色的可解释性标记

Ansible 理解剧本中角色的可解释性标记,ansible,Ansible,困惑于剧本中的角色标记是否正常工作,如果是,其背后的理念 剧本 - 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" t

困惑于剧本中的角色标记是否正常工作,如果是,其背后的理念

剧本

- 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
当传递不匹配的标记时,角色仍在运行,但(不匹配)标记将传递给任务,并且仅执行带有该标记(或标记为“始终”)的任务

不匹配标记:

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
这是预期的行为吗?
还有,有没有一种方法可以在剧本中标记一个角色(以及一个任务-包含_角色),只根据执行时传入的标记过滤该角色本身是否执行

这是预期的行为吗

是的

还有,有没有一种方法可以在剧本中标记一个角色(以及一个任务-包含_角色),只根据执行时传入的标记过滤该角色本身是否执行

自Ansible 2.4版以来,有三种使用角色的方式:

  • ,
  • ,及
使用
import\u role
roles
声明,Ansible连接声明中指定的标记并执行任务

include_role
像一个独立的任务一样工作,即它自己观察标记


注释

-同样的规则适用于
条件时的

-在Ansible 2.4之前,
include_role
的工作方式类似于
import_role
now


-参考

好的,谢谢techraf。因此,我想,试图根据标记筛选出“角色”存在一些固有的危险,因为您可能会遇到一些意外的任务。不管怎样,看来我需要找到另一种方法来解决这个问题。我需要我在剧本中的角色循环遍历变量列表。“角色”和“导入角色”都不允许循环,标记“包含角色”也要求标记所有基础任务。您可以通过
--list tasks
参数轻松验证确切的标记。