ansible:tags模块


1. tags

只执行某个任务而不是全部任务,使用打标签的方法tags
在这里插入图片描述
格式一:

[root@server4 ~]# cat mkdir.yml 
---
- hosts: testB
  remote_user: root
  tasks: 
  - name: task1
    file: path=/testdir/task1
          state=touch
    tags: t1

  - name: task2
    file: path=/testdir/task2
          state=touch
    tags: t2
# 测试:
[root@server4 ~]# ansible-playbook --tags=t2 mkdir.yml

PLAY [testB] *******************************************************************

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

TASK [task2] *******************************************************************
changed: [server3]

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

[root@server4 ~]# ansible-playbook --skip-tags=t2 mkdir.yml  # 不执行t2其它的都执行
PLAY [testB] *******************************************************************

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

TASK [task1] *******************************************************************
changed: [server3]

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

格式二:

[root@server4 ~]# cat mkdir.yml 
---
- hosts: testB
  remote_user: root
  tasks: 
  - name: task1
    file: path=/testdir/task1
          state=touch
tags: 
   - t1

  - name: task2
    file: path=/testdir/task2
          state=touch
tags: [‘t2’]

给多个task使用多个标签

格式一:

[root@server4 ~]# cat tags.yml 
---
- hosts: testB
  remote_user: root
  tasks:
  - name: install httpd
    tags: httpd,package
    yum: 
      name=httpd
      state=latest
  
  - name: start httpd
    tags: httpd,service
    service:
      name: httpd
      state: started

格式二:

[root@server4 ~]# cat tags.yml 
---
- hosts: testB
  remote_user: root
  tags: httpd
  tasks:
  - name: install httpd
    tags: ['package']
    yum: 
      name=httpd
      state=latest
  
  - name: start httpd
    tags: 
      - service
    service:
      name: httpd
      state: started

查看playbook中有多少个标签

[root@server4 ~]# ansible-playbook --list-tags tags.yml

playbook: tags.yml

  play #1 (testB): testB	TAGS: [httpd]
      TASK TAGS: [httpd, package, service]
1.1 always

当task中的tags有always时,这个task每次都会执行

[root@server4 ~]# cat tags1.yml 
---
- hosts: testB
  remote_user: root
  tasks: 
  - name: task1
    file: path=/testdir/task1
          state=touch
    tags: t1

  - name: task2
    file: path=/testdir/task2
          state=touch
    tags: t2

  - name: task3
    file: path=/testdir/task3
          state=touch
    tags: t3,always
# 测试:
[root@server4 ~]# ansible-playbook --tags=t2 mkdir.yml  # 只执行t2但是t3中有always,所以t3也会被执行

PLAY [testB] *******************************************************************

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

TASK [task2] *******************************************************************
changed: [server3]

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

如果不想执行always,可以跳过always

[root@server4 ~]# ansible-playbook --tags t1 --skip-tags always mkdir.yml

PLAY [testB] *************************************************************************************************************************************

TASK [task1] *************************************************************************************************************************************
changed: [server3]

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

只跳过指定task的always

[root@server4 ~]# cat tags1.yml 
---
- hosts: testB
  remote_user: root
  tasks: 
  - name: task1
    file: path=/testdir/task1
          state=touch
    tags: t1,always

  - name: task2
    file: path=/testdir/task2
          state=touch
    tags: t2

  - name: task3
    file: path=/testdir/task3
          state=touch
    tags: t3,always

[root@server4 ~]# ansible-playbook --tags t2 --skip-tags t3 tags1.yml # 跳过t3

PLAY [testB] *******************************************************************

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

TASK [task1] *******************************************************************
changed: [server3]

TASK [task2] *******************************************************************
changed: [server3]

PLAY RECAP *********************************************************************
server3                    : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
1.2 关于tags的其它标签用法:tagged、untagged、all

在这里插入图片描述
在这里插入图片描述

1.3 nerver

nerver标签:表示不执行task3

[root@server4 ~]# cat tags1.yml 
---
- hosts: testB
  remote_user: root
  tasks: 
  - name: task1
    file: path=/testdir/task1
          state=touch
    tags: t1

  - name: task2
    file: path=/testdir/task2
          state=touch
    tags: t2

  - name: task3
    file: path=/testdir/task3
          state=touch
    tags: t3,never
# 测试:
[root@server4 ~]# ansible-playbook tags1.yml 

PLAY [testB] *******************************************************************

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

TASK [task1] *******************************************************************
changed: [server3]

TASK [task2] *******************************************************************
changed: [server3]

PLAY RECAP *********************************************************************
server3                    : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值