We are running a python script via ansible. The script takes around 45 mins to complete and the logging is in verbose mode. But the issue is we get to know the result only after 45 minutes or if the script fails.
- name: Run a python script
shell: |
cd /tmp/
python {{script_name}} {{branch}} {{ script_action_arg_test }} >> output.txt
I have tried the above approach, the python process keeps running but does not log anything to output.txt Is there a way to capture the verbose logs of the script in ansible itself?
OK -- it's ugly. BUT, Ansible is for automation -- fire and forget, and is not designed for people to see what's going on in real time.
Now, use ansible-galaxy init my-cool-role in your roles directory to create the role. Then, these are the files you will put in the role:
tasks/main.yml:
---
- name: Copy script to remote host
copy:
src: long_script.sh
dest: long_script.sh
mode: 0755
- name: Run script in async
shell: ./long_script.sh > long_script.out
async: 1000
poll: 0
register: long_task
- name: Include monitoring tasks
include_tasks: monitor.yml
loop: "{{ range(0, 100, 1)|list }}"
tasks/monitor.yml
---
- set_fact:
done: "{{ 'DONE' == tail.stdout_lines[9] | default(false) }}"
- debug:
var: tail.stdout_lines[9]
- debug:
var: done
- meta: end_play
when: done|bool
- name: Get last ten lines
shell: sleep 5; tail long_script.out
register: tail
- name: Show lines
debug:
var: tail.stdout_lines
files/long_script.sh:
#!/bin/bash
for (( ii=0; ii<50; ii++ )); do
echo -n "$ii: "
date
sleep 1
done
echo DONE
This assumes that the long task is the last thing you're doing, because it ends the play. Note that you have to have enough loops to cover the expected time it il take the task to run. If you need to do something after this, just put another play in the playbook. Good luck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With