2
votes

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:

 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

BR Oleg

2
could you please ensure you don´t have that variable somewhere else? I have tested your scenario with 2.4.2 and works fine: --- - name: Test hosts: all gather_facts: False tasks: - name: Print Var debug: var: var_port TASK [Print DBs (Raw)] ***************************************************************************************************** ok: [server3] => { "var_port": 9002 } ok: [server4] => { "var_port": 9002 }imjoseangel
Agree with @imjoseangel: as presented, your usecase should work. You can try to check your inventory configuration with the ansible-inventory and the options --graph and --varszigarn
when I disabled variable var_port in file group variables "group_A" ( with #) my playbook take right variable value 9002, from group_var file " group_B". It is look like playbook run through all group_files take first one and did not filter by group condition (in my case group_B)oleg.borovykh
I include ANSIBLE_DEBUG 2018-05-03 15:23:23,663 p=129458 u=user | 129458 1525353803.66336: Loading data from /ansible/inventories/prod/group_vars/p_dseoper.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/t_dseoper.ymloleg.borovykh

2 Answers

4
votes

I 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

on playbook execution ansible scan all files with variables in folder group_vars which have variable "var_port", last will win.....

as you can found in another topic: Ansible servers/groups in development/production

and from documentation: http://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable

Note

Within any section, redefining a var will overwrite the previous instance. If multiple groups have the same variable, **the last one loaded wins**. If you define a variable twice in a play’s vars: section, the **2nd one wins**.

For me now NOT clear how to manage configuration files. In this case I must use unique variables names for each group, but it is not possible regarding roles, or should I use include_vars when i call playbook?

1
votes

Super example how to manage variables files in multistage environment from DigitalOcean
How to Manage Multistage Environments with Ansible