forked from beregond/ansible-filter-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefined.py
31 lines (27 loc) · 853 Bytes
/
defined.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Ansible defined filter plugin
# Raises error when variable is not defined or empty
# Optional argument is name of the variable to display in error message
#
# Marcin Hlybin, [email protected]
#
# Example usage:
# - debug: msg="User id is {{ uid | defined }}"
# - debug: msg="User id is {{ uid | defined('uid') }}"
#
# Result1: FAILED! => {"failed": true, "msg": "Variable not defined"}
# Result2: FAILED! => {"failed": true, "msg": "Variable not defined: uid"}
#
from ansible import errors
def defined(value, name=None):
if name:
message = "Variable not defined: {}".format(name)
else:
message = "Variable not defined"
if value in (None, ''):
raise errors.AnsibleFilterError(message)
return value
class FilterModule(object):
def filters(self):
return {
'defined': defined
}