在Ansible中,可以使用set_fact
模块来覆盖默认变量。以下是一个包含代码示例的解决方法:
main.yml
的Ansible Playbook文件,内容如下:---
- name: Set custom variable
hosts: localhost
gather_facts: false
vars:
default_variable: "default value"
tasks:
- name: Set custom variable
set_fact:
custom_variable: "custom value"
- name: Display variables
debug:
var: default_variable
- name: Display variables
debug:
var: custom_variable
ansible-playbook main.yml
PLAY [Set custom variable] *********************************************************************************************************
TASK [Set custom variable] ********************************************************************************************************
ok: [localhost]
TASK [Display variables] **********************************************************************************************************
ok: [localhost] => {
"default_variable": "default value"
}
TASK [Display variables] **********************************************************************************************************
ok: [localhost] => {
"custom_variable": "custom value"
}
PLAY RECAP ***********************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0
如上所示,set_fact
模块可以用于覆盖默认变量,并且可以在后续任务中使用这些自定义变量。