Jinja2: format + join the items of a list

前端 未结 4 2258
無奈伤痛
無奈伤痛 2021-02-12 12:42

play_hosts is a list of all machines for a play. I want to take these and use something like format() to rewrite them like rabbitmq@%s and

4条回答
  •  孤城傲影
    2021-02-12 13:46

    You can create custom filter

    # /usr/share/ansible/plugins/filter/format_list.py (check filter_plugins path in ansible.cfg)
    
    def format_list(list_, pattern):
        return [pattern % s for s in list_]
    
    
    class FilterModule(object):
        def filters(self):
            return {
                'format_list': format_list,
            }
    

    and use it

    {{ play_hosts | format_list('rabbitmq@%s') }}
    

提交回复
热议问题