6
6
from __future__ import (absolute_import , division , print_function )
7
7
__metaclass__ = type
8
8
9
- # import json
9
+ import os
10
+ import json
11
+ import hashlib
10
12
11
13
from ansible .utils .display import Display
12
14
@@ -23,10 +25,11 @@ def filters(self):
23
25
'vhost_directory' : self .vhost_directory ,
24
26
'vhost_listen' : self .vhost_listen ,
25
27
'vhost_templates' : self .vhost_templates ,
28
+ 'vhost_templates_checksum' : self .vhost_templates_checksum ,
29
+ 'vhost_templates_validate' : self .vhost_templates_validate ,
26
30
'http_vhosts' : self .http_vhosts ,
27
31
'changed_vhosts' : self .changed_vhosts ,
28
32
'certificate_existing' : self .certificate_existing ,
29
- 'validate_listener' : self .validate_listener ,
30
33
}
31
34
32
35
def vhost_directory (self , data , directory , state = "present" ):
@@ -58,7 +61,6 @@ def vhost_listen(self, data, port, default):
58
61
used in jinja_macros.j2
59
62
"""
60
63
# display.v(f"vhost_listen({port}, {default})")
61
-
62
64
result = []
63
65
64
66
if (isinstance (port , str ) or isinstance (port , int )):
@@ -91,6 +93,43 @@ def vhost_templates(self, data, defaults):
91
93
display .v (f" = result { result } " )
92
94
return result
93
95
96
+ def vhost_templates_checksum (self , data ):
97
+ """
98
+ """
99
+ result = {}
100
+
101
+ for tpl in data :
102
+ if os .path .exists (tpl ):
103
+ with open (tpl ,"rb" ) as f :
104
+ bytes = f .read ()
105
+ readable_hash = hashlib .sha256 (bytes ).hexdigest ()
106
+ result [tpl ] = readable_hash
107
+
108
+ # display.v(f" = result {result}")
109
+ return result
110
+
111
+ def vhost_templates_validate (self , data , ansible_local ):
112
+ """
113
+ """
114
+ result = {}
115
+ changed_templates = {}
116
+
117
+ for tpl , checksum in data .items ():
118
+ local_checksum = ansible_local .get (tpl , "-" )
119
+
120
+ if checksum != local_checksum :
121
+ changed_templates [tpl ] = checksum
122
+
123
+ changed = len (changed_templates ) > 0
124
+
125
+ result = {
126
+ "changed" : changed ,
127
+ "templates" : changed_templates
128
+ }
129
+
130
+ # display.v(f" = result {result}")
131
+ return result
132
+
94
133
def http_vhosts (self , data , tls = False ):
95
134
"""
96
135
"""
@@ -142,23 +181,8 @@ def certificate_existing(self, data):
142
181
returns a list of vhosts where the certificate exists.
143
182
"""
144
183
# display.v(f"certificate_existing({data})")
145
-
146
184
if isinstance (data , list ):
147
185
data = [x for x in data if x .get ("ssl" , {}).get ("state" ) == "present" ]
148
186
149
187
# display.v(f" = result {data}")
150
188
return data
151
-
152
- def validate_listener (self , data , replace = '(quic|reuseport)' ):
153
- """
154
- """
155
- result = []
156
-
157
- if isinstance (data , str ):
158
- result .append (re .sub (find , replace , s ).strip ())
159
- if isinstance (data , list ):
160
- for i in data :
161
- result .append (re .sub (find , replace , i ).strip ())
162
-
163
- display .v (f" = { result } " )
164
- return result
0 commit comments