Let’s say I ask for a revision_no from the user. If the user doesn’t input a value, I would like to display a custom message. Right now ansible shows a bunch of “skipping” message, but I know my users would want to know why it skipped.
hosts: webservers
user: deployment
serial: 1
vars_prompt:
name: “revision_no”
prompt: “Input revision number”
private: no
There may be a better way to do this but I solved the problem by having a separate task initially that verifies the variable is set and fails the play if not (via the fail module).
name: Checking for required variables
fail: msg=“Playbook execution failed - ‘revision_no’ is required!”
when: not revision_no
If you don’t want the play to outright fail, you could replace the fail module with something like debug.
So, here’s the full site.yml file. If I dont input a revision number (I hit enter at the prompt), it throws the error message as expected BUT it also executes the role. I want it to bomb out w/ the error msg and not continue.
I believe this is because variables set via vars_prompt are always defined.
A workaround might be to set a default for revision_no and check it doesn’t match in your role, e.g.:
vars_prompt:
name: “revision_no"
prompt: "Input revision number”
default: “invalid”
private: no
……
roles:
{ role: web, when: revision_no != “invalid” }
I’ve not made extensive use of roles so it seems odd to me that a role would still execute when a failure is thrown but it must have something to do with Ansible’s order of execution.
not revision_no works because of you just do Enter it will have empty string and empty string is False. It’s matter of preference but I would test for that.