# Async start a process and check until its listen port condition is successful

**URL:** <https://forum.ansible.com/t/async-start-a-process-and-check-until-its-listen-port-condition-is-successful/36757>\
**Category:** Ansible Project\
**Created:** [June 20, 2022, 4:03pm UTC](https://forum.ansible.com/t/async-start-a-process-and-check-until-its-listen-port-condition-is-successful/36757 "2022-06-20T16:03:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [June 20, 2022, 4:03pm UTC](https://forum.ansible.com/t/async-start-a-process-and-check-until-its-listen-port-condition-is-successful/36757/1 "2022-06-20T16:03:34Z")

</div>

I trigger multiple tomcat startup scripts and then need to check if all process listens on their specific port across multiple hosts in the quickest time possible.

For the test case, I m writing 3 scripts instead of tomcat scripts that runs on a single host and listen on ports 4443,4445,4447 respectively as below.

cat /tmp/startapp1.sh

while test 1 # infinite loop  
sleep 10  
do  
nc -l localhost 4443 \> /tmp/app1.log  
done

cat /tmp/startapp2.sh

while test 1 # infinite loop  
sleep 30  
do  
nc -l localhost 4445 \> /tmp/app2.log  
done

cat /tmp/startapp3.sh

while test 1 # infinite loop  
sleep 20  
do  
nc -l localhost 4447 \> /tmp/app3.log  
done  
Below is my code to trigger the script and check if the telnet is successful:

cat main.yml

- include\_tasks: “internal.yml”  
loop:

- /tmp/startapp1.sh 4443

- /tmp/startapp2.sh 4445

- /tmp/startapp3.sh 4447  
cat internal.yml

- shell: “{{ item.split()[0] }}”  
async: 600  
poll: 0

- name: DEBUG CHECK TELNET  
shell: “telnet {{ item.split()[1] }}”  
delegate\_to: localhost  
register: telnetcheck  
until: telnetcheck.rc == 0  
async: 600  
poll: 0  
delay: 6  
retries: 10

- name: Result of TELNET  
async\_status:  
jid: “{{ item.ansible\_job\_id }}”  
register: \_jobs  
until: \_jobs.finished  
delay: 6  
retries: 10  
with\_items: “{{ telnetcheck.results }}”

Expectation: The above three scripts should start along with telnet check in about 30 seconds.

Thus, the basic check that needs to be done here is telnet until: telnetcheck.rc == 0 but due to async the telnet shell module does not have entries for rc and hence I get the below error:

“msg”: “The conditional check ‘telnetcheck.rc == 0’ failed. The error was: error while evaluating conditional (telnetcheck.rc == 0): ‘dict object’ has no attribute ‘rc’”

In the above code where and how can I check if telnet had succeeded i.e telnetcheck.rc == 0 and make sure the Expectation is met?

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [June 20, 2022, 5:41pm UTC](https://forum.ansible.com/t/async-start-a-process-and-check-until-its-listen-port-condition-is-successful/36757/2 "2022-06-20T17:41:36Z")

</div>

What is the reason to use shell + telnet why the wait\_for module has  
such functionality?  
This does what (I think!) you want:

1. run ncat (not nc) on 3 ports on the target system:

dnmvisser@villa:~$ for i in 4443 4445 4447; do ncat -l -k -p $i & done  
[1] 253485  
[2] 253486  
[3] 253487

Consider this playbook "waitfor.yml":

---

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [June 20, 2022, 7:05pm UTC](https://forum.ansible.com/t/async-start-a-process-and-check-until-its-listen-port-condition-is-successful/36757/3 "2022-06-20T19:05:54Z")

</div>

@Dick under ` - name: Starting multiple wait_for tasks` → i do not have a loop ` loop: "{{ ports }}"` as my loop is in `main.yml` outer yml

Thus, i get the following error:

TASK [Collecting status of wait\_for tasks] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /root/newinternal.yml:17  
fatal: [localhost]: FAILED! =\> {  
“msg”: “‘dict object’ has no attribute ‘results’”  
}

Can you please suggest?

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [June 20, 2022, 7:12pm UTC](https://forum.ansible.com/t/async-start-a-process-and-check-until-its-listen-port-condition-is-successful/36757/4 "2022-06-20T19:12:01Z")

</div>

I can't do everything for you.  
Look at the tasks/logic of the (fully working) playbook that I  
provided and adapt it to your situation.

---

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [June 21, 2022, 7:56am UTC](https://forum.ansible.com/t/async-start-a-process-and-check-until-its-listen-port-condition-is-successful/36757/5 "2022-06-21T07:56:11Z")

</div>

@Dick your solution takes 64 seconds for starting and checking for successful telnet if each of the three scripts has 30 seconds sleep time.

With the below approach, it takes only half the time i.e 35 seconds to complete everything.

cat main.yml
