Search code examples
ansible

Is it possible to debug multiple variables in one Ansible task without using a loop?


I want to print var1 and var2 in one Ansible task. I have this working YAML.

- debug:
    var: "{{ item }}"
  with_items:
    - var1
    - var2

I wonder whether is possible to do it without using with_items nor msg parameter.


Solution

  • You can definitely have multiple variables in a debug message, as long as it is a valid YAML.

    For example, the task

    - debug:
        msg:
          var1: "{{ var1 }}"
          var2: "{{ var2 }}"
      vars:
        var1: foo
        var2: bar
    

    Yields

    ok: [localhost] => 
      msg:
        var1: foo
        var2: bar
    

    And if you really don't want a message, drop the two variables in a dictionary:

    - debug:
        var: to_debug
      vars:
        to_debug:
          var1: "{{ var1 }}"
          var2: "{{ var2 }}"
    
        var1: foo
        var2: bar
    

    Yields

    ok: [localhost] => 
      to_debug:
        var1: foo
        var2: bar