1

I would like to get output (and print it) of a bash command on RUNTIME (not after the command has finished).

I tried to do sometime like that but it doesn't work (because I can't combine async_status with another module):

- hosts: localhost
  tasks:
    - name: Run an async task
      shell: while true; do echo "test" >> ~/test; done
      async: 1000
      poll: 0
      register: cmd_result

    - name: Print output of above command
      async_status:
        jid: "{{ cmd_result.ansible_job_id }}"
      shell: cat ~/test && truncate -s 0 ~/test
      register: job_result
      until: job_result.finished
      retries: 100
      delay: 10

The last task doesn't work because there are 2 commands async_status and shell but it really shows what I want. It runs a command asynchronously and then it check its status AND cat the logs of the shell command so that we can have the log output on runtime.

Theo Cerutti
  • 779
  • 1
  • 10
  • 33
  • According your description I understand that you like to have [Ansible display realtime shell stdout](https://serverfault.com/a/972664/448950) and implement an [mechanism for streaming logs from modules](https://github.com/ansible/proposals/issues/92). – U880D Jan 31 '23 at 10:18
  • Regarding "_... so that we can have the log output on runtime_" you may have a look into the [linked concept here](https://stackoverflow.com/a/54448411/6771046). – U880D Jan 31 '23 at 10:39
  • Are you OK with considering your question as a duplicate of the linked concept? – U880D Jan 31 '23 at 10:46
  • thanks for your answer, i read it and it seems to be a duplicate – Theo Cerutti Jan 31 '23 at 10:52
  • I don't really know if I will use this custom module because it seems to be too complex for a simple thing. – Theo Cerutti Jan 31 '23 at 10:54
  • As you've might read from the first linked answer, as well from the linked Ansible Issue it is a complex use case and not that simple thing. Unfortunately you've showed only how you like to implemented a solution for what is not described in more in detail, or how you like to resolve a problem for which no information is provided. – U880D Jan 31 '23 at 10:56
  • It might really be streaming log files and/or streaming process output. But that's not something what was Ansible made for. There are other solutions. – U880D Jan 31 '23 at 11:02
  • You may also have a look into a kind of simpler approach or workaround for [How can I show progress for a long-running Ansible task?](https://stackoverflow.com/a/42773566/6771046) and without using Custom Modules. – U880D Jan 31 '23 at 11:20

0 Answers0