以下 ansible 代码失败并出现错误:
The task includes an option with an undefined variable. The error was: 'storage_size_gb' is undefined
- debug:
msg: "{{ mongo_output_withoutvar.stdout }}"
- set_fact:
storage_size_gb: "{{ mongo_output_withoutvar.stdout | int }}"
min_free_space_rec: "{{ min_free_space_pass | int }}"
combined_output: "{{ storage_size_gb + min_free_space_rec }} GB"
输出:
TASK [debug] *******************************************************************
ok: [ip-10-236-75-137.ec2.internal] => {
"msg": "25"
}
TASK [set_fact] ****************************************************************
fatal: [remotehostl]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'storage_size_gb' is undefined\\n\\nThe error appears to be in '/runner/project/backupmongodb.yml': line 220, column 7, but may\\nbe elsewhere in the file depending on the exact syntax problem.\\n\\nThe offending line appears to be:\\n\\n\\n - set_fact:\\n ^ here\\n"}
我尝试运行示例测试用例,但出现了相同的错误:
---
- hosts: localhost
gather_facts: false
vars:
min_free_space_pass: "30"
tasks:
- set_fact:
storage_size_gb: 40
min_free_space_rec: "{{ min_free_space_pass | int }}"
combined_output: "{{ storage_size_gb + min_free_space_rec }} GB"
- debug:
var: combined_output
运行:
ansible-playbook sample.yml
输出:
PLAY [localhost] **************************************************************************************************************
TASK [set_fact] ***************************************************************************************************************
Wednesday 11 October 2023 02:41:29 -0500 (0:00:00.055) 0:00:00.055 *****
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'storage_size_gb' is undefined\n\nThe error appears to be in '/home/wladmin/testset.yml': line 7, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - set_fact:\n ^ here\n"}
PLAY RECAP ********************************************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
您能建议我如何克服这个错误吗?
该错误来自于您在同一个
set_fact
任务中声明并使用相同的变量,如@β.εηοιτ.βε 评论中所报告的那样。同时,这里绝对没有任何正当理由使用set_fact
。以下剧本应该可以帮助您继续前进:
- hosts: localhost
gather_facts: false
vars:
min_free_space_pass: 30
storage_size_gb: 40
combined_output: "{{ storage_size_gb | int + min_free_space_pass | int }} GB"
tasks:
- debug:
var: combined_output