- 安装
yum -y install ansible
ansible –version
- 添加控制机器
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.100.20
- 添加到hosts文件中
vim /etc/ansible/hosts
Ansible 通过设备列表以分组的方式添加到 /etc/ansible/hosts 文件来实现对设备的管理,所以在正式管理之前,首先要编写好 hosts 文件。hosts 文件中,以[ ]包含的部分代表组名,设备列表支持主机名和IP地址。
默认情况下,通过访问22端口(SSH)来管理设备。若目标主机使用了非默认的SSH端口,还可以在主机名称之后使用冒号加端口标明,以行为单位分隔配置。另外,hosts文件还支持通配符。
[root@centos01 ~]# vim /etc/ansible/hosts
…………
[web]
192.168.100.20
192.168.100.30
[test]
www.benet.com:222
[mail]
yj1.kgc.cn
yj[2:5].kgc.cn
可以将一个主机同时归置在不同的组中。
配置完成之后,可以针对hosts定义的组进行远程操作,也可以针对组中的某一个或多个主机操作。例如:
1)只对web组中192.168.1.2主机操作,通过—limit参数限定主机的变更。
[root@centos01 ~]# ansible web -m command -a “systemctl status httpd” –limit “192.168.100.20”
192.168.100.20 | SUCCESS | rc=0 »
2)只对192.168.100.20主机操作。通过IP限定主机的变更。
[root@centos01 ~]# ansible 192.168.100.20 -m command -a “systemctl status httpd”
192.168.100.20 | SUCCESS | rc=0 »
3)只对192.168.100.0网段主机操作,这就需要使用到通配符来限定主机的变更了。
[root@centos01 ~]# ansible 192.168.1.* -m command -a “systemctl status httpd”
192.168.100.20 | SUCCESS | rc=0 »
…….
192.168.100.30 | SUCCESS | rc=0 »
…….