2

Sometimes I have to wait very long during running ansible command. For example such command can be executed 30 minutes for my module:

- name: make project
  shell: make -j4 install
  args:
    chdir: "{{ project_dir }}/build"

I would like to see the stdout of this command live, during runtime, not when the command has finished (I see the output when the command has finished, but I need responsiveness, so I'm not interested in something like -v, -vvv or -vvvv). Is it possible to force ansible to print the output during running command (not to buffer it and not to print it at the end)?

toydarian
  • 4,246
  • 5
  • 23
  • 35
marco
  • 111
  • 2
  • 8

2 Answers2

2

You can't print the output while the command runs, but you can print it after the command finished:

- name: make project
  shell: make -j4 install
  args:
    chdir: "{{ project_dir }}/build"
  register: out
  ignore_errors: yes

- name: print output
  debug:
    msg: "{{ out.stdout }}"

- name: fail when make failed
  fail:
    msg: "make task failed"
  when: out.rc != 0

The ignore_errors: yes and the fail-task are there, so the output will get printed before your play fails in case the make-task fails.

You should also consider using the make module instead of running make in a shell.

toydarian
  • 4,246
  • 5
  • 23
  • 35
1

The only solution for given problem I found is to redirect the output of the command to some temporary file. So it will be:

- name: make project
  shell: make -j4 install 2>&1 >/tmp/compile_logs.txt
  args:
    chdir: "{{ project_dir }}/build"

Then I can open in another window:

tail -f /tmp/compile_logs.txt
marco
  • 111
  • 2
  • 8
  • You could also redirect to a network socket like in [How can I show progress for a long-running Ansible task?](https://stackoverflow.com/a/42773566/6771046) or implement a [mechanism for streaming logs from modules](https://github.com/ansible/proposals/issues/92#issuecomment-506572064) or have a look into [Debug info from Ansible Custom Module](https://stackoverflow.com/a/54448411/6771046). – U880D Jul 19 '23 at 16:29