Skip to content

Instantly share code, notes, and snippets.

@inabhi9
Last active July 22, 2016 12:49
Show Gist options
  • Save inabhi9/9da3ac8d752f57f162cf7c3b80d450c8 to your computer and use it in GitHub Desktop.
Save inabhi9/9da3ac8d752f57f162cf7c3b80d450c8 to your computer and use it in GitHub Desktop.
wrapper to find Ansible project's root path. Useful when you can't change working directory to run playbook
#!/usr/bin/python
import sys
import os
from subprocess import call
ansible_args = sys.argv[1:]
# Finding playbook file arg
try:
ansible_playbook = [arg for arg in ansible_args if '.yml' in arg][0]
except IndexError:
call(['ansible-playbook'] + ansible_args)
os._exit(1)
playbook_dir = os.path.dirname(ansible_playbook)
current_search_path = playbook_dir
working_dir = None
while current_search_path != '/':
if os.path.isfile(current_search_path + '/.projectroot'):
working_dir = current_search_path
break;
current_search_path = os.path.abspath(current_search_path + '/../')
# Changing working directory if we found working_dir
if working_dir is not None: os.chdir(working_dir)
call(['ansible-playbook'] + ansible_args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment