0

Placing a debug statement in an Ansible loop causes an error.

Is there a way to display the contents of a register variable while in a loop?

  vars:
    fs_dir:
      - /etc
      - /mnt
      - /bin


  tasks:
    - name: dir loop
      command: "ls {{ item }}"
      register: result
#      debug: msg="{{ result }}"
      loop: "{{ fs_dir }}"
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
jer826
  • 11
  • 1

1 Answers1

0

Q: "Is there a way to display the contents of a register variable while in a loop?"

A: No. There is no way to display the contents of a register variable while in a loop. You can see the registered variable after the loop completes and the task returns the registered data from the remote host to the controller

    - name: dir loop
      command: "ls {{ item }}"
      register: result
      loop: "{{ fs_dir }}"

    - debug:
        var: result

If you want to see any intermediate results of a loop you'll have to use a monitoring tool. Ansible doesn't provide such a tool.

Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • Thanks for your reply Vladimir! Too bad Ansible doesn't allow this. Very limiting. Will research and test saltstack with hopefully better results. – jer826 May 14 '23 at 22:57
  • 1
    Run the task [asynchronously](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_async.html#asynchronous-actions-and-polling) and poll the status if you expect it will take a longer time to complete. This way you manage the resources reasonably instead of keeping open links. In production, you need monitoring anyway - at least centralized logging. Integration of system management with monitoring into one tool would be a mistake. There are tons of examples of failed products that ignored modularity. Btw. modularity is what makes Ansible so successful. – Vladimir Botka May 15 '23 at 00:44
  • @jer826, "_Too bad Ansible doesn't allow this_" because of the nature of the product which is not a Cluster Shell or Terminal Multiplexer, as well Distributed Computing, such functionality is just not implemented yet. It is technically possible, see [How can I show progress for a long-running Ansible task?](https://stackoverflow.com/a/42773566/6771046) and Ansible can be enhanced wich such [Debug info from Ansible Custom Module?](https://stackoverflow.com/a/54448411/6771046), [Provide mechanism for streaming logs from modules](https://github.com/ansible/proposals/issues/92). – U880D May 15 '23 at 06:36
  • @jer826, by using the linked solution approaches one will be able to debug the command output of such as `ls /dir` in a live log manner on the Control Node. – U880D May 15 '23 at 06:40