Why playbook take wrong values for group variables?

Hi,

favorite

I have a problem with groups variables.

Example: I have two inventory groups group_A and group_B, and also have the same name files in group_vars:

inventories/ 
           hosts.inv    
             [group_A]
               server1
               server2
             [group_B]
               server3
               server4
             group_vars/
                  group_A - file
                    var_port: 9001
                  group_B - file
                    var_port: 9002 

The problem is when i execute playbook :

 ansible-playbook  playbooks/playbook.yml -i inventories/hosts.inv -l **group_B**

playbook was executed for proper scope of servers (server3, server4) but it takes variables from group variables file group_A.

    expected result:        var_port: 9002
    in realty :             var_port: 9001

ansible 2.4.2.0

I can’t understand what I am doing wrong?

BR Oleg

Do you have the following files defined?
/group_vars/group_A.yml
/group_vars/group_B.yml
/group_vars/all.yml
/inventories/group_vars/all.yml

also, maybe try using "hosts: group_B" in playbooks/playbook.yml instead of "hosts:all"


hi Tony,
i have no files at all:
/group_vars/all.yml
/inventories/group_vars/all.yml
and i try to put "hosts: group_B", but it not resole this issue. Playbook apply changes to the servers from right group but, variables it take from neighborhood group_vars/file.

I have included ANSIBLE_DEBUG , and what i have found:

2018-05-03 15:23:23,663 p=129458 u=user | 129458 1525353803.66336: Loading data from /ansible/inventories/prod/group_vars/group_B.yml

2018-05-03 15:23:23,663 p=129458 u=user | 129661 1525353803.66060: in run() - task 00505680-eccc-d94e-2b1b-0000000000f4

2018-05-03 15:23:23,664 p=129458 u=user | 129661 1525353803.66458: calling self._execute()

2018-05-03 15:23:23,665 p=129458 u=user | 129458 1525353803.66589: Loading data from /ansible/inventories/prod/group_vars/group_A.yml

it is look like on playbook execution ansible scan all files with variables in folder group_vars which have variable “var_port”, last will win…

The `hosts:` keyword is only used to select which hosts get targeted,
it has no influence on group membership, any host is ALWAYS a member
of ALL the groups as defined in inventory, so the variables for a host
always reflect ALL GROUPS.

Interesting. Could you show us what yours "hosts:" line on playbook.yml?

The `hosts:` keyword is only used to select which hosts get targeted,
it has no influence on group membership, any host is ALWAYS a member
of ALL the groups as defined in inventory, so the variables for a host
always reflect ALL GROUPS.

--
----------
Brian Coca

--
You received this message because you are subscribed to the Google Groups

"Ansible Project" group.

To unsubscribe from this group and stop receiving emails from it, send an

email to ansible-project+unsubscribe@googlegroups.com.

To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit

https://groups.google.com/d/msgid/ansible-project/CACVha7d8CM6kwVi0ESJdQMNb%3DgkdzQh0eGSg-FcA5p%3DNPOH3_A%40mail.gmail.com
.

Hi Markos,
my playbook looks like:

- hosts: group_B
  roles:
    - dse_agent_full_install