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

3

7

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.

Ithnan answered 13/3, 2023 at 16:23 Comment(0)
F
3

I find this to be very handy:

- name: Display variables
  ansible.builtin.debug:
    msg:
      - "x: {{ x }}"
      - "y: {{ y }}"
Fiducial answered 2/7, 2024 at 10:41 Comment(0)
D
10

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
Dragelin answered 13/3, 2023 at 16:50 Comment(0)
F
3

I find this to be very handy:

- name: Display variables
  ansible.builtin.debug:
    msg:
      - "x: {{ x }}"
      - "y: {{ y }}"
Fiducial answered 2/7, 2024 at 10:41 Comment(0)
D
-5

You can use the vars option in the debug task to print the values of multiple variables in one go. Here's an example:

- debug:
    vars:
      var1: "{{ var1 }}"
      var2: "{{ var2 }}"
Darton answered 13/3, 2023 at 16:27 Comment(1)
Unfortunately no. Using that syntax will result into an error fatal: [localhost]: FAILED! => msg: 'Invalid options for debug: vars'. Neither the docu debug module – Print statements during execution nor the Action Plugin source plugins/action/debug.py show such parameter and syntax. Are you mixing it up with Using variables?Tails

© 2022 - 2025 — McMap. All rights reserved.