Theppasin Kongsakda
Theppasin Kongsakda

Reputation: 207

Ansible: How to get variable name from variable

I want to use variable name and data in variable at the same time. Like this..

- vars: 
    my_var: 
      attr1: Hello world
      attr2: yes
  debug: 
    msg: "This is variable name > my_var. And this is data inside `attr1` > {{ my_var.attr1 }}"

How do I get variable name from variable.

Upvotes: 1

Views: 730

Answers (1)

Zeitounator
Zeitounator

Reputation: 44615

You can use the vars lookup

- vars:
    var_name: my_var 
    my_var: 
      attr1: Hello world
      attr2: yes
  debug: 
    msg: "This is variable name > {{ var_name }}. And this is data inside `attr1` > {{ lookup('vars', var_name).attr1 }}"

Upvotes: 1

Related Questions