0

I have to set the stage a little first. The application is kafka.

End goal: I want to be able to make kafka config match what I provide in the dictionary (specifically topics). Adding topics to make kafka match is easy. The part I'm having problem is removing topics from kafka if they are not in the dictionary. The only variable I ened to provide kafka is the "topic name" in order to delete.

I can obtain a list of currently added topics from kafka as a variable we will call {{ existing }}. It looks like this:

topic1
topic2
topic3
topic4

My dictionary is as follows:

kafka_topics:
  topic1:
    partitions: 1
    replication_factor: 1
  topic2:
    partitions: 1
    replication_factor: 1

How do I take action on topic3 and topic4 because they are not part of the dict?

At first glance, you just say do a if {{existing}} != {{item.key}} then do action but that does not work because it only checks 1 key at a time. If the types were reversed the list were a dict, dict was a list) you could easily do it by saying if topics contains existing, then do action.

There has GOT to be a better way to do this.

Community
  • 1
  • 1
sharkcus
  • 1
  • 1
  • Possibly duplicates https://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary – Chin Huang Apr 17 '18 at 15:09

1 Answers1

0

Check if each item in existing list is in kafka_topics.keys(). Example:

---
- hosts: localhost
  gather_facts: no
  connection: local
  vars:
    kafka_topics:
      topic1:
        partitions: 1
        replication_factor: 1
      topic2:
        partitions: 1
        replication_factor: 1
    existing:
      - topic1
      - topic2
      - topic3
      - topic4
  tasks:
    - debug:
        msg: "{{ item }} is not in 'kafka_topics'"
      with_items: "{{ existing }}"
      when: item not in kafka_topics.keys()

Gets you:

TASK [debug] *******************************************************************
skipping: [localhost] => (item=None)
skipping: [localhost] => (item=None)
ok: [localhost] => (item=None) => {
    "msg": "topic3 is not in 'kafka_topics'"
}
ok: [localhost] => (item=None) => {
    "msg": "topic4 is not in 'kafka_topics'"
}

Replace debug with the "action" you want.


Alternatively you can use the difference filter on the above data structures.

techraf
  • 64,883
  • 27
  • 193
  • 198