目录
[2.1 Debug模块](#2.1 Debug模块)
[2.2 tags标签](#2.2 tags标签)
[2.3 忽略错误](#2.3 忽略错误)
1、调试方法
|---------|---------------------------------------------------|
| 具体方法 | 应用场景 |
| debug标签 | 执⾏剧本的时候输出剧本的执⾏流程,⼀般配合register⼀起使⽤. 输出facts变量自定义变量 |
| tags标签 | 给⼀些模块加上标签,运⾏剧本的时候可以运⾏指定标签的内容,排除指定标签. |
| 忽略错误 | 运⾏剧本的时候忽略⼀些模块的错误,让剧本继续运⾏. |
2、实例
2.1 Debug模块
|-------|---------------------------|
| debug | |
| msg | 相当于是echo命令,配置着register一起用 |
实例1-调试-nfs服务端部署剧本
剧本文件及执行结果:
[root@ansible ~]# cat debug-nfs-server.yml
---
- name: 调试nfs服务端
hosts: all
tasks:
- name: 部署nfs服务端软件
yum:
name: nfs-utils
state: installed
- name: 创建对应的目录,权限
file:
path: /nfsfile
owner: nfsnobody
group: nfsnobody
state: directory
register: file_jieguo
- name: 修改配置文件
lineinfile:
path: /etc/exports
line: "/nfsfile 192.168.81.0/24(rw,sync,root_squash)"
state: present
backup: yes
- name: 输出这个过程
debug:
msg: "{{ file_jieguo }}"
- name: 启动rpc服务
systemd:
name: rpcbind
enabled: yes
state: started
- name: 启动nfs服务
systemd:
name: nfs
enabled: yes
state: started
[root@ansible ~]# ansible-playbook debug-nfs-server.yml
PLAY [调试nfs服务端] *****************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************
ok: [192.168.81.134]
ok: [192.168.81.136]
ok: [192.168.81.133]
ok: [192.168.81.135]
ok: [192.168.81.132]
TASK [部署nfs服务端软件] ***************************************************************************************************************************************
ok: [192.168.81.132]
ok: [192.168.81.136]
ok: [192.168.81.133]
ok: [192.168.81.134]
ok: [192.168.81.135]
TASK [创建对应的目录,权限] ***************************************************************************************************************************************
changed: [192.168.81.132]
changed: [192.168.81.135]
changed: [192.168.81.136]
changed: [192.168.81.133]
changed: [192.168.81.134]
TASK [修改配置文件] *******************************************************************************************************************************************
changed: [192.168.81.132]
changed: [192.168.81.135]
changed: [192.168.81.136]
changed: [192.168.81.134]
changed: [192.168.81.133]
TASK [输出这个过程] *******************************************************************************************************************************************
ok: [192.168.81.136] => {
"msg": {
"changed": true,
"diff": {
"after": {
"group": 65534,
"mode": "0777",
"owner": 65534,
"path": "/nfsfile",
"state": "directory"
},
"before": {
"group": 0,
"mode": "0755",
"owner": 0,
"path": "/nfsfile",
"state": "absent"
}
},
"failed": false,
"gid": 65534,
"group": "nfsnobody",
"mode": "0777",
"owner": "nfsnobody",
"path": "/nfsfile",
"secontext": "unconfined_u:object_r:default_t:s0",
"size": 6,
"state": "directory",
"uid": 65534
}
}
ok: [192.168.81.132] => {
"msg": {
"changed": true,
"diff": {
"after": {
"group": 65534,
"mode": "0777",
"owner": 65534,
"path": "/nfsfile",
"state": "directory"
},
"before": {
"group": 0,
"mode": "0755",
"owner": 0,
"path": "/nfsfile",
"state": "absent"
}
},
"failed": false,
"gid": 65534,
"group": "nfsnobody",
"mode": "0777",
"owner": "nfsnobody",
"path": "/nfsfile",
"secontext": "unconfined_u:object_r:default_t:s0",
"size": 6,
"state": "directory",
"uid": 65534
}
}
ok: [192.168.81.134] => {
"msg": {
"changed": true,
"diff": {
"after": {
"group": 65534,
"mode": "0777",
"owner": 65534,
"path": "/nfsfile",
"state": "directory"
},
"before": {
"group": 0,
"mode": "0755",
"owner": 0,
"path": "/nfsfile",
"state": "absent"
}
},
"failed": false,
"gid": 65534,
"group": "nfsnobody",
"mode": "0777",
"owner": "nfsnobody",
"path": "/nfsfile",
"secontext": "unconfined_u:object_r:default_t:s0",
"size": 6,
"state": "directory",
"uid": 65534
}
}
ok: [192.168.81.133] => {
"msg": {
"changed": true,
"diff": {
"after": {
"group": 65534,
"mode": "0777",
"owner": 65534,
"path": "/nfsfile",
"state": "directory"
},
"before": {
"group": 0,
"mode": "0755",
"owner": 0,
"path": "/nfsfile",
"state": "absent"
}
},
"failed": false,
"gid": 65534,
"group": "nfsnobody",
"mode": "0777",
"owner": "nfsnobody",
"path": "/nfsfile",
"secontext": "unconfined_u:object_r:default_t:s0",
"size": 6,
"state": "directory",
"uid": 65534
}
}
ok: [192.168.81.135] => {
"msg": {
"changed": true,
"diff": {
"after": {
"group": 65534,
"mode": "0777",
"owner": 65534,
"path": "/nfsfile",
"state": "directory"
},
"before": {
"group": 0,
"mode": "0755",
"owner": 0,
"path": "/nfsfile",
"state": "absent"
}
},
"failed": false,
"gid": 65534,
"group": "nfsnobody",
"mode": "0777",
"owner": "nfsnobody",
"path": "/nfsfile",
"secontext": "unconfined_u:object_r:default_t:s0",
"size": 6,
"state": "directory",
"uid": 65534
}
}
TASK [启动rpc服务] ******************************************************************************************************************************************
changed: [192.168.81.134]
changed: [192.168.81.133]
changed: [192.168.81.132]
changed: [192.168.81.135]
changed: [192.168.81.136]
TASK [启动nfs服务] ******************************************************************************************************************************************
changed: [192.168.81.136]
changed: [192.168.81.133]
changed: [192.168.81.134]
changed: [192.168.81.132]
changed: [192.168.81.135]
PLAY RECAP **********************************************************************************************************************************************
192.168.81.132 : ok=7 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.81.133 : ok=7 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.81.134 : ok=7 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.81.135 : ok=7 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.81.136 : ok=7 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@ansible ~]#
2.2 tags标签
Ansible中的tags标签用于为任务指定标记,并在运行时根据指定的标记只运行部分任务,从而避免对整个剧本进行执行。在编写包含多个任务的剧本时,有时候只需要在执行时运行其中一任务。这时,使用tag标签是非常有用的,它可以为每个任务指定一个标签,这样在执行时只需要运行指定标签的任务即可。
也可以这样理解
一个剧本里面可能包涵了多个任务,但是又不知道剧本中任务是不是都能执行,不确定,这个时候就可以用标签标记任务,然后运行剧本时指定标签运行不确定能不能执行的任务
执行剧本时,使用--tags选项指定要运行的标签。
运行指定的标签
ansible-play --tags 自定义的标签名 剧本文件名
运行剧本的时候排除指定的标签
ansible-play --skip-tags 要排除的标签名 剧本文件名
实例
2.3 忽略错误
当运行剧本时,剧本是按照任务的编写顺序执行,当某个任务运行出现错误时,则则这个任务下面的任务出现错误时,
用于运行剧本的时候,强制让某个任务(模块)运行即使出现错误,也不会中断我们整个剧本的执行。
实例