6

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?

Roy
  • 4,376
  • 4
  • 36
  • 53

1 Answers1

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 )

Joshua Grigonis
  • 157
  • 1
  • 9
Pablo Martinez
  • 2,406
  • 17
  • 13
  • Should work, by the look of it, though I still need a way to check with every relevant machine and work out the combined state before I can decide to start a new process. It might be simpler to solve this outside of the automation tools, although I would have thougt that with Ansibles suite of node discovery, dynamic inventory and cluster management features there would be a pretty straight forward way of doing this. – Roy Dec 16 '15 at 01:46
  • So running the same play twice could lead to having the service started on two nodes? – Henrik Pingel Feb 17 '16 at 09:07