从Ansible中的字典创建逗号分隔的字符串

时间:2018-09-03 06:59:16

标签: ansible jinja2

我想编写一个Ansible角色,以便能够alter给定的Kafka主题。我正在使用键/值对的字典。

然后,command模块用于执行一个Kafka脚本,该脚本采用一串用逗号分隔的值。例如,使用app_kafka_topic列表:

---
app_kafka_topic:
    cleanup.policy      :
    - "delete"
    retention.ms        :
    - "146800000"
    partitions          :
    - "6"
    replication-factor  :
    - "2"

并创建string

“ cleanup.policy =删除,retention.ms = 146800000,分区= 6,复制因子= 2”

这是我到目前为止所拥有的。

- name: Reading the Default Topic Properties
  set_fact:
    app_kafka_topic_properties_dicts: |
        {% set res = [] -%}
        {% for key in app_kafka_topic.keys() -%}
           {% for value in app_kafka_topic[key] -%}
            {% set ignored = res.extend([{'topic_property': key, 'value':value}]) -%}
           {%- endfor %}
        {%- endfor %}
        {{ res }}

- name: Create Topic with Default Properties
  command: "{{ kafka_bin_dir }}/{{ kafka_config_script }}
            --zookeeper {{ prefix }}-kafka-{{ Kafka_node }}.{{ DNSDomain}}:{{ zookeeper_port }}
            --entity-type topics
            --alter
            --entity-name {{ kafka_topic }}
            --add-config
            {{ properties }}"
  with_items: "{{ app_kafka_topic_properties_dicts }}"
  register: createdTopic
  vars:
    properties: |-
      {% for key in app_kafka_topic.keys() %}
      {% for value in app_kafka_topic[key] %}
       "{{ key }}={{ value }}"
      {%- endfor %}
      {%- endfor %}

但是,properties变量未将值连接到字符串的末尾。有没有办法将值附加到字符串并用逗号分隔?

1 个答案:

答案 0 :(得分:1)

这是您要查找的代码吗?

play.yml

0.001404 0.013153

$ ansible-playbook play.yml | grep字符串

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    ofstream fact("factorial.txt");
    int i,n,f=1;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        f=f*i;
        fact<<i<<" ";
    }
    fact<<endl;
    fact<<f<<" ";

    return 0 ;
}