-
Notifications
You must be signed in to change notification settings - Fork 0
/
ansible_custom_module__detect_substring_in_csv_column.py
131 lines (91 loc) · 3.89 KB
/
ansible_custom_module__detect_substring_in_csv_column.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
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/python
# - name: Check if any mismatch in prezzo
# ansible_custom_module__detect_substring_in_csv_column:
# csv_path: "{{ base_path }}/ctrl2_results__common_items_attributes_mismatch.csv"
# delimiter: ";"
# column_name: "confronto_prezzo"
# substring: "f"
# register: any_mismatch_in_prezzo_obj
# # if this condition is met, the variable.substring_found is set to true = error
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible.module_utils.basic import AnsibleModule
def run_module():
module_args = dict(
# inputs to the module
# path of the csv file to inspect
csv_path = dict(type='str', required=True),
delimiter = dict(type='str', required=True),
# column where youwant to search the substring
column_name = dict(type='str', required=True),
# substring to search in the column
substring = dict(type='str', required=True),
)
# this is the output of the playbook for each host
result = dict(
file_accessed_successfully=False,
substring_found=False,
# match_indexes=[],
# found_lines='',
# found=False
)
# options which state syntax rules for calling the module
module = AnsibleModule(
argument_spec=module_args,
supports_check_mode=True
)
# # this snippet was to access the file at indicated path, search a a string and retrun true if it was found
# with open(module.params['path'], 'r') as f:
# for line in f.readlines():
# if module.params['search_string'] in line:
# result['found_lines'] = result['found_lines'] + line
# result['found'] = True
# python is called on the target machine
#---------------------------------------
# specific for csv and sorting
import csv, operator
csv_file_path = module.params['csv_path']
column_name = module.params['column_name']
# columns_to_order_by = ['idm', 'idc']
mydelimiter = module.params['delimiter']
substring = module.params['substring']
# # temp
# # ------
# column_name="negozi_to_casse"
# substring="ERRORE -> Elementi mancanti su uno o più casse"
# csv_file_path = "mailbodychecks_ctrl2_all.csv"
# import csv
# mydelimiter=";"
try:
with open(csv_file_path, 'r') as csvfile:
results["file_accessed_successfully"] = True
pass
except Exception as e:
print("I could not open file {}. Check if the file exists. Eccezione: \n{}".format(csv_file_path, e))
with open(csv_file_path, 'r') as csvfile:
result["substring_found"] = False
spamreader = csv.DictReader(csvfile, delimiter=mydelimiter)
# # spamereader è un generator e può essere usato solo una volta
# rows_list = [ row for row in spamreader ]
# try:
# entire_column = [ row[column_name] for row in spamreader ]
# except:
# raise Keyerror("Could not access key {} in all rows of {}. This key could be missing.".format(column_name, csv_file_path))
substring_in_column = [ substring in row[column_name] for row in spamreader ]
if any( substring_in_column ):
# print("trovata stringa!")
result["substring_found"] = True
# indexes delle righe in cui è presente la substring
indexes = [
index for index in range(len(substring_in_column)) if substring_in_column[index] == True
]
# for row in spamreader:
# if substring in row[column_name]:
# # print("trovata stringa!")
# substring_found = True
result["match_indexes"] = indexes
module.exit_json(**result)
def main():
run_module()
if __name__ == '__main__':
main()