示例-2:结合fail语句
test.sh[vipxf@test02 ~]$ pwd/home/vipxf[vipxf@test02 ~]$[vipxf@test02 ~]$ ll test.sh-rw-rw-r-- 1 vipxf vipxf 127 Oct 21 00:11 test.sh[vipxf@test02 ~]$[vipxf@test02 ~]$ cat test.sh#!/bin/bashif [ "$1" = "Anliven" ];thenecho "Success"elseecho "Failed"fi[vipxf@test02 ~]$
when2.yaml
[root@test01 ansible-test]# cat when2.yaml- hosts: taremote_user: vipxfgather_facts: yesvars:- teststring: Failedtasks:- name: test1shell: sh /home/vipxf/test.sh {{ testvar }}register: result- name: test2shell: echo "rc 0"when: result.rc == 0# 基于先前任务的结果来执行- name: test3shell: echo "Success"when: '"Success" in result.stdout'# 对stdout结果进行判断- name: test4fail: msg="Check Failed"# 任务报错并抛出msg信息when: result.stdout == (( teststring ))# 在when使用playbook中定义的变量[root@test01 ansible-test]#[root@test01 ansible-test]# ansible-playbook -v when2.yaml -e testvar=AnlivenUsing /etc/ansible/ansible.cfg as config filePLAY [ta] *********************************************************************************************************************************************************************************************TASK [Gathering Facts] ********************************************************************************************************************************************************************************ok: [172.20.8.247]TASK [test1] ******************************************************************************************************************************************************************************************changed: [172.20.8.247] => {"changed": true, "cmd": "sh /home/vipxf/test.sh Anliven", "delta": "0:00:00.003638", "end": "2022-10-21 18:05:00.273596", "rc": 0, "start": "2022-10-21 18:05:00.269958", "stderr": "", "stderr_lines": [], "stdout": "Success", "stdout_lines": ["Success"]}TASK [test2] ******************************************************************************************************************************************************************************************changed: [172.20.8.247] => {"changed": true, "cmd": "echo \"rc 0\"", "delta": "0:00:00.002893", "end": "2022-10-21 18:05:00.722340", "rc": 0, "start": "2022-10-21 18:05:00.719447", "stderr": "", "stderr_lines": [], "stdout": "rc 0", "stdout_lines": ["rc 0"]}TASK [test3] ******************************************************************************************************************************************************************************************changed: [172.20.8.247] => {"changed": true, "cmd": "echo \"Success\"", "delta": "0:00:00.002632", "end": "2022-10-21 18:05:01.172659", "rc": 0, "start": "2022-10-21 18:05:01.170027", "stderr": "", "stderr_lines": [], "stdout": "Success", "stdout_lines": ["Success"]}TASK [test4] ******************************************************************************************************************************************************************************************skipping: [172.20.8.247] => {"changed": false, "skip_reason": "Conditional result was False"}PLAY RECAP ********************************************************************************************************************************************************************************************172.20.8.247: ok=4changed=3unreachable=0failed=0skipped=1rescued=0ignored=0[root@test01 ansible-test]#[root@test01 ansible-test]# ansible-playbook -v when2.yaml -e testvar="test"Using /etc/ansible/ansible.cfg as config filePLAY [ta] *********************************************************************************************************************************************************************************************TASK [Gathering Facts] ********************************************************************************************************************************************************************************ok: [172.20.8.247]TASK [test1] ******************************************************************************************************************************************************************************************changed: [172.20.8.247] => {"changed": true, "cmd": "sh /home/vipxf/test.sh test", "delta": "0:00:00.003612", "end": "2022-10-21 18:06:49.907767", "rc": 0, "start": "2022-10-21 18:06:49.904155", "stderr": "", "stderr_lines": [], "stdout": "Failed", "stdout_lines": ["Failed"]}TASK [test2] ******************************************************************************************************************************************************************************************changed: [172.20.8.247] => {"changed": true, "cmd": "echo \"rc 0\"", "delta": "0:00:00.002721", "end": "2022-10-21 18:06:50.338001", "rc": 0, "start": "2022-10-21 18:06:50.335280", "stderr": "", "stderr_lines": [], "stdout": "rc 0", "stdout_lines": ["rc 0"]}TASK [test3] ******************************************************************************************************************************************************************************************skipping: [172.20.8.247] => {"changed": false, "skip_reason": "Conditional result was False"}TASK [test4] ******************************************************************************************************************************************************************************************fatal: [172.20.8.247]: FAILED! => {"changed": false, "msg": "Check Failed"}PLAY RECAP ********************************************************************************************************************************************************************************************172.20.8.247: ok=3changed=2unreachable=0failed=1skipped=1rescued=0ignored=0[root@test01 ansible-test]#
推荐阅读
- Web自动化---解决登录页面随机验证码问题
- 小样本利器4. 正则化+数据增强 Mixup Family代码实现
- 从0搭建vue3组件库:自动化发布、管理版本号、生成 changelog、tag
- ansible使用临时命令通过模块来执行任务
- Ansible 批处理实战
- ansible应用之安装elk框架
- app自动化测试环境安装
- reportportal 集成 robotframework 自动化执行及结果可视化
- Android自动化测试工具调研
- 五 微软出品自动化神器【Playwright+Java】系列 之常见点击事件操作