Ansible:为生产者实现映射算法到消费者

时间:2017-07-14 06:37:13

标签: ansible jinja2 ansible-2.x ansible-template

所以,情景是我有生产者和消费者的比例为7:1 ,我希望有一个一致且确定的多重映射,而不是生产者和消费者在我的服务中。配置中为每个生产者提供了消费者列表,这是通过ansible完成的。因此,我尝试在ansible 本身中实现映射逻辑,而不是传递整个消费者列表,并在生产者服务中进行。因此,我考虑使用自定义过滤器从过滤器列表中过滤掉,并将其分配给生产者。下面是我写的自定义过滤器:

#!/usr/bin/python

class FilterModule(object):

    def filters(self):
        return { 'map_producer_to_consumer': self.map_producer_to_consumer }

    # consumer_servers: complete list of consumers servers
    # producer_id: provided to each producer for mapping purpose
    # producer_count: total no. of producers
    # map_consumer_count: no. of consumers need to be mapped to each producer
    # consumer_count: total no. of consumers

    def map_producer_to_consumer(self, consumer_servers, producer_id, producer_count, map_consumer_count):
        consumer_count = len(consumer_servers)
        index_offset = 0 if  producer_count%consumer_count else 1
        rotation_count = (producer_id/consumer_count) % (map_consumer_count-1) # used for left rotation of mapped servers
        map_consumer_indexes = [ (producer_count*i  + producer_id + index_offset*i) % consumer_count for i in xrange(map_consumer_count)]
        mapped_consumer_servers = [consumer_servers[map_consumer_indexes[0]]]
        for i in xrange(1, map_consumer_count):
            index = (i + rotation_count) % map_consumer_count
            if i + rotation_count >= map_consumer_count:
                mapped_consumer_servers.append( consumer_servers[map_consumer_indexes[index] + 1] )
            else:
                mapped_consumer_servers.append( consumer_servers[map_consumer_indexes[index]] )
        return (',').join(mapped_consumer_servers) 

当使用这样的静态参数时,此过滤器正在按预期工作:

"{{ tsdb_boxes | map_producer_to_consumer(2,3,3) }}"

但我想通过jinja2模板使用动态参数,例如:

"{{ groups['producers'] | map_producer_to_consumer ({{ consumer_servers }}, {{ producer_id }}, {{ producer_count }}, {{ map_consumer_count }}) }}"

但由于变量嵌套导致错误,这在Jinja2中是不允许的。如果我尝试这样的事情:

"{{ groups['producers'] }} | map_producer_to_consumer ({{ consumer_servers }}, {{ producer_id }}, {{ producer_count }}, {{ map_consumer_count }})"

它导致打印出这样的字符串:

['ip-1', 'ip-2'...] | map_producer_to_consumer (1000, 10, 150, 3)

有人可以建议什么应该是实现这一目标的最佳方式。我应该使用脚本模块并在bash中转换逻辑,否则最好将其保留在服务中。

1 个答案:

答案 0 :(得分:1)

评论回答:

  

为什么不试试{{ groups['producers'] | map_producer_to_consumer(consumer_servers, producer_id, producer_count, map_consumer_count) }}

来自@techraf的link关于嵌套。