Good afternoon,
I opened an issue on GitHub, and was directed here, which is the obvious place. (I initially thought that the behavior was indeed a bug, but there was some clarity added).
What would be the preferred method to perform input validation on a user supplied variable provided via extra vars? Is the recommended approach to simply handle an error (and end the host, play or task) if the variable doesn’t match an expected input, as it is not possible to overwrite it due to precedence?
As it was, we were trying to use “-e” for the extra variables, and then using set_fact to overwrite it if it didn’t meet the expected requirements.
This is what I was trying:
`
###This works as expected
- name: "Setting Deploy Environment Variable for Deployment Tasks"
set_fact:
deploy_environment: "Development"
when: deploy_environment is undefined
tags:
- variable-test-task
###This does not work
- name: "Setting Deploy Environment Variable for Deployment Tasks"
set_fact:
deploy_environment: "Development"
when: deploy_environment != "Development"
tags:
- variable-test-task
###This does not work
- name: "Setting Deploy Environment Variable for Deployment Tasks"
set_fact:
deploy_environment: "Development"
when: deploy_environment == "Production"
tags:
- variable-test-task
###This does not work, playing with syntax, might be expected
- name: "Setting Deploy Environment Variable for Deployment Tasks"
set_fact:
deploy_environment: "Development"
when: not (deploy_environment is defined) or not ((deploy_environment == 'Development') or (deploy_environment == 'Production'))
tags:
- variable-test-task
###This does not work, playing with syntax, might be expected
- name: "Setting Deploy Environment Variable for Deployment Tasks"
set_fact:
deploy_environment: "Development"
when: (deploy_environment is undefined) or ((deploy_environment != 'Development') or (deploy_environment != 'Production'))
tags:
- variable-test-task
###This does not work, playing with syntax, might be expected
- name: "Setting Deploy Environment Variable for Deployment Tasks"
set_fact:
deploy_environment: "Development"
when: (deploy_environment != 'Development') or (deploy_environment != 'Production')
tags:
- variable-test-task
`
And that was patiently explained to have been by design. I’m open to suggestions on the best overall way to accomplish this.
(Slightly related, I’d like to submit documentation changes that may make this more clear for users, and is that best done via a merge request?)
Thanks,
Michael