# Why playbook take wrong values for group variables?

**URL:** <https://forum.ansible.com/t/why-playbook-take-wrong-values-for-group-variables/27404>\
**Category:** Ansible Project\
**Created:** [April 29, 2018, 8:39am UTC](https://forum.ansible.com/t/why-playbook-take-wrong-values-for-group-variables/27404 "2018-04-29T08:39:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![user30](https://avatars.discourse-cdn.com/v4/letter/u/ed655f/32.png) [@user30](https://forum.ansible.com/u/user30)\
**Post date:** [April 29, 2018, 8:39am UTC](https://forum.ansible.com/t/why-playbook-take-wrong-values-for-group-variables/27404/1 "2018-04-29T08:39:59Z")

</div>

Hi,

[favorite](https://stackoverflow.com/questions/50084758/why-playbook-take-wrong-values-for-group-variables#)

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:

```auto
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 :

```auto
 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.

```auto
    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

---

<div class="post-metadata">

**Author:** ![Tchia04](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@Tchia04](https://forum.ansible.com/u/Tchia04)\
**Post date:** [May 1, 2018, 8:35pm UTC](https://forum.ansible.com/t/why-playbook-take-wrong-values-for-group-variables/27404/2 "2018-05-01T20:35:41Z")

</div>

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"**

```auto

```

---

<div class="post-metadata">

**Author:** ![user30](https://avatars.discourse-cdn.com/v4/letter/u/ed655f/32.png) [@user30](https://forum.ansible.com/u/user30)\
**Post date:** [May 4, 2018, 12:02pm UTC](https://forum.ansible.com/t/why-playbook-take-wrong-values-for-group-variables/27404/3 "2018-05-04T12:02:38Z")

</div>

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…**

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 4, 2018, 1:43pm UTC](https://forum.ansible.com/t/why-playbook-take-wrong-values-for-group-variables/27404/4 "2018-05-04T13:43:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mhalano](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mhalano/32/1815_2.png) [@mhalano](https://forum.ansible.com/u/mhalano)\
**Post date:** [May 4, 2018, 3:03pm UTC](https://forum.ansible.com/t/why-playbook-take-wrong-values-for-group-variables/27404/5 "2018-05-04T15:03:42Z")

</div>

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](https://groups.google.com/d/msgid/ansible-project/CACVha7d8CM6kwVi0ESJdQMNb%3DgkdzQh0eGSg-FcA5p%3DNPOH3_A%40mail.gmail.com)  
.

---

<div class="post-metadata">

**Author:** ![user30](https://avatars.discourse-cdn.com/v4/letter/u/ed655f/32.png) [@user30](https://forum.ansible.com/u/user30)\
**Post date:** [May 6, 2018, 8:30pm UTC](https://forum.ansible.com/t/why-playbook-take-wrong-values-for-group-variables/27404/6 "2018-05-06T20:30:51Z")

</div>

Hi Markos,  
my playbook looks like:

```auto
- hosts: group_B
  roles:
    - dse_agent_full_install

```
