I'm going to use Ansible script to create Kafka topics. I define a YAML Hash var for topics to create as:
topics:
topic1:
partitions: 1
replication_factor: 2
topic2:
partitions: 1
replication_factor: 2
Before create them, I need first check if a topic has existed. I can use kafka-topics.sh --list --zookeeper localhost:2181
to get the list of existing topic names, then compare with topics
hash to get a list of topics to create, then call kafka-topics.sh --create --topic name
to create topics.
How to implement this?