可以使用“必需”属性来使某个变量成为强制要求,并在该变量未定义时引发错误。
以下是一个示例,在其中定义了一个强制要求变量“my_var”:
- name: Example playbook
hosts: localhost
vars:
some_var: 'value1'
my_var: '{{ my_var | mandatory }}'
tasks:
- name: Example task
debug:
msg: "The value of some_var is {{ some_var }}, and the value of my_var is {{ my_var }}"
在上面的示例中,如果你在调用 playbook 时不定义 “my_var”,则 playbook 将在插值变量时引发错误,并输出以下错误消息:
TASK [Example Task] *************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'my_var' is undefined\n\nThe error appears to have been in '/path/to/playbook.yml': line 13, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n - name: Example Task\n ^ here\n"}