ansible的yum安装和常用重点命令使用
管理端:(ansible,192.168.231.128):
1)管理端yum安装ansible:
[root@localhost ~]# yum -y install epel-release
[root@localhost ~]# yum -y install ansible
2)管理端配置主机管理: 在hosts文件中添加管理主机的IP地址列表:
[root@localhost ~]# vim /etc/ansible/hosts
……
[test] #添加一个组名
192.168.231.129 #添加被管理主机的IP
192.168.231.137 #添加被管理主机的IP
……
3)管理端机器生成ssh密钥对,实现能无密码连接登录到被管理机器 (无密码登录,也可不配置,将密码写到文件里)
[root@localhost ~]# ssh-keygen -t rsa #下面一路回车,不用输密码
[root@localhost ~]# ls /root/.ssh/
id_rsa id_rsa.pub
[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.231.129 #或仅IP也可
[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.231.137 #或仅IP也可
注意:第一次需要输入对方用户密码:xxx
注意:ssh-copy命令格式有两种:1)ssh-copy-id 远端用户@远端IP #或仅IP
2)ssh-copy-id -i /root/.ssh/id_rsa.pub 远端用户@远端IP #或仅IP
ansible批量管理客户端的命令总结:
1)检查主机连接:
# ansible 单独模块名 -m ping //管理单独模块的ping,是否能通
# ansible all -m ping //管理所有模块的ping,是否能通
# ansible 192.168.40.134 -m ping //管理某个机器IP的ping,是否能通
2)执行远程命令: 以下的command也可以用shell代替
# ansible 单独模块名 -m command -a '执行的远程命令' //管理单独模块下机器,执行远程机器命令
# ansible all -m command -a '执行的远程命令' //管理所有模块下机器,执行远程机器命令
3)执行远程主机的脚本:
# ansible 单独模块名 -m shell -a 'sh shell脚本名 或 python python脚本名'
//管理单独模块下机器,执行远程机器脚本
# ansible all -m shell -a 'sh shell脚本名 或 python python脚本名'
//管理所有模块下机器,执行远程机器脚本
4)复制文件到远程服务器:
# ansible 单独模块名 -m copy -a "src=/路径/…ansible机器文件名 dest=/路径/…远端机器文件名"
//管理单独模块下机器,将复制文件到远端机器
# ansible all -m copy -a "src=/路径/…ansible机器文件名 dest=/路径/…远端文件名"
//管理所有模块下机器,将复制文件到远端机器
注意:所有被管理端需要安装:libselinux-python,即:yum -y install libselinux-python
注意事项:
1.当管理端向被管理端发送文件时候,在(被管理端)安装:libselinux-python,才能将管理端(ansible机器)上本地文件批量发送给被管理端,即: yum install libselinux-python -y
2.ansible机器也可不用创建密钥对,进行无秘钥连接被管理机器,只需要在配置文件/etc/ansible/hosts中添加各个被管理机器的密码也可(不过第一次连接时需要输入yes确认,后面就不需要了)
格式例子如:
[maya]
keeper-01 ansible_ssh_host="192.168.14.128" ansible_ssh_user="root" ansible_ssh_pass="123456"
maya-001-129 ansible_ssh_host="192.168.14.129" ansible_ssh_user="root" ansible_ssh_pass="123456"
[mem]
mem1 ansible_ssh_host="192.168.14.130" ansible_ssh_user="root" ansible_ssh_pass="123456"
mem2 ansible_ssh_host="192.168.14.131" ansible_ssh_user="root" ansible_ssh_pass="123456"
3.ansible机器可以对定义的整个模块批量操作管理机器,也可对某模块中定义的某个被管理机器单独进行操作
例子如:
[root@keeper-01 ~]# vim /etc/ansible/hosts
[maya]
keeper-01 ansible_ssh_host="192.168.14.128" ansible_ssh_user="root" ansible_ssh_pass="123456"
maya-001-129 ansible_ssh_host="192.168.14.129" ansible_ssh_user="root" ansible_ssh_pass="123456"
[mem]
mem1 ansible_ssh_host="192.168.14.130" ansible_ssh_user="root" ansible_ssh_pass="123456"
mem2 ansible_ssh_host="192.168.14.131" ansible_ssh_user="root" ansible_ssh_pass="123456"
#根据上面的模块,ansible想单独给mem1机器发送文件:
[root@keeper-01 ~]# ansible mem1 -m copy -a "src=/root/jenkins.war dest=/tmp/ROOT.war"
注意上面各模块下的第一个并不一定非要是主机名,不一定就要能解析,也不用必须要在/etc/hosts文件中将该名和对应的IP对应,而只是自己模块下定义的指定被管理机器的一个别名,只是为了好区分,将该别名一般和主机名设置的一样而已。
4.ansible批量发送文件时,远端机器ssh的端口号不是22,而是已经改变了的22115时候的配置
[root@keeper-01 ~]# vim /etc/ansible/hosts
[app-girl]
app-girl1 ansible_ssh_host="172.17.133.212" ansible_ssh_user="root" ansible_ssh_pass="b6eMWV2VQQ" ansible_ssh_port=22115
app-girl2 ansible_ssh_host="172.17.133.213" ansible_ssh_user="root" ansible_ssh_pass="C4NMcSyBrQ" ansible_ssh_port=22115
[root@keeper-01 ~]# ansible app-girl1 -m copy -a "src=/root/a.txt dest=/tmp/" #给机器app-girl1发送文件