Created
October 27, 2014 05:57
-
-
Save saikat/c16dd908603d8e142a08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def remote_log_tailer(host, log_file=''): | |
line_number = 1 | |
inventory_file = "ansible/%s" % env['environment'] | |
while True: | |
output = check_output(["/usr/local/bin/ansible", host, "-i", inventory_file, "-a", "tail -n +%s %s" % (str(line_number), log_file)], env={'ANSIBLE_HOST_KEY_CHECKING' : str(env['check_host_key'])}, stderr=STDOUT) | |
output_lines = output.strip().splitlines() | |
output_lines.pop(0) | |
if len(output_lines) > 0: | |
line_number = line_number + len(output_lines) | |
print bcolors.OKGREEN + '\n'.join(output_lines) + bcolors.ENDC | |
def ansible(host, command): | |
env_var = {} | |
env_var['ANSIBLE_HOST_KEY_CHECKING'] = str(env['check_host_key']) | |
env_var['PYTHONUNBUFFERED'] = '1' | |
log_file = '/var/log/ansible.log' | |
inventory_file = "ansible/%s" % env['environment'] | |
call(["/usr/local/bin/ansible", host, "-i", inventory_file, "-a", "sudo rm -f %s" % log_file], env=env_var) | |
call(["/usr/local/bin/ansible", host, "-i", inventory_file, "-a", "sudo touch %s" % log_file], env=env_var) | |
ansible_command = ["/usr/local/bin/ansible-playbook", "-i", "ansible/%s" % env['environment']] + command + ["--extra-vars", "log_file=%s" % log_file] | |
p = Process(target=remote_log_tailer, args=(host, log_file)) | |
p.daemon = True | |
p.start() | |
call(ansible_command, env=env_var, stderr=STDOUT) | |
p.join(1) | |
# Example usage: | |
# ansible('all', ["ansible/provision.yml", "--timeout=20"]) | |
# Example line in provision.yml | |
# - name: Build {{ application }} Dockerfile | |
# sudo: yes | |
# shell: docker build -t mockingbird/{{ application }} /mockingbird/apps/{{ application }} >> {{ log_file }} 2>&1 | |
# register: docker_build |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment