[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