0

I am using ansible for the execution of a script (myScript.sh) over some nodes through the command:

- hosts: primaryNodes,!node13
  vars:
    mongodPath: /usr/local/mongodb
  become: yes
  become_user: user
  tasks:
  - name: starting mongod on all shards
    shell: "sh {{ mongodPath }}/myScript.sh"
    register: out
  - debug: var=out.stdout_lines

The execution of the script may need around 2-3 seconds or maybe less in order to give an output message on the shell (depending on the node). I am using the out.stdout_lines for debugging purposes, to be sure about the status of the execution.

Some nodes give as an output the following one which is correct;

ok: [node16] => {
    "out.stdout_lines": [
        "about to fork child process, waiting until server is ready for connections.", 
        "forked process: 2837", 
        "child process started successfully, parent exiting"
    ]
}

But some other nodes, seem to have passed the command, without executing it;

ok: [node2] => {
    "out.stdout_lines": []
}

I have managed to solve this by using the following playbook (added sleep) - all the nodes now give the desired output message (child process started successfully);

- hosts: primaryNodes,!node13
  vars:
    mongodPath: /usr/local/mongodb
  become: yes
  become_user: user
  tasks:
  - name: starting mongod on all shards
    shell: "sh {{ mongodPath }}/myScript.sh && sleep 5s"
    register: out
  - debug: var=out.stdout_lines

I know that this is a rough solution for my case, but is there a way to wait for a responding message from the node?

2
  • Why do you call sh? This is already run with a shell! Oct 26 '20 at 13:08
  • if you debug: var=out then you will see the return code and std_err, as it's possible your script failed on something else
    – Tom
    Oct 26 '20 at 18:16
0
  shell: "sh {{ mongodPath }}/myScript.sh && sleep 5s"

This will execute myscript.sh, then wait for it to end, then wait 5 seconds, you could try something like:

  shell: "{{ mongodPath }}/myScript.sh && tail -n 10 {{ mongodPath }}/myScript.log"

The second command will wait for the myScript.sh to finish succesfully (return 0), then it will show the script log, if there is no log then your first command didn't work correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.