different setting in template for different inventory

Hello, I am trying to make simple NGINX template. I have development inventory (it is vagrant) and staging which is remote VPS. I want to make different default root folder and different log location for these two environments. Ofcorse couldnt manage that :slight_smile: .
I have this in my template:

{% if (inventory_hostname in groups[‘webservers’]) %}
access_log /var/log/nginx/nginx-access.log;
error_log /var/log/nginx/nginx-error.log;
{% endif %}

And for dev environment:

{% if (inventory_hostname in groups[‘vagrant’]) %}
access_log /vagrant/log/nginx/nginx-access.log;
error_log /vagrant/log/nginx/nginx-error.log;
{% endif %}

Whatever inventory I choose when depoying ansible-playbook I get BOTH of cases created in nginx config file. I am obviously missing something here. Any advice?

It seems most likely that your host has been inadvertently added to both groups.

It’s hard to say without seeing your inventory.

Hello here s contents of my hosts file. I have separate files for staging and vagrant.

Unrelated:

What you have done above is redefined the host record. In the second case you are overriding the variables for the host. To fix this, instead do:

[webservers]
webservers ansible_ssh_user=root

[dbservers]
dbservers ansible_ssh_user=root

Since you are definining “ansible_hostname” it won’t use the IP address to contact things anyway.

I’m unclear why your template is not working, though this may be something else not shown above, such as it using the wrong template file, etc?

thanks for help, I still have some trouble.

I have defined inventory like this:

[webservers]
webservers ansible_ssh_user=root

[dbservers]
dbservers ansible_ssh_user=root

Problem is I dont have DNS names for these hosts, is it possibile to define IP addresses of these hosts inside ansible, so I dont have to edit /etc/hosts ?

/etc/ansible/hosts:

[groupname]
myhostname ansible_ssh_host=10.1.1.10