清单文件位于/etc/ansible/hosts
[root@ansible ~]# vim /etc/ansible/hosts
[web_clust] //定义清单名
192.168.81.220 //主机ip
192.168.81.230
192.168.81.240
2.验证ansible是否可用
[root@ansible ~]# ansible web_clust -m ping
192.168.81.240 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.81.230 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.81.220 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
如果提示permission denied(publickey.gssapi-keyex…)表示ssh生成的公钥有问题
3.ansible命令语法格式
ansible 主机组模块名 -m 模块名 -a 指定利用模块执行的动作参数 批量执行操作动作
command模块和shell模块的区别
command模块只是调用一个命令,不能添加类似|、&&、;、>这种的符号,也不能调用命令别名,shell模块相当于提供一个shell环境,可以使用管道、定向、并且等这种逻辑性的符号
3.1.获取主机清单中所有服务器的主机名
[root@ansible ~]# ansible web_clust -m command -a "hostname"
192.168.81.230 | CHANGED | rc=0 >>
nfs
192.168.81.240 | CHANGED | rc=0 >>
backup
192.168.81.220 | CHANGED | rc=0 >>
web
3.2.获取主机清单中所有服务器的ip地址
[root@ansible ~]# ansible web_clust -m shell -a "ifconfig ens33 | awk '{if(NR==2){print $1}}'"
192.168.81.240 | CHANGED | rc=0 >>
inet 192.168.81.240 netmask 255.255.255.0 broadcast 192.168.81.255
192.168.81.230 | CHANGED | rc=0 >>
inet 192.168.81.230 netmask 255.255.255.0 broadcast 192.168.81.255
192.168.81.220 | CHANGED | rc=0 >>
inet 192.168.81.220 netmask 255.255.255.0 broadcast 192.168.81.255
4ansible清单配置
inventory文件通常用于定义要管理主机的认证信息,例如ssh登录用户名、登录密码以及key相关信息。清单位置文件位于/etc/ansible/hosts
可以配置主机、主机组
主机:
1.支持主机名通配以及正则表达式web[1:3].oldboy.com/192.168.81.2[2:4]
2.支持基于非标准的ssh端口,例如web1.oldboy.com:666
3.支持指定变量,可以对个别主机进行特殊配置
主机组:
1.支持嵌套组,例如[web_clust:children],web_clust模块下的所有主机都会被包含
2.指定变量,例如[web_clust:vars]在下面指定变量将会对所有web_clust模块中的所有主机生效
4.1.清单配置实例
添加三台主机至web_clust(升级版)
[root@ansible ~]# vim /etc/ansible/hosts
[web_clust]
192.168.81.220
192.168.81.230
192.168.81.240
添加三台主机至web_clust(升级版)
[root@ansible ~]# vim /etc/ansible/hosts
[web_clust]
192.168.81.2[2:4]0
4.2.清单配置实例2
添加三台主机并指定密码(初版)
[root@ansible ~]# vim /etc/ansible/hosts
[web_clust]
192.168.81.220 ansible_ssh_pass='redhat'
192.168.81.230 ansible_ssh_pass='redhat'
192.168.81.240 ansible_ssh_pass='redhat'
添加三台主机并指定密码(升级版)
[root@ansible ~]# vim /etc/ansible/hosts
[web_clust]
192.168.81.2[2:4]0
[web_clust:vars]
ansible_ssh_pass='redhat'
添加三台主机并指定密码(拆分版)
[root@ansible ~]# vim /etc/ansible/hosts
[web_clust]
192.168.81.220
192.168.81.230
192.168.81.240
[web_clust:vars]
ansible_ssh_pass='redhat'
5.ansible清单配置真实案例
5.1.环境概述

5.2.安装ansible
[root@ansible ~]# yum -y install ansible
[root@ansible ~]# ansible --version
ansible 2.9.9
5.3.配置主机清单
常用的配置是对应的主机做成对应的模块,然后将所有模块加入到主机组中
[root@ansible ~]# vim /etc/ansible/hosts
[web]
192.168.81.220
[nfs]
192.168.81.230
[mysql]
192.168.81.240
[web_clust:children]
web
nfs
backup
5.4.验证
既可以对整个组进行操作
[root@ansible ~]# ansible web_clust -m ping
192.168.81.230 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.81.240 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.81.220 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
也可以对单个模块进行操作
[root@ansible ~]# ansible web -m ping
192.168.81.220 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@ansible ~]# ansible nfs -m ping
192.168.81.230 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@ansible ~]# ansible mysql -m ping
192.168.81.240 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
6.主机清单常用变量