ansible shell传递命令而不执行它

分享于 

3分钟阅读

互联网

  繁體

问题:

我使用ansible通过命令在一些节点上执行脚本(myscript.sh ):


- 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



为了在shell (取决于node )上给出输出消息,脚本的执行需要大约两秒或更少的时间,我使用out.stdout_lines进行调试,以确保执行的状态。

某些节点作为输出提供以下正确的节点;


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"


 ]


}



但是,有些其他节点已经传递了命令,没有执行它;


ok: [node2] => {


 "out.stdout_lines": []


}



我已经通过使用下面的playbook ( added sleep )成功地解决了这个问题--现在所有节点都给出了所需的输出消息(子进程已成功启动);


- 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



有没有办法等待来自节点的响应消息?


答案1:


 shell: "sh {{ mongodPath }}/myScript.sh && sleep 5s"



这将执行myscript.sh,然后等待它结束,然后等待5秒,你可以尝试,


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



第二个命令将等待myscript.sh成功完成(返回0),如果不存在日志,则它显示脚本日志,



COM  COMM  Comma  EXE  Shell  EXEC  
相关文章