为什么playbook对组变量采取错误的值?

时间:2018-04-29 08:35:42

标签: ansible ansible-inventory

我对群组变量有疑问。    示例:我有两个清单组group_A和group_B,并且在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 

问题出在我执行时:

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

为适当的服务器范围(server3,server4)执行了playbook,但它从组变量文件group_A中获取变量。

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

ansible 2.4.2.0

BR Oleg

2 个答案:

答案 0 :(得分:1)

我包含了ANSIBLE_DEBUG,以及我发现的内容:

2018-05-03 15:23:23,663 p = 129458 u = user | 129458 1525353803.66336:从/ ansible / inventoryories / prod / group_vars / group_B.yml 加载数据 2018-05-03 15:23:23,663 p = 129458 u = user | 129661 1525353803.66060:在run()中 - 任务00505680-eccc-d94e-2b1b-0000000000f4 2018-05-03 15:23:23,664 p = 129458 u = user | 129661 1525353803.66458:调用self._execute() 2018-05-03 15:23:23,665 p = 129458 u = user | 129458 1525353803.66589:从/ansible/inventories/prod/group_vars/group_A.yml

加载数据

关于playbook执行ansible扫描文件夹group_vars中变量的所有文件,其中包含变量" var_port",last将赢得.....

你可以在另一个话题中找到

Ansible servers/groups in development/production

并从文档中: 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**.

对我来说,现在还不清楚如何管理配置文件。在这种情况下,我必须为每个组使用唯一的变量名称,但是关于角色是不可能的,或者当我调用playbook时我应该使用include_vars吗?

答案 1 :(得分:1)

如何从DigitalOcean管理多级环境中的变量文件的超级示例
How to Manage Multistage Environments with Ansible