How should this Ansible variable be defined?

2020-05-06 11:38发布

I am trying to make sense of a variable reference I found in an incomplete Ansible role. The role references a value using

dest: “{{params['box'].t1}}”

In a separate yaml file I have

box:
    t1: "Albany"
    t2: "Albuquerque"

params isn't defined, so obviously this isn't going to work, but I can't figure out the correct way to define it. Can someone tell me where (or how) params must be defined for this variable reference to work in Ansible?


Related questions. Does the use of square brackets in dest: “{{params['box'].t1}}” indicate that it is a dictionary? If yes, could I also write this as dest: “{{params['box']['t1']}” or dest: “{{params.box.t1}”?

标签: ansible
1条回答
一夜七次
2楼-- · 2020-05-06 11:49

params['box'].t1 refers to Albany in:

params:
  box:
    t1: "Albany"
    t2: "Albuquerque"

It is the same as params.box.t1 and params['box']['t1'].

Brackets refer to a key name, so they imply it is a dictionary.


You typically use square bracket-notation when you want to refer to a key via a variable:

vars:
  wanted_key: box
  params:
    box:
      t1: Albany
    other:
      t1: Albuquerque

Then params[wanted_key].t1 refers to Albany.

In your example the value inside square brackets is a string (quoted), so all above examples are equivalent.

查看更多
登录 后发表回答