-
Notifications
You must be signed in to change notification settings - Fork 1
/
ansiblefilterexample
69 lines (45 loc) · 1.74 KB
/
ansiblefilterexample
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
create under master/filter_plugins/flifo_json.py
# (c) 2015, Filipe Niero Felisbino <[email protected]>
#
# This file is part of Ansible
#
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible.errors import AnsibleError, AnsibleFilterError
try:
import json
HAS_LIB = True
except ImportError:
HAS_LIB = False
def flifo_json(data):
if not HAS_LIB:
raise AnsibleError('You need to install "python json module" prior to running '
'flifo_json filter')
try:
print(data)
#print expr
#item_dict = json.loads(data)
item_len = len(data['result']) - 1
#return item_len
return data['result'][int(item_len)]['id']
#item_id = item_dict['result'][item_len-1]['id']
#return 345
except Exception as e:
# For older jmespath, we can get ValueError and TypeError without much info.
raise AnsibleFilterError('Error in jmespath.search in json_query filter plugin:\n%s' % e)
class FilterModule(object):
''' Query filter '''
def filters(self):
return {
'flifo_json': flifo_json
}
how to consume fiter:
- name: Get ALL backups using sedssion ID for app Name APP_NAME
uri:
url: https://{{ INPUT_APPLIANCE }}/actifio/api/info/lsbackup?filtervalue=appname%3D{{ APP_NAME }}%26componenttype%3D0%26consistencydate%3C{{ INPUT_DATE }}%20{{ INPUT_TIME }}%26apptype%3DOracle%26jobclass%3Dsnapshot&sessionid={{ webpage.json.sessionid }}
method: GET
validate_certs: no
register: get_all_backups
- debug: msg="object lenghth is {{ get_all_backups.json | flifo_json() }}"
you are passing an array and return array length