0

I have a build server which runs ansible playbooks, after each commit to the given playbook repository. The host data comes from a dynamic inventory. The problem I am encountering right now is that when I push several commits, the build servers runs the ansible-playbook for the same host after another and it creates a race condition on the host server. How can I make the ansible wait for the running playbook to finish and then start or simply not run when there is a playbook running on the host? (I want only one instance of ansible running in the host.)

Pouyan
  • 499
  • 1
  • 5
  • 14
  • How are you triggering the build server to run ansible playbooks? – kfreezy Jul 27 '17 at 15:16
  • @kfreezy I use [Phabricator](https://secure.phabricator.com/book/phabricator/article/harbormaster/#triggering-builds) , and you can trigger builds on commits. The problem is when you push multiple commits at the same time. Is there a way to make sure that the only one instance of ansible is running? – Pouyan Jul 27 '17 at 15:27
  • You can write wrapper shell script for ansible-playbook or modify ansible-playbook python file to check for other processes, but it seems like an awful solution. You'd better setup some build queue on your CD server. – Konstantin Suvorov Jul 27 '17 at 20:01
  • @KonstantinSuvorov I have thought the same about wrapping ansible-playbook. But the queue seems to be a good solution untill the fact that the host is written in the playbook and I have no idea from outside which hosts are written there so when I use the queue, every playbook for every host should wait and this make the build server very slow for the legitimate builds. – Pouyan Jul 27 '17 at 20:18

1 Answers1

1

You can check the ansible pid of the remote server before launching a playbook run. What OS/distro are you using? Hmm.. depending of what you are testing, you can spin up a container(docker or lxd) to test your environment for each commit. Take in account that if you creating/modifying network interfaces or creating device files, it won't suite your needs. I use this method to test my playbook roles in just one server having several unit tests for each role param.

clvx
  • 318
  • 1
  • 2
  • 8
  • We have SuSE running. Using Containers is not an option for our systems. Is there another way to find out that ansible is running on the remote host? I found [this](https://stackoverflow.com/questions/35176944/) but it seems like none of the answer were useful. – Pouyan Jul 27 '17 at 20:25
  • If containers is not what you want, you can always create a mount namespace and pivot_root to that namespace. The idea of a container is just separating namespaces. You still can achieve the same using just namespaces(7). Your kernel needs to be >=3.11 though. Anyways, ps awfx | grep ansible, or check the pid file?. – clvx Jul 28 '17 at 15:04