外部传入变量值
---
- hosts: testB
remote_user: root
tasks:
- name: "Passing Variables On The Command Line"
debug:
msg: "{{pass_var}}"
不给值会报错

只会输出被引用的变量值
---
- hosts: testB
remote_user: root
tasks:
- name: "Passing Variables On The Command Line"
debug:
msg: "{{num_var}}"

同时引用传入多个变量值
---
- hosts: testB
remote_user: root
tasks:
- name: "Passing Variables On The Command Line"
debug:
msg: "{{num_var,pass_var}}"

外部传入值的优先级大于内部定义
---
- hosts: testB
remote_user: root
vars:
pass_var: test
tasks:
- name: "Passing Variables On The Command Line"
debug:
msg: "{{pass_var}}"

命令行传入值
ansible testB -e "testdir=redhat" -m shell -a "echo {{testdir}}"
文件传入变量值

---
- hosts: testB
remote_user: root
tasks:
- name: "Passing Variables On The Command Line"
debug:
msg: "{{testvar}} {{numlist[2]}}"

本文深入探讨了Ansible中变量的传递方式,包括命令行、文件及外部传入的多种场景。解析了变量引用、优先级及具体用例,为自动化运维人员提供了实用指南。
464

被折叠的 条评论
为什么被折叠?



