Ansible Inventory file

Hi Everyone,

I am new to Ansible , I have created new inventory yml file with below details .

KafkaNonprodservers:
hosts:
abc.abc.com:2022
abd.abd.com:2022
ansible_user : abc

but I am getting below error

  • ansible-inventory -i ./ansible/Hosts.yml --list [WARNING]: Skipping unexpected key (ansible_user) in group (KafkaNonprodservers), only “vars”, “children” and “hosts” are valid
    {

Can you please guide me,.

Regards,
Deepak

Hi Deepak,

ansible_user is a special variable used by ansible for ssh, part of the main config. If you use a different variable name in your config file you should be fine.

https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#list-of-behavioral-inventory-parameters

sorry got the wrong idea, I’m also new to ansible.

The variables you’re adding need to go under a vars section

KafkaNonprodservers:
hosts:

ansible_user: abc

[keif@ansible-lab ansible]$ ansible-inventory -i keif/test.yml --list
{
“KafkaNonprodservers”: {
“hosts”: [
abc.abc.com:2022 abd.abd.com:2022
]
},
“_meta”: {
“hostvars”: {
abc.abc.com:2022 abd.abd.com:2022”: {
“ansible_user”: “abc”
}
}
},
“all”: {
“children”: [
“KafkaNonprodservers”,
“ungrouped”
]
},
“ungrouped”: {}
}

KafkaNonprodservers:
hosts:
abc.abc.com:2022
abd.abd.com:2022
vars:
ansible_user: abc

Hello Deepak,

You’re not following the right syntax for a YML inventory. Check the following link:

https://docs.ansible.com/ansible/latest/plugins/inventory/yaml.html

I don’t know what you’re trying to do with the " 2022" string there. It’s not valid yaml.

According to the docs in the link. You probably have to do something like this.

$ cat lala.yml
KafkaNonprodservers:
hosts:
abc.abc.com:
abd.abd.com:
vars:
ansible_user: abc

You can always analyze your inventory file with ansible-inventory, like this:

$ ansible-inventory -i lala.yml --graph --vars
@all:

@KafkaNonprodservers:

–abc.abc.com
–abd.abd.com
–{ansible_user = abc}
@ungrouped:

Hope that helps,

Hugo

Hi Hugo,

Thanks a lot , now it is working fine.

Regards,
Deepak

Thanks Keif,

I have added vars as port and users, after that it is working fine.

Regards,
Deepak