checking if a dynamically selected variable is defined?

37 views
Skip to first unread message

Dylan Martin

unread,
Aug 29, 2014, 12:30:48 PM8/29/14
to ansible...@googlegroups.com
Hi all.

I often write plays that require extra vars.  eg "ansible-playbook user -e user=bob"

In that play, I'll start off with 

- fail: msg="You forgot to set the user variable"
  when: user is not defined or user == ""

And I was thinking it might be nice to write an external, reusable play that would be called like this:

- include: check_defined.yaml checkvar=user

and look something like this

----
- hosts: all
  tasks:
  - fail: msg="{{ you forgot to specify a checkvar }}"
    when: checkvar is not defined or checkvar == ""
  - fail: msg="you forgot to set {{ checkvar }}"
    when: ?????? is not defined or ?????? == ""

The first fail is just to catch myself if I include check_defined.yaml without setting checkvar
the second fail is the thing I actually am curious about.  The ????? are the bits I don't know how to do.

Okay, I admit this is pretty silly, since I could write the is not defined checks for about 20 playbooks in the time it took to write this email.  And including the play is one line when the play itself ( or task) is 2.  But still, I think I'd understand ansible and jinja2 better if I knew the answer to this.  

Thanks
-Dylan

Michael DeHaan

unread,
Aug 30, 2014, 9:25:11 AM8/30/14
to ansible...@googlegroups.com
Here's a quicker one-liner:

- assert: { that: 'foo is defined' }

Or any other conditional, etc.  'that' can also take a list of conditionals.


--
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/a1dd35e3-6ea3-4482-991f-e979456bdc28%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages