软件安装
再此我们采用的是yum安装方法,命令如下
#yum install -y ansible
软件完整完毕之后,默认一般会在/etc/ansible目录下生成有如下文件
[root@bogon ~]# ls /etc/ansible/
ansible.cfg hosts roles
其中ansible.cfg为ansible主配置文件,hosts一般为默认的管理客户机文件
ansible主配置文件去掉注释内容如下
[root@bogon ~]# grep -Ev "#|^$" /etc/ansible/ansible.cfg
[defaults]
roles_path = /etc/ansible/roles:/usr/share/ansible/roles
[inventory]
[privilege_escalation]
[paramiko_connection]
[ssh_connection]
[persistent_connection]
[accelerate]
[selinux]
[colors]
[diff]
[root@bogon ~]#
修改管理主机配置文件/etc/ansible/hosts文件
在文件末尾直接添加如下内容:
#vi /etc/ansible/hosts
[web]
192.168.1.200
[windows]
192.168.1.105
进行ansible软件批量管理
ansible测试主机是否在线
[root@bogon ~]# ansible web -m ping
192.168.1.200 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n",
"unreachable": true
}
[root@bogon ~]#
解决步骤:
步骤一:管理节点生成ssh-key
#ssh-keygen
成功在~/.ssh/路径系生成ssh密钥文件:id_rsa及id_rsa.pub
步骤二:添加目标节点的SSH认证信息
#ssh-copy-id root@目标节点IP
这里root是在目标节点上登录的用户。@符号后面连接目标节点IP即可,之后会提示输入目标节点root用户密码,输入即可
处理好之后即可再次使用ansible的ping模块测试客户机是否在线
查看客户机的IP信息
[root@bogon ~]# ansible web -m command -a "ifconfig"
192.168.1.200 | SUCCESS | rc=0 >>
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.200 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::9a02:ba9b:6c82:a2d8 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:ed:73:45 txqueuelen 1000 (Ethernet)
RX packets 32500 bytes 11689302 (11.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 11438 bytes 1462522 (1.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 84 bytes 6392 (6.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 84 bytes 6392 (6.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
实现客户机软件安装
ansible web -m command -a "yum install -y httpd"