当变量存在时执行命令、当变量不存在时执行命令.note

noteId: WEB0acfc2d0c52ec9af488fab6df51995be · 原始路径:/ALL/Ansible 自动化管理/Ansible 配置合集/当变量存在时执行命令、当变量不存在时执行命令.note · 图片:1 · 附件待处理:0

 
 
当变量存在时执行命令:
when: hostname is defined
当变量不存在时执行命令:
when: hostname is not defined
 
还有另外一种 jinja2的写法,定义一个默认值:
当hostname存在时就使用hostname变量,当它不存在时就是用 inventory_hostname变量