marco
marco

Reputation: 111

Printing command stdout on console during runtime

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)?

Upvotes: 2

Views: 8268

Answers (2)

marco
marco

Reputation: 111

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

Upvotes: 1

toydarian
toydarian

Reputation: 4554

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.

Upvotes: 2

Related Questions