直接上代码
---
- hosts: all
gather_facts: no
vars:
xstr1: node-45-67-tencent.domain
tasks:
- name: debug-01
debug:
msg: "原始变量是: {
{xstr1}}"
run_once: true
- name: 改变变量
shell: echo "{
{ xstr1}}-hehe"
register: xstr1
run_once: true
connection: local
- name: debug-05
debug:
msg: "原始变量是: {
{xstr1.stdout}}"
run_once: true
- hosts: web
gather_facts: no
tasks:
- name: debug-15
debug:
msg: "原始变量是: {
{xstr1}}"
run_once: true
执行的结果是
PLAY [all] ***************************************************************************************************************************************************
TASK [debug-01] **********************************************************************************************************************************************
ok: [10.100.100.20] => {
"msg": "原始变量是: node-45-67-tencent.domain"
}
TASK [改变变量] **************************************************************************************************************************************************
changed: [10.100.100.20]
TASK [debug-05] **********************************************************************************************************************************************
ok: [10.100.100.20] => {
"msg": "原始变量是: node-45-67-tencent.domain-hehe"
}
PLAY [web] ***************************************************************************************************************************************************
TASK [debug-15] **********************************************************************************************************************************************
ok: [10.100.100.20] => {
"msg": "原始变量是: {'stderr_lines': [], u'changed': True, u'end': u'2023-05-27 20:45:30.103563', 'failed': False, u'stdout': u'node-45-67-tencent.domain', u'cmd': u'echo \"node-45-67-tencent.domain-hehe\"', u'rc': 0, u'start': u'2023-05-27 20:45:30.099734', u'stderr': u'', u'delta': u'0:00:00.003829', 'stdout_lines': [u'node-45-67-tencent.domain-hehe'], 'ansible_facts': {u'discovered_interpreter_python': u'/usr/bin/python'}}"
}
PLAY RECAP ***************************************************************************************************************************************************
10.100.100.20 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
小结:说明变量只要在一个剧本中都是可以使用的