@@ -21,41 +21,44 @@ def save_config_file(data, path):
2121 with open (path , 'w' ) as f :
2222 json .dump (data , f , indent = 2 , ensure_ascii = False )
2323
24- def add_sharding (config , modulus , hash_value ):
24+ def add_sharding (config , modulus , hash_value , ignorances ):
2525 for job in config .get ('scrape_configs' , []):
26- job ['relabel_configs' ] = [
27- {
28- 'source_labels' : ['__address__' ],
29- 'modulus' : modulus ,
30- 'target_label' : '__tmp_hash__' ,
31- 'action' : 'hashmod'
32- },
33- {
34- 'source_labels' : ['__tmp_hash__' ],
35- 'regex' : hash_value ,
36- 'action' : 'keep'
37- }
38- ]
26+ if job .get ('job_name' , '' ) not in ignorances :
27+ job ['relabel_configs' ].extend ([
28+ {
29+ 'source_labels' : ['__address__' ],
30+ 'modulus' : modulus ,
31+ 'target_label' : '__tmp_hash__' ,
32+ 'action' : 'hashmod'
33+ },
34+ {
35+ 'source_labels' : ['__tmp_hash__' ],
36+ 'regex' : hash_value ,
37+ 'action' : 'keep'
38+ }
39+ ])
3940 return config
4041
4142def main ():
4243 module = AnsibleModule (
4344 argument_spec = dict (
4445 source = dict (type = 'str' , required = True ),
4546 modulus = dict (type = 'int' , required = True ),
46- hash_value = dict (type = 'int' , required = True )
47+ hash_value = dict (type = 'int' , required = True ),
48+ ignorances = list (type = 'str' , required = True )
4749 )
4850 )
4951
5052 source = module .params ['source' ]
5153 modulus = module .params ['modulus' ]
5254 hash_value = module .params ['hash_value' ]
55+ ignorances = module .params ['ignorances' ]
5356
5457 config = load_config_file (source )
5558 if not isinstance (config , dict ):
5659 module .exit_json (changed = False )
5760 return
58- sharded_config = add_sharding (config , modulus , hash_value )
61+ sharded_config = add_sharding (config , modulus , hash_value , ignorances )
5962 save_config_file (sharded_config , source )
6063
6164 module .exit_json (changed = True )
0 commit comments