[ master]
master ansible_user= root ansible_password= 123456 ansible_connection= ssh
[ slave]
node1 ansible_user= root ansible_password= 123456 ansible_connection= ssh
node2 ansible_user= root ansible_password= 123456 ansible_connection= ssh
---
- name : Hello World
hosts : all
gather_facts : no
tasks :
- name : test vars
debug :
msg : "ansible_user = {{ ansible_user }}, ansible_password = {{ ansible_password }}"
[ root@master ~]
[ control]
master
[ control:vars]
ansible_user= root
ansible_password= 123456
ansible_connection= ssh
[ slave]
node1
node2
[ slave:vars]
ansible_user= root
ansible_password= 123456
ansible_connection= ssh
---
- name: Hello World
hosts: control
gather_facts: no
tasks:
- name: test vars
debug:
msg: "ansible_user = {{ ansible_user }}, ansible_password = {{ ansible_password }}"
[ root@master ~]
PLAY [ Hello World] *******************************************************************************************************************************
TASK [ test vars] *********************************************************************************************************************************
ok: [ master] = > {
"msg" : "ansible_user = root, ansible_password = 123456"
}
PLAY RECAP ***************************************************************************************************************************************
master : ok= 1 changed= 0 unreachable= 0 failed= 0
host && group 配置
---
- name : Hello World
hosts : slave
gather_facts : no
tasks :
- name : test vars
debug :
msg : "ansible_user = {{ ansible_user }}, ansible_password = {{ ansible_password }}, http_port = {{ http_port }}"
group-host-vars/inventory/host
[ control]
master
[ slave]
node1
node2
group-host-vars/inventory/group_vars/control.yml – control 組 – 共用
ansible_user : root
ansible_password : 123456
ansible_connection : ssh
http_port : 10080
group-host-vars/inventory/group_vars/slave.yml – slave 組 – 共用
ansible_user : root
ansible_password : 123456
ansible_connection : ssh
http_port : 80
group-host-vars/inventory/host_vars/node1.yml – node1 節點 – 獨有
http_port : 20080
[ root@master ~]
PLAY [ Hello World] *******************************************************************************************************************************
TASK [ test vars] *********************************************************************************************************************************
ok: [ node1] = > {
"msg" : "ansible_user = root, ansible_password = 123456, http_port = 20080"
}
ok: [ node2] = > {
"msg" : "ansible_user = root, ansible_password = 123456, http_port = 80"
}
PLAY RECAP ***************************************************************************************************************************************
node1 : ok= 1 changed= 0 unreachable= 0 failed= 0
node2 : ok= 1 changed= 0 unreachable= 0 failed= 0