Ansible中的tags

646次阅读
没有评论

共计 7970 个字符,预计需要花费 20 分钟才能阅读完成。

Ansible中的tags

ansible中可以给task或playbook打tags,从而实现运行playbook中指定tag的task

Ansible中的tags

ansible中tag有以下几种定义:

  • always:该任务会一直被执行,除非用–skip-tags指定了该标签
  • never:除非–tags指定了该标签,否则都不会执行。–tags never 执行标记always和never的任务
  • 自定义tag

–tags/–skip-tags指令参数:

  • always/never/自定义tag
  • tagged:执行所有有标签的task,但不包括带never的task
  • untagged:执行所有没有标签的task和带always的task

看一个样例

[root@manager test-tags]# cat debug-tags.yaml
---
- name: test tags
  hosts: all
  tasks:
  - name: test untagged
    debug:
      msg: "no tag"

  - name: test tag always
    debug:
      msg: "always tag"
    tags:
    - always

  - name: test tag never
    debug:
      msg: "never tag"
    tags:
    - never

  - name: test tag customer
    debug:
      msg: "tagged tag"
    tags:
    - test-tagged

不指定–tags和–skip-tags

[root@manager test-tags]# ansible-playbook -i hosts debug-tags.yaml

PLAY [test tags] *******************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [10.100.235.228]

TASK [test untagged] ***************************************************************************************************
ok: [10.100.235.228] => {
    "msg": "no tag"
}

TASK [test tag always] *************************************************************************************************
ok: [10.100.235.228] => {
    "msg": "always tag"
}

TASK [test tag customer] ***********************************************************************************************
ok: [10.100.235.228] => {
    "msg": "tagged tag"
}

PLAY RECAP *************************************************************************************************************
10.100.235.228             : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

指定–tags always

[root@manager test-tags]# ansible-playbook -i hosts debug-tags.yaml --tags always

PLAY [test tags] *******************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [10.100.235.228]

TASK [test tag always] *************************************************************************************************
ok: [10.100.235.228] => {
    "msg": "always tag"
}

PLAY RECAP *************************************************************************************************************
10.100.235.228             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

指定–tags never

[root@manager test-tags]# ansible-playbook -i hosts debug-tags.yaml --tags never

PLAY [test tags] *******************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [10.100.235.228]

TASK [test tag always] *************************************************************************************************
ok: [10.100.235.228] => {
    "msg": "always tag"
}

TASK [test tag never] **************************************************************************************************
ok: [10.100.235.228] => {
    "msg": "never tag"
}

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

指定–tags tagged

[root@manager test-tags]# ansible-playbook -i hosts debug-tags.yaml --tags tagged

PLAY [test tags] *******************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [10.100.235.228]

TASK [test tag always] *************************************************************************************************
ok: [10.100.235.228] => {
    "msg": "always tag"
}

TASK [test tag customer] ***********************************************************************************************
ok: [10.100.235.228] => {
    "msg": "tagged tag"
}

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

指定–tags untagged

[root@manager test-tags]# ansible-playbook -i hosts debug-tags.yaml --tags uptagged

PLAY [test tags] *******************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [10.100.235.228]

TASK [test tag always] *************************************************************************************************
ok: [10.100.235.228] => {
    "msg": "always tag"
}

PLAY RECAP *************************************************************************************************************
10.100.235.228             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

指定–skip-tags 自定义tag

[root@manager test-tags]# ansible-playbook -i hosts debug-tags.yaml --skip-tags test-tagged

PLAY [test tags] *******************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [10.100.235.228]

TASK [test untagged] ***************************************************************************************************
ok: [10.100.235.228] => {
    "msg": "no tag"
}

TASK [test tag always] *************************************************************************************************
ok: [10.100.235.228] => {
    "msg": "always tag"
}

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

定义多个tag

定义多个tags

[root@manager test-tags]# cat debug-tags.yaml
---
- name: test tags
  hosts: all
  tags:
  - xadocker
  tasks:
  - name: test untagged
    debug:
      msg: "no tag"

  - name: test tag
    debug:
      msg: "mytag"
    tags:
    - mytag

  - name: test tag tag2
    debug:
      msg: "mytag mytag2" 
    tags:
    - mytag
    - mytag2

在剧本中定义tag

上面都是在tasks中定义tag,其实还可以在剧本中定义tag,此时该剧本中的所有task都会继承该tag

[root@manager test-tags]# cat debug-tags.yaml
---
- name: test tags
  hosts: all
  tags:
  - xadocker
  tasks:
  - name: test untagged
    debug:
      msg: "no tag"

  - name: test tag customer
    debug:
      msg: "tagged tag"
    tags:
    - test-tagged

测试运行

[root@manager test-tags]# ansible-playbook -i hosts debug-tags.yaml --tags uptagged

PLAY [test tags] *******************************************************************************************************

PLAY RECAP *************************************************************************************************************

[root@k8s-master test-tags]# ansible-playbook -i hosts debug-tags.yaml --tags tagged

PLAY [test tags] *******************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [10.100.235.228]

TASK [test untagged] ***************************************************************************************************
ok: [10.100.235.228] => {
    "msg": "no tag"
}

TASK [test tag customer] ***********************************************************************************************
ok: [10.100.235.228] => {
    "msg": "tagged tag"
}

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

[root@k8s-master test-tags]# ansible-playbook -i hosts debug-tags.yaml --tags xadocker

PLAY [test tags] *******************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [10.100.235.228]

TASK [test untagged] ***************************************************************************************************
ok: [10.100.235.228] => {
    "msg": "no tag"
}

TASK [test tag customer] ***********************************************************************************************
ok: [10.100.235.228] => {
    "msg": "tagged tag"
}

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

正文完
 
xadocker
版权声明:本站原创文章,由 xadocker 2019-04-04发表,共计7970字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)