Reputation: 7762
I am trying to include
file dynamically on the basis of parameter passed in ansible-playbook
. To validate parameter I am checking it in when
condition but its suppose to not working for me. Its checking include file first.
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
These are two cases:
1) When action is not passed in parameter
ansible-playbook playbook.yml
Error:
ERROR: file not found: /path/to/action/.yml
Expected Output:
Should be execute task 1 "Validate action" first.
2) When action is wrong
ansible-playbook playbook.yml -e "action=action5"
Error:
ERROR: file not found: /path/to/action/action5.yml
Expected Output:
when user pass action which is not valid action then it should check in task1 entity not in validEntity
. in first task if condition are not valid then it should abort the executions.
any suggestions to handle this case?
Upvotes: 0
Views: 3554
Reputation: 3234
Well it seems that ansible does a pre checks of include before it run the function parse_yaml_from_file
is called and that fails if no file is on FS
As a workaround you can use the following code, but you might want to report that on Ansible github as a feature request.
Please create a dummy.yml under the directory action for the code below to work touch action/dummy.yml
---
- hosts: all
gather_facts: no
vars:
action : ""
dummy : "{{ action if action in validAction else 'dummy' }}"
validAction : ["action1","action2","action3","action4"]
tasks:
- include: "action/{{dummy}}.yml"
Upvotes: 1