1 Host Inventory
Host Inventory 是配置文件,用来告诉Ansible需要管理哪些主机。并且把这些主机根据按需分类。
可以根据用途分类:数据库节点,服务节点等;根据地点分类:中部,西部机房。
默认的文件是: /etc/ansible/hosts
最简单的hosts文件:
192.168.1.50
aserver.example.org
bserver.example.org
带分类的hosts文件:
mail.example.com
[webservers]
foo.example.com
bar.example.com
[dbservers]
one.example.com
two.example.com
three.example.com
配置此文件:
vim /etc/ansible/hosts
[web1]
192.168.143.192
[web2]
192.168.143.194
:wq
其他常用配置文件:
/etc/ansible:主程序,临时命令执行工具
/etc/ansible-playbook:定制自动化任务,编排剧本工具
/etc/ansible-doc:查看配置文档,模块功能查看工具
/etc/ansible-galaxy:上传和下载优秀代码或者Roles模块的官网平台
/etc/ansible-pull:远程执行命令工具
/etc/ansible-console:基于console界面与用户交互的执行工具
2 Ansible 命令
ansible命令的格式是:
ansible <host-pattern> [options]
host-pattern:指定目标主机,可以与/etc/ansible/hosts的格式一样
-m:指定模块名
-a:指定模块参数
-k:输入此选项使用密码认证(不建议使用)
-b:指定sudo到哪个用户,如-b root
-C:检查,不执行
–list-hosts:查看主机列表,可简写–list
-v:显示详细的执行过程,要想更详细的信息-vv、-vvv
例子:
验证是否连通web1 web2
[root@controller ~]# ansible web1 -m ping
192.168.143.192 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
[root@controller ~]# ansible web2 -m ping
192.168.143.194 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
检查ansible安装环境
检查所有的远程主机,是否以root用户创建了ansible主机可以访问的环境。
[root@controller ~]# ansible all -m ping -u root
192.168.143.194 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
192.168.143.192 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
在所有的远程主机上,以当前bash的同名用户,在远程主机执行“echo bash”
[root@controller ~]# ansible all -a "/bin/echo hello"
192.168.143.192 | CHANGED | rc=0 >>
hello
192.168.143.194 | CHANGED | rc=0 >>
hello
拷贝文件/etc/host到远程主机(组)web1,位置为/tmp/hosts
[root@controller ~]# ansible web1 -m copy -a "src=/etc/hosts dest=/tmp/hosts"
192.168.143.192 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "606d67a1d2bafa4b7b30cad2fd321070d057af41",
"dest": "/tmp/hosts",
"gid": 0,
"group": "root",
"md5sum": "6fee9ae14c8e52ee1f552e29a88b9e7f",
"mode": "0644",
"owner": "root",
"size": 263,
"src": "/root/.ansible/tmp/ansible-tmp-1649314945.6000264-3681-98402353471501/source",
"state": "file",
"uid": 0
}
远程主机(组)web1安装yum包tree
[root@controller ~]# ansible web1 -m yum -a "name=tree state=present"
192.168.143.192 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: tree-1.7.0-15.el8.x86_64"
]
}
查看远程主机的全部系统信息
[root@controller ~]# ansible all -m setup
192.168.143.192 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"10.10.20.192"
],
"ansible_all_ipv6_addresses": [
"fe80::f816:3eff:fe47:1c0b"
],
"ansible_apparmor": {
"status": "disabled"
},
"ansible_architecture": "x86_64",
"ansible_bios_date": "04/01/2014",
"ansible_bios_version": "1.11.0-2.el7",
"ansible_brq18557e19_f2": {
"active": true,
"device": "brq18557e19-f2",
"features": {
......