且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Jinja2 模板创建一个字符串

更新时间:2023-11-15 12:01:34

它有点脏,但为了回答问题,下面的代码段应该适用于您所描述的内容.一个问题是您没有指定当 attr1 或任何其他 attr 列表中有多个项目时会发生什么.如果每个列表中只有一项,此代码段将起作用.

Its a bit dirty way but for the sake of answer the snippet below should work for what you have described. One problem is that you have not specified that what will happen when there are more than one items in attr1 or any other attr list. This snippet will work if there is only one item in each list.

- set_fact:
    default_attr:
        attr1    :
        - "1"
        nexatt  :
        - "b"
- set_fact: app_attributes="{{ default_attr | to_json | regex_replace('\:\ ','=') | regex_replace('[\[\]{}\"]') }}"
- debug: var=app_attributes