运维工具Ansible(批量对远程服务器进行管理)简单使用教程(本示例采用CentOS7)

CentOS7 64bit 安装Ansible

1
yum install epel-release -y && yum install ansible -y

配置ansible

1
vi /etc/ansible/hosts

——————————————–
添加各IP服务器
其中本机为:
localhost

增加内容

1
2
[targets]
localhost ansible_connection=local

——————————————–
主控端生成密匙
生成key
ssh-keygen
ansible 推送公匙public key到其他远程服务器
ansible all -m authorized_key -a “user=root key='{{ lookup(‘file’,’~/.ssh/id_rsa.pub’)}}'”

——————————————–
验证是否与远程主机通信正常

1
ansible all -m ping

使用Ansible进行远程shell操作

1
ansible all -m shell -a "需要执行的命令"

例如让所有远程被控端服务器进行系统更新并安装wget curl git命令

1
ansible all -m shell -a "yum update -y && yum install wget curl git -y"

——————————————–
注:ansible all -m shell -a “需要执行的命令” 中的all为/etc/ansible/hosts中全部的远程被控端
如果在/etc/ansible/hosts对远程服务器进行了分组,则命令中的all可以替换为分组名称,分组进行操作.其他命令余同.

原文链接:https://xiaohost.com/2749.html,转载请注明出处。
0

评论0

请先