Ansible Playbook: Ensure a process is running on any one node of a cluster?

Ansible makes it easy enough to ensure that a process is running on every host.

I could do something like:

---
- hosts: app_cluster
  tasks: 

  - name: Look for the "foo" process
    shell: ps -ef |  grep foo | grep -v grep
    register: process_list
    changed_when: false  

  - name: Start "foo" if needed
    shell: nohup /bin/foo &
    when: "process_list.stdout.find('foo') == -1"  

However, I need to have exactly one instance of a certain process across the cluster. Ie. it can run on any host, just as long as it is running somewhere and so long as there is only one such process anywhere in the cluster.

Might there be a convenient way to do this in an ansible playbook?


Solution 1:

You may use the run_once parameter as described in http://docs.ansible.com/ansible/playbooks_delegation.html#run-once so the task will only run in the first host of the batch. Take into consideration that you cannot specify the order but it's somehow "predictable" (more info in https://github.com/ansible/ansible/issues/10964 )