Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ansible run command on remote host in background

I am trying to start filebeat (or for that matter any other process which will run continuously on demand) process on multiple hosts using ansible. I don't want ansible to wait till the process keeps on running. I want ansible to fire and forget and come out and keep remote process running in back ground. I've tried using below options:

    ---     - hosts: filebeat       tasks:       - name: start filebeat option a)  command: filebeat -c filebeat.yml & option b)  command: nohup filebeat -c filebeat.yml & option c)  shell: filebeat -c filebeat.yml &            async: 0 //Tried without as well. If its > 0 then it only waits for that much of time and terminates the filebeat process on remote host and comes out.            poll: 0 
like image 964
Mrunal Gosar Avatar asked Sep 06 '16 10:09

Mrunal Gosar


People also ask

How do I run Ansible background?

You can leverage the Linux shell to background the task. Run the playbook command then hit ctrl+z to background it. Type fg to bring it back. Or you can find ansible-playbook ... & to run it in the background.

How do I run a parallel task in Ansible?

If you want to run multiple tasks in a playbook concurrently, use async with poll set to 0. When you set poll: 0 , Ansible starts the task and immediately moves on to the next task without waiting for a result. Each async task runs until it either completes, fails or times out (runs longer than its async value).

Should Ansible run as root?

Note: Ansible does not require root access; however, if you choose to use a non-root user, you must configure the appropriate sudo permissions for the tasks you want to accomplish. You will be prompted for the root password for servera, which will allow your SSH key to be installed on the remote host.


1 Answers

Simplified answer from link I mentioned in the comment:

--- - hosts: centos-target   gather_facts: no   tasks:     - shell: "(cd /; python -mSimpleHTTPServer >/dev/null 2>&1 &)"       async: 10       poll: 0 

Note subshell parentheses.

Update: actually, you should be fine without async, just don't forget to redirect stdout:

- name: start simple http server in background   shell: cd /tmp/www; nohup python -mSimpleHTTPServer </dev/null >/dev/null 2>&1 & 
like image 62
Konstantin Suvorov Avatar answered Sep 20 '22 07:09

Konstantin Suvorov