Run task based on value in register

I have created a playbook that should run the following scenario – I’m having a problem with the second tasks

  1. Query the source Kafka for a topic list and save the output in register

  2. Run topics creation as a loop on destination kafka

a. Topics list is taken from the input file

b. The topic will be created only if the topic exists in the source kafka

Playbook tasks explanation – It seems that task 6 is been skipped

Host: source Kafka

  1. Task 1 : connect to source Kafka and get a list of topic à update register

  2. Task 2: print topic list – debug

  3. Task 3 : Copy register output to a file

Host: destination kafka

  1. Task 4 : upload topic list from the file to a register

  2. Task 5: print register

  3. Task 6: create topic per input list, only if topic name exists in register * If this can be done as a search in file it will be much better (will save task 4)

## Verification

  • hosts:

  • kafka_source_master_server[0]
    become: true
    any_errors_fatal: true
    gather_facts: False
    vars:
    zookeeper_port: 2999
    tasks:

  • name: list topic from source kafka
    command:
    argv:

  • kafka-topics

  • –list

  • –zookeeper

  • localhost

  • “{{ zookeeper_port }}”
    register: kafka_topic_list

  • name: Debug - print list of Topics
    debug:
    msg: “{{kafka_topic_list.stdout_lines}}”

  • name: write results to a file
    local_action: copy content={{ kafka_topic_list.stdout_lines }} dest=/tmp/topic-list.txt

  • hosts:

  • bigdata_kafka_master[0]
    become: true
    any_errors_fatal: true
    gather_facts: False
    vars_files:

  • ./environment.yml

  • ./kafka_enviroment.yml
    vars:
    zookeeper_port: 2999
    srm_bin_path: /opt/cloudera/parcels/STREAMS_REPLICATION_MANAGER/bin/srm-control
    tasks:

  • name: Update register with topic list
    shell: cat /tmp/topic-list.txt
    register: topic_list
    delegate_to: localhost

  • name: print topics
    debug:
    msg: “{{ topic_list.stdout_lines }}”

  • name: Create duplication per topic
    command:
    argv:

  • “{{ srm_bin_path }}”

  • topics

  • –source

  • “{{ kafka_source }}”

  • –target

  • “{{ kafka_destination }}”

  • –add

  • “{{ item }}”
    with_items: “{{ kafka_topics_to_be_mirrored }}”
    when: ‘“{{ kafka_topics_to_be_mirrored }}” in topic_list’

I have created a playbook that should run the following scenario – I’m having a problem with the second tasks

1. Query the source Kafka for a topic list and save the output in register

2. Run topics creation as a loop on destination kafka

a. Topics list is taken from the input file

b. The topic will be created only if the topic exists in the source kafka

Playbook tasks explanation – It seems that task 6 is been skipped

Host: source Kafka

1. Task 1 : connect to source Kafka and get a list of topic à update register

2. Task 2: print topic list – debug

3. Task 3 : Copy register output to a file

Host: destination kafka

4. Task 4 : upload topic list from the file to a register

5. Task 5: print register

6. Task 6: create topic per input list, only if topic name exists in register * If this can be done as a search
in file it will be much better (will save task 4)

/## Verification
/- hosts:
- kafka_source_master_server[0]
become: true
any_errors_fatal: true
gather_facts: False
vars:
zookeeper_port: 2999
tasks:

\- name: list topic from source kafka
  command:
    argv:
      \- kafka\-topics
      \- \-\-list
      \- \-\-zookeeper
      \- localhost
      \- "\{\{ zookeeper\_port \}\}"
  register: kafka\_topic\_list

\- name: Debug \- print list of Topics
  debug:
    msg: "\{\{kafka\_topic\_list\.stdout\_lines\}\}"

/

/\- name: write results to a file
  local\_action: copy content=\{\{ kafka\_topic\_list\.stdout\_lines \}\} dest=/tmp/topic\-list\.txt

- hosts:
- bigdata_kafka_master[0]
become: true
any_errors_fatal: true
gather_facts: False
vars_files:
- ./environment.yml
- ./kafka_enviroment.yml
vars:
zookeeper_port: 2999
srm_bin_path: /opt/cloudera/parcels/STREAMS_REPLICATION_MANAGER/bin/srm-control
tasks:

\- name: Update register with topic list
  shell: cat /tmp/topic\-list\.txt
  register: topic\_list
  delegate\_to: localhost

\- name: print topics
  debug:
    msg: "\{\{ topic\_list\.stdout\_lines \}\}"

\- name: Create duplication per topic
  command:
    argv:
      \- "\{\{ srm\_bin\_path \}\}"
      \- topics
      \- \-\-source
      \- "\{\{ kafka\_source \}\}"
      \- \-\-target
      \- "\{\{ kafka\_destination \}\}"
      \- \-\-add
      \- "\{\{ item \}\}"
  with\_items: "\{\{ kafka\_topics\_to\_be\_mirrored \}\}"
  when: '"\{\{ kafka\_topics\_to\_be\_mirrored \}\}" in topic\_list'

In a loop when is evaluated for *every* item, so I think that might solve your problem:

when: item in topic_list

Regards
        Racke

It looks that the item is not been searched in teh register - Maybe I need to implement a a regex search? if so how?

It looks that the item is not been searched in teh register - Maybe I need to implement a a regex search? if so how?

Well you didn't show what is in the register and what is in kafka_topics_to_be_mirrored.

Regards
         Racke