如果某些条件失败,我想中止执行剩余任务。并显示正确的错误信息。
因此,我不想跳过剩余的任务,而是想显示错误消息并停止执行 ansible playbook。
可以说我在命令下运行
$ ansible-playbook playbook.yml -e "param1=value1 param2=value"
我的剧本看起来像这样:-
playbook.yml:-
---
- hosts: local
user: roop
gather_facts: no
vars: {param1: "", param2: ""}
tasks:
#check whether param1 defined
- name: 'Check for valid param1'
shell: echo {{ param1 }}
register: isValidParam1
when: param1 !=""
#check if param1 is null or invalid
- name: 'check if param1 is null or invalid'
debug: msg="Please enter correct param1"
when: param1 == ""
#check whether param2 defined
- name: 'Check for valid param2'
shell: echo {{ param2 }}
register: isValidParam2
when: param2 != ""
#check if param2 is null or invalid
- name: 'check if param2 is null or invalid'
debug: msg="Please enter correct param2"
when: param2 == ""
#check params is valid and defined
- name: 'Check for valid params'
shell: echo "param1={{ param1 }} param2={{ param2 }}
register: validParams
when: isValidParam1 is defined and isValidParam2 is defined
#check if params are invalid then abort below all tasks.
- name: 'check if validParams is null or invalid'
debug: msg="Please enter correct Params"
when: validParams is not defined
# based on validParams, and different value of param1 more than
10 task executing.
正如我在上次任务评论中提到的那样。我正在执行 10 多个基于不同价值validParams
的任务。param1
如果validParams
未定义我在这里需要的内容,则中止所有执行并显示错误消息。
有什么有效的方法可以做到这一点。请给我建议。