文件
[root@ansible-server ansible]# tree ./
./
├── hosts
└── var.yaml
hosts 文件
[web]
192.168.10.11 key=13
192.168.10.12
[web:vars]
ansible_python_interpreter=/usr/bin/python2.6
key=test
var.yaml 文件
---
- hosts: all
gather_facts: Flase
tasks:
- name : display host variables
debug: msg="The {{ inventory_hostname }} vaule is {{ key }}"
获取变量值
#ansible-playbook -i hosts var.yaml
PLAY [all] ********************************************************************
TASK: [display host variables] ************************************************
ok: [192.168.10.11] => {
"msg": "The 192.168.10.11 vaule is 13"
}
ok: [192.168.10.12] => {
"msg": "The 192.168.10.12 vaule is test"
}
PLAY RECAP ********************************************************************
192.168.10.11 : ok=1 changed=0 unreachable=0 failed=0
192.168.10.12 : ok=1 changed=0 unreachable=0 failed=0
PS:当hosts定义了key值,以hosts定义变量优先,如hosts未定义变量,则取hosts中web:vars中变量值
转载自:https://www.chinasa.net/archives/311.html