Ansible read variable defined in inventory from playbook yaml file.

Need some help regarding reading on vars from inventory…

Your inventory looks wrong, I'm not sure if you are going for a 'yaml'
format one?

In any case, accessing variables would just be using the variable
name, for example:

- debug: msg='{{username}}'

but for that to work the file would need to look like this (assuming
you want the vars fro every host in the group):

groupname:
   hosts:
      localhost:
   vars:
      username: myusername
       password: mypassword

Hi Brain,

Thank you. yes I was going for a yaml formatted inventory for this example but I wouldn’t mind even if it is a ini file. I tried as you have mentioned. It gives an an error as failed to connect to host via ssh : permission denied. I actually don’t want to connect to any hosts. what I want is access the username, password variables from inventory rather than passing them as --extra-vars.

is accesing using hostvars[‘hosatname’].username different to this.

Is it possible to access any variable in inventory just only using the variable name ?

II’m trying to create a inventory dynamically base on this.

“group”: {
“hosts”: [
“localhost”
],
“vars”:
{
“username” : “myusername”,
“password” : “mypassword”
}
},
“_meta”: {
“hostvars”: {
“localhost”: {
“ansible_connection”: “ssh”,

}
}
}

What I’m trying to then is access username ans password variables from playbook yaml file

As i said, accessing the variables is just using them by name.

The problem on 'connection' is that you defined localhost and must
have not set gather_facts: no.

The 'form' of the inventory does not matter as long as it is a valid inventory.