Structuring inventory

Hi,

I am just starting with Ansible and have questions on how to better structure my inventory file(s).

Here is what I have:

Multiple hosts, assigned to different environments.

test1 → host1, host2, host3
test2 → host4, host5, host6

On each of those hosts I have multiple users, that used to run various software components and such users have to be configured differently.

I’ve put this to inventory like this:

[test1-host1]
test1-host1-user1 ansible_ssh_user=user1
test1-host1-user2 ansible_ssh_user=user2

[test1-host1:vars]
ansible_ssh_host=host1_name

[test1-host2]
test1-host2-user3 ansible_ssh_user=user3
test1-host2-user4 ansible_ssh_user=user4

[test1-host2]
ansible_ssh_host=host2_name

Am I doing my inventory right?

Regards,
Aleksey Maksimov

Tyipcially you wouldn’t create different host records just to connect to them as different users, but would specify this as “-u” to the playbook execution.

And while you can keep variables inline with “:vars”, I would generally recommend group_vars/ and host_vars/ files for nearly everyone, they are cleaner
and can also help represent structured data.

It’s definitely fine to set ansible_ssh_user there, but having hte “-user” as part of the host name is a bit unusual.

Also if you have different environments that are not managed together, I usually recommend seperate inventory files for them, keeps it simpler in most cases.

I don’t know that there is a right or wrong way to do inventory, but I wouldn’t do it like that. :slight_smile:

Given that you have two environments that might be two groups… But if there is nothing in common between the hosts in an environment then that may not be the case.

I would take your hosts and see what they have in common…

If for example you have two web servers, four application servers and two of your application servers also run databases then that looks like three clear groups…

[Web]
host1
host2

[App]
host3
host4
host5
host6

[DB]
host5
host6

If the environments share some things but are different for some things then you might also want environment groups

[test1]
host1
host3
host5

[test2]
host2
host4
host6

… Then use the group_vars files to set the variables that you want ansible to apply.

I hope that this helps,

Adam

Tyipcially you wouldn’t create different host records just to connect to them as different users, but would specify this as “-u” to the playbook execution.

That way if I need to do something for all users in all hosts in the environments, I would have to execute playbooks manually per each user. This kind of defeats the purpose of automation in my mind.

And while you can keep variables inline with “:vars”, I would generally recommend group_vars/ and host_vars/ files for nearly everyone, they are cleaner
and can also help represent structured data.

I understand that and just find it easier to have vars in the same file and format as inventory. Need to get used to switching between yaml and property files. :slight_smile:

It’s definitely fine to set ansible_ssh_user there, but having hte “-user” as part of the host name is a bit unusual.

Also if you have different environments that are not managed together, I usually recommend seperate inventory files for them, keeps it simpler in most cases.

Yes, I already do that, not pointed out for the sake of simpicity.

Regards,
Aleksey Maksimov