Ansible不覆盖默认变量
Ansible not overriding default vars
我在 role/defaults/main.yml
中定义了以下变量:
jvm_heap_size_max: 1024
jvm_heap_size_min: "{{ (jvm_heap_size_max * 0.5)|int|abs }}"
我的 group_vars
文件夹如下所示:
├── group_vars
│ ├── all
│ ├── group
│ │ ├── vars
│ │ └── vault
我在 inventories/test/group_vars/group/vars
中有以下变量:
jvm_heap_size_max: 512
jvm_heap_size_min: 1024
我的预期输出是:
jvm_heap_size_max: 512
jvm_heap_size_min: 1024
但我得到的是:
jvm_heap_size_max: 1024
jvm_heap_size_min: 1024
怎么会?我正在使用 Ansible 2.7.7
这应该按照您的描述工作。
我设置了一个最小示例,并验证了 group_vars
确实比角色默认值具有更高的优先级,written in the documentation。
由于您的示例不包括组的实际名称和相应的文件夹名称,因此我认为这就是错误所在。
另外,我验证的时候,我的group_vars
在我的项目根目录下的文件group_vars/all.yml
里,其他模式我不熟悉
使用 localhost
和 group_vars/all.yml
的快速示例:
roles/testrole/defaults/main.yml
---
jvm_heap_size_max: 1024
jvm_heap_size_min: "{{ (jvm_heap_size_max * 0.5)|int|abs }}"
roles/testrole/tasks/main.yml
---
- debug:
var: jvm_heap_size_min
- debug:
var: jvm_heap_size_max
group_vars/all.yml
---
jvm_heap_size_max: 2048
testplay.yml
- hosts: localhost
roles:
- testrole
调用&输出:
➜ ansible-playbook testplay.yml
[WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] *************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [testrole : debug] ******************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"jvm_heap_size_min": "1024"
}
TASK [testrole : debug] ******************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"jvm_heap_size_max": 2048
}
PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0
我忘了提及我在上面的示例中针对的是同一个主机。显然,记录了这种行为:
https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#how-variables-are-merged
By default variables are merged/flattened to the specific host before a play is run. This keeps Ansible focused on the Host and Task, so groups don’t really survive outside of inventory and host matching. By default, Ansible overwrites variables including the ones defined for a group and/or host (see the hash_merge setting to change this) .
我在 role/defaults/main.yml
中定义了以下变量:
jvm_heap_size_max: 1024
jvm_heap_size_min: "{{ (jvm_heap_size_max * 0.5)|int|abs }}"
我的 group_vars
文件夹如下所示:
├── group_vars
│ ├── all
│ ├── group
│ │ ├── vars
│ │ └── vault
我在 inventories/test/group_vars/group/vars
中有以下变量:
jvm_heap_size_max: 512
jvm_heap_size_min: 1024
我的预期输出是:
jvm_heap_size_max: 512
jvm_heap_size_min: 1024
但我得到的是:
jvm_heap_size_max: 1024
jvm_heap_size_min: 1024
怎么会?我正在使用 Ansible 2.7.7
这应该按照您的描述工作。
我设置了一个最小示例,并验证了 group_vars
确实比角色默认值具有更高的优先级,written in the documentation。
由于您的示例不包括组的实际名称和相应的文件夹名称,因此我认为这就是错误所在。
另外,我验证的时候,我的group_vars
在我的项目根目录下的文件group_vars/all.yml
里,其他模式我不熟悉
使用 localhost
和 group_vars/all.yml
的快速示例:
roles/testrole/defaults/main.yml
---
jvm_heap_size_max: 1024
jvm_heap_size_min: "{{ (jvm_heap_size_max * 0.5)|int|abs }}"
roles/testrole/tasks/main.yml
---
- debug:
var: jvm_heap_size_min
- debug:
var: jvm_heap_size_max
group_vars/all.yml
---
jvm_heap_size_max: 2048
testplay.yml
- hosts: localhost
roles:
- testrole
调用&输出:
➜ ansible-playbook testplay.yml
[WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] *************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [testrole : debug] ******************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"jvm_heap_size_min": "1024"
}
TASK [testrole : debug] ******************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"jvm_heap_size_max": 2048
}
PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0
我忘了提及我在上面的示例中针对的是同一个主机。显然,记录了这种行为:
https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#how-variables-are-merged
By default variables are merged/flattened to the specific host before a play is run. This keeps Ansible focused on the Host and Task, so groups don’t really survive outside of inventory and host matching. By default, Ansible overwrites variables including the ones defined for a group and/or host (see the hash_merge setting to change this) .