Chris F
Chris F

Reputation: 16673

In Ansible, how can I show the variable name in a debug module?

Say I have a defaults/main.yml file that has

---
var1: my_value1
var2: my_value2

Now I want to print my vars in a debug

---
- debug: ??? what to put here ???
  with_items:
  - "{{ var1 }}"
  - "{{ var2 }}"

So I show

The value of var1 is my_value1
The value of var2 is my_value2

var and msg only show the value, not the variable name? This seems so simple but I can't find in in the Ansible docs.

Upvotes: 0

Views: 4333

Answers (2)

Chris F
Chris F

Reputation: 16673

This worked for me. It's not quite the message I wanted, but it at least shows me the item name and its value

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

and I get

TASK [python3 : debug] *********************************************************
ok: [localhost] => (item=var1) => {
    "item": "var1",
    "var1": "my_value1"
}
ok: [localhost] => (item=var2) => {
    "var2": "my_value2",
    "item": "var2"
}

Upvotes: 0

victor gallet
victor gallet

Reputation: 1898

You can use with_dict to show var names but you have to define your variables differently :

---
vars:
 var1: 
   my_value1
 var2: 
   my_value2

And then you can do :

---
debug: 
 msg: "The value of {{item.key}} is {{item.value}}"
with_dict: "{{ vars }}"

Upvotes: 2

Related Questions