我正在尝试include
根据传入的参数动态归档ansible-playbook
。为了验证参数,我正在检查它的when
条件,但它假设不适合我。它首先检查包含文件。
playbook.yml:-
---
- hosts: local
user: roop
gather_facts: no
vars: {action: "",validAction: ["action1","action2","action3","action4"]}
tasks:
- name: validate action
fail: msg="please make sure you pass valid action"
when: action == "" or action not in validAction
- include: "action/{{ action }}.yml"
when: action !="" and action in validAction
这是两种情况:
1)当action没有传入参数时
ansible-playbook playbook.yml
错误:
ERROR: file not found: /path/to/action/.yml
预期输出:
Should be execute task 1 "Validate action" first.
2) 动作错误时
ansible-playbook playbook.yml -e "action=action5"
错误:
ERROR: file not found: /path/to/action/action5.yml
预期输出:
当用户传递无效操作时,它应该检查 task1 entity not in validEntity
。在第一个任务中,如果条件无效,那么它应该中止执行。
有什么建议来处理这个案子吗?