1.set_factz在tasks内部定义变量
---
- hosts: testB
remote_user: root
tasks:
- set_fact:
testvar: "test"
- debug:
msg: "{{testvar}}"
2.set_fact 将一个变量的值赋予另一个变量
---
- hosts: testB
remote_user: root
vars:
testvar1: test1_string
tasks:
- shell: "echo test2_string"
register: shellreturn 注册定义变量
- set_fact:
testvar2: "{{shellreturn.stdout}}" 从key拿value
- debug:
msg: "{{testvar1}} {{testvar2}}"
#var: shellreturn
3.一个play引用另外一个play的变量
set_fact定义可以引用
var定义不可以引用
---
- hosts: testB
remote_user: root
vars:
testvar1: tv1
tasks:
- set_fact:
testvar2: tv2
- debug:
msg: "{{testvar1}} ---- {{testvar2}}"
- hosts: testB
remote_user: root
tasks:
- name: other play get testvar2
debug:
msg: "{{testvar2}}"
- name: other play get testvar1
debug:
msg: "{{testvar1}}"
4.不同play引用注册变量
---
- hosts: testB
remote_user: root
vars:
testvar3: tv3
tasks:
- shell: "echo tv4"
register: testvar4
- debug:
msg: "{{testvar3}} ---- {{testvar4.stdout}}"
- hosts: testB
remote_user: root
tasks:
- name: other play get testvar4
debug:
msg: "{{testvar4.stdout}}"
- name: other play get testvar3
debug:
msg: "{{testvar3}}"
总结: 通过vars定义的变量,别的play不能引用