背景
跨play引用变量出错,提示The task includes an option with an undefined variable. The error was: 'var1' is undefined
。
Ansible的变量可以通过几种主要的方式定义,var
、register
、set_fact
等等。但需要注意这些变量的命名空间,在Ansible中变量的命名空间“绑定”在主机上面。因此跨play引用变量时,如果针对的hosts不同,那么直接引用变量就会出错。
举例说明
- hosts: localhost
tasks:
- name: set var1 from localhost
command: uname -r
register: var1
- hosts: myhost
tasks:
- name: show var1.stdout
debug:
msg: "var1.stdout