Display error message if variable isn't set using playbook

536 views
Skip to first unread message

Samnang Sen

unread,
May 5, 2014, 10:53:54 AM5/5/14
to ansible...@googlegroups.com
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

  vars_files:
  - "group_vars/deploy_list"

  roles:
  - { role: web, when: revision_no is not defined }

Samnang Sen

unread,
May 5, 2014, 10:58:27 AM5/5/14
to ansible...@googlegroups.com
That last line is supposed to read "revision_no is defined"

Garrett Plasky

unread,
May 5, 2014, 1:19:05 PM5/5/14
to ansible...@googlegroups.com
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.

Michael DeHaan

unread,
May 5, 2014, 11:51:29 PM5/5/14
to ansible...@googlegroups.com
You can say this specifically:

       when: revision_no is not defined

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/73615f28-23c0-4d0b-89f1-80a323ccce42%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Samnang Sen

unread,
May 7, 2014, 4:34:31 PM5/7/14
to ansible...@googlegroups.com
This does not work for me. Garrett's example does ...

Samnang Sen

unread,
May 7, 2014, 5:02:59 PM5/7/14
to ansible...@googlegroups.com
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.
 

---
- hosts: webservers
  user: deployment
#  serial: 1

  vars_prompt:
   - name: "revision_no"
     prompt: "Input revision number"
     private: no

  vars_files:
  - "group_vars/deploy_list"

  tasks:
  - name: Checking for required variables
    fail: msg="Playbook execution failed - 'revision_no' is required!"
    when: not revision_no

  roles:
  - { role: web, when: revision_no is defined }

Garrett Plasky

unread,
May 7, 2014, 5:21:58 PM5/7/14
to ansible...@googlegroups.com
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.

-- 
Garrett Plasky
Evernote Operations

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/JJWv3IWtWwU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

Sam S

unread,
May 7, 2014, 7:36:13 PM5/7/14
to ansible...@googlegroups.com

I figured it out, not sure if this is valid.

roles:

  - { role: web, when: revision_no }

Michael DeHaan

unread,
May 9, 2014, 7:58:24 PM5/9/14
to ansible...@googlegroups.com
Seems like your variable is defined then, just False.




Stanislav Antic

unread,
May 10, 2014, 3:41:45 AM5/10/14
to ansible...@googlegroups.com
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.

You can always do debug to see true value:

  tasks:
    - debug: msg={{revision_no}}
Reply all
Reply to author
Forward
0 new messages