ansible学习(2):清单配置详情
2019-10-30 本文已影响0人
August________
ansible学习(2):清单配置详情
配置文件写上受管理的主机
]# tail -n 5 /etc/ansible/hosts
## db-[99:101]-node.example.com
test1 ansible_host=192.168.13.132 ansible_port=22 ansible_user=root ansible_ssh_pass=123456
test2 ansible_host=192.168.13.134 ansible_port=22 ansible_user=root ansible_ssh_pass=123456
test3 ansible_host=192.168.13.133 ansible_port=22 ansible_user=root ansible_ssh_pass=123456
- 通过命令测试受管理的主机
[root@ansible ~]# ansible test1 -m ping
test1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@ansible ~]# ansible test2 -m ping
test2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
使用“all”关键字,一次性去操作清单下的所有主机。
[root@ansible ~]# ansible all -m ping
test1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
test2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
test3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
"分组功能"
-
配置清单支持"分组功能",将默写主机分为一组,通过组名来管理主机
-
分组测试
[root@ansible ~]# tail -n 5 /etc/ansible/hosts
test1 ansible_host=192.168.13.132 ansible_port=22 ansible_user=root ansible_ssh_pass=123456
test2 ansible_host=192.168.13.134 ansible_port=22 ansible_user=root ansible_ssh_pass=123456
[B]
test3 ansible_host=192.168.13.133 ansible_port=22 ansible_user=root ansible_ssh_pass=123456
- 测试A、B组
[root@ansible ~]# ansible A -m ping
test2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
test1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@ansible ~]# ansible B -m ping
test3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
-
配置名单使用的是INI的配置风格
-
/etc/ansible/hosts不仅支持INI的配置语法,还能识别“TAML”的配置语法
-
使用"YAML"语法来编写清单
-
使用YAM了的语法格式
[root@ansible Software]# tail -n 15 /etc/ansible/hosts
#[B]
#test3 ansible_host=192.168.13.133 ansible_port=22 ansible_user=root ansible_ssh_pass=123456
all:
hosts:
# 192.168.13.130:
test1:
ansible_host: 192.168.13.132
ansible_port: 22
test2:
ansible_host: 192.168.13.134
ansible_port: 22
test3:
ansible_host: 192.168.13.133
ansible_port: 22
[root@ansible Software]#
- 测试
# ansible all -m ping
test3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
test1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
test2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}