Skip to content

Instantly share code, notes, and snippets.

@saikat
Created October 27, 2014 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saikat/c16dd908603d8e142a08 to your computer and use it in GitHub Desktop.
Save saikat/c16dd908603d8e142a08 to your computer and use it in GitHub Desktop.
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