# Run task based on value in register

**URL:** <https://forum.ansible.com/t/run-task-based-on-value-in-register/34184>\
**Category:** Ansible Project\
**Created:** [November 12, 2020, 12:12pm UTC](https://forum.ansible.com/t/run-task-based-on-value-in-register/34184 "2020-11-12T12:12:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [November 12, 2020, 12:12pm UTC](https://forum.ansible.com/t/run-task-based-on-value-in-register/34184/1 "2020-11-12T12:12:31Z")

</div>

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’

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [November 12, 2020, 12:33pm UTC](https://forum.ansible.com/t/run-task-based-on-value-in-register/34184/2 "2020-11-12T12:33:24Z")

</div>

> 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
> \- &quot;\{\{ zookeeper\_port \}\}&quot;
> register: kafka\_topic\_list
> 
> \- name: Debug \- print list of Topics
> debug:
> msg: &quot;\{\{kafka\_topic\_list\.stdout\_lines\}\}&quot;
> 
> ```
> 
> /
> 
> ```
> /\- 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: &quot;\{\{ topic\_list\.stdout\_lines \}\}&quot;
> 
> \- name: Create duplication per topic
> command:
> argv:
> \- &quot;\{\{ srm\_bin\_path \}\}&quot;
> \- topics
> \- \-\-source
> \- &quot;\{\{ kafka\_source \}\}&quot;
> \- \-\-target
> \- &quot;\{\{ kafka\_destination \}\}&quot;
> \- \-\-add
> \- &quot;\{\{ item \}\}&quot;
> with\_items: &quot;\{\{ kafka\_topics\_to\_be\_mirrored \}\}&quot;
> when: &#39;&quot;\{\{ kafka\_topics\_to\_be\_mirrored \}\}&quot; in topic\_list&#39;
> 
> ```

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

when: item in topic\_list

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [November 12, 2020, 1:41pm UTC](https://forum.ansible.com/t/run-task-based-on-value-in-register/34184/3 "2020-11-12T13:41:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [November 12, 2020, 1:54pm UTC](https://forum.ansible.com/t/run-task-based-on-value-in-register/34184/4 "2020-11-12T13:54:37Z")

</div>

> 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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
