Inventory folder structure verification

Hi all, first time posting, so apologies if I miss something.

I’m working on organizing an inventory folder for a multistage environment and would like verification on whether the way I expect to work is true, or if I’m missing something.

Objective
Using the following inventory folder structure:

.
├── production/
│ ├── group_vars/
│ ├── host_vars/
│ │ └── hostname-prod.fqdn.yml
│ └── inventory.ini # (contains entry for hostname-prod.fqdn)

├── staging/
│ ├── group_vars/
│ ├── host_vars/
│ │ └── hostname-stage.fqdn.yml
│ └── inventory.ini # (contains entry for hostname-stage.fqdn)

└── testing/
├── group_vars/
├── host_vars/
│ └── hostname-test.fqdn.yml
└── inventory.ini # (contains entry for hostname-test.fqdn)

I would execute a playbook (with “hosts: hostname-prod.fqdn”) to be run as follows, with the expectation that it will automatically pick up the hostname entry from the “production/inventory.ini” and the associated host variables from the “production/host_vars/hostname-stage.fqdn.yml” file:

$> ansible-playbook my-play.yml

Problem
When I try to do this, the hostname entry is indeed picked up from the “production/inventory.ini” file, however, the associated host variables aren’t picked up from the “production/host_vars/hostname-stage.fqdn.yml” file.

If I run the command as follows though, it does work:

$> ansible-playbook -i production my-play.yml

Question
Is this the correct and expected behavior? Or should my original method work without specifying “-i production”?

Im unsure of what the problem is since you seem to have contradictory
expectations, you state info should be picked from
`"production/host_vars/hostname-stage.fqdn.yml` but that file is not
under production, but under staging.

In any case you can use `ansible-inventory` to see what ansible
actually composes from the given inventory.

That is expected.

If you don’t specify -i how the ansible-playbook know you want to run against production or staging or test env ?

You could probably set some defaults using the inventory setting under defaults section in ansible.cfg. For example
https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg

I would probably organize all the inventories in one folder by moving the production, staging and test folder under an inventory folder

Then you should be able to run
Ansible-playbook -i inventory/production my-play.yml and pick up the grou_vars and host_vars

The behavior is the same if I specify -i ec2.py. I like to specify inventory manually instead of making it a global setting. Are you saying the yml should call the python script?