Score:0

how to refer to a variable in another host in static inventory host var file?

de flag

I have an inventory like following:

inventory/
├── group_vars
│   └── all.yml
├── host_vars
│   ├── serverC.yml
│   ├── master02.yml
│   └── master01.yml
└── hosts.yml

I knew I can dynamically access other host's variable via hostvars[otherhost][variable]. However, I would like to do similar thing in inventory file: In serverC.yml:

myvar1: "{{ hostvars['master01']['myvar1'] }}"
myvar2: "{{ hostvars['master02']['myvar2'] }}"

In master02.yml:

myvar2: "{{ hostvars['master01']['myvar2'] }}"

In master01.yml:

myvar1: test1
myvar2: test2

So far the myvar1 is working when run playbook with -l serverC. myvar2 is working when run playbook on master02, too. However, if myvar2 is print as "{{ hostvars['master02']['myvar2'] }}" when run with -l serverC. Is there any way can make sure the myvar2 correctly expands to test2?

djdomi avatar
za flag
Your questions seems already been answered, take a look here https://serverfault.com/questions/638507/how-to-access-host-variable-of-a-different-host-with-ansible?rq=1
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.