ansible批量管理工具

简介:

当下有许多的运维自动化工具( 配置管理 ),例如:Ansible、SaltStack、Puppet、Fabric 等。

Ansible 一种集成 IT 系统的配置管理、应用部署、执行特定任务的开源平台,是 AnsibleWorks 公司名下的项目,该公司由 Cobbler 及 Func 的作者于 2012 年创建成立。

Ansible 基于 Python 语言实现,由 Paramiko 和 PyYAML 两个关键模块构建。

Ansible 特点:

>> 部署简单,只需在主控端部署 Ansible 环境,被控端无需做任何操作。
>> 默认使用 SSH(Secure Shell)协议对设备进行管理。
>> 主从集中化管理。
>> 配置简单、功能强大、扩展性强。
>> 支持 API 及自定义模块,可通过 Python 轻松扩展。
>> 通过 Playbooks 来定制强大的配置、状态管理。
>> 对云计算平台、大数据都有很好的支持。
>> 提供一个功能强大、操作性强的 Web 管理界面和 REST API 接口 ---- AWX 平台。

首先准备三台服务器,一台作为管理机,另外两台作为被管理机,并且管理机对被管理机做了公钥的分发。

在管理机上
[root@jinchuan ~]# yum -y install epel-release  安装和epel有关的所有yum源
[root@jinchuan ~]# yum -y install ansible  安装ansible
[root@jinchuan ~]# > /etc/ansible/hosts  清空ansible的配置文件,把权限改为600
[root@jinchuan ~]# vim hosts

[Nginx]
WebA ansible_ssh_host=192.168.200.131
WebB ansible_ssh_host=192.168.200.132

配置完成了
[root@jinchuan ~]# ansible all -m ping 如果有很多模块all就是指所有模块下的所有被管理机,检测对方在不在线

command模块:可以执行很多命令,但是不支持管道符和重定向
[root@jinchuan ~]# ansible all -m command -a 'hostname -I'  获取所有被管理机的IP

shell模块:完全可以取代command模块,支持绝大多数命令
[root@jinchuan ~]# ansible all -m shell -a 'echo "hello" > /tmp/test'

cron模块:定时任务模块
[root@jinchuan ~]# ansible all -m cron -a 'minute="*/5" day="*/10" job="/bin/echo hello" name="test cron job"'
给所有被管理机root用户添加定时任务,每十天,每五分钟,执行一遍echo hello这个命令,name是这个定时任务的描述
时间顺序是分时日月周,如果不写默认就是*
[root@jinchuan ~]# ansible all -m cron -a 'name="test cron job" state="absent"'  所有被管理机root用户删除任务表述为test cron job的定时任务

copy模块:复制模块
[root@jinchuan ~]# ansible all -m copy -a 'src=/tmp/test dest=/root'  将管理机的/tmp/test复制到被管理机的/root下
[root@jinchuan ~]# ansible all -m copy -a 'src-/tmp/test dest=/root backup=yes'  这是如果复制时有重名的文件,先进行备份在复制,否则直接覆盖了

script模块:脚本模块
[root@jinchuan ~]# ansible all -m script -a '/tmp/test.sh'  在管理机上执行这个脚本,会在被管理机的内存里执行,不会在被管理机里留下错操作痕迹

yum模块
[root@jinchuan ~]# ansible all -m yum -a 'name=finger'  给所有被管理机用yum安装finger
[root@jinchuan ~]# ansible all -m yum -a 'name=finger state absent'  给所有被管理机用yum卸载finger

user模块:用户模块
[root@jinchuan ~]# ansible all -m user -a 'name=jinchuan uid=501 password=必须是被加密的密文 shell=/bin/bash home=/home/jinchuan'
给所有被管理机创建普通用户jinchuan,指定uid,加密的密文,可以登录,家目录的位置。
要生成加密的密文需要进行一下步骤
[root@jinchuan ~]# yum -y install epel-release  安装epel有关的yum源
[root@jinchuan ~]# yum -y install python-pip  安装python
[root@jinchuan ~]# pip install passlib  安装python的支持程序
[root@jinchuan ~]# python -c "from passlib.hash import sha512_crypt;import getpass;print sha512_crypt.encrypt(getpass.getpass())"
回车后输入密码,再回车,就会生成密文。
[root@jinchuan ~]# ansible all -m user -a 'name=jinchuan state=absent remove=true'  删除被管理机的jinchuan普通用户并删除其家目录

setup模块:数据收集模块
[root@jinchuan ~]# ansible all -m setup | less  一页一页看到被管理机的详细信息

service模块:服务模块
[root@jinchuan ~]# ansible all -m service -a 'name=firewalld state=started enabled=true'  启动所有被管理机的防火墙并设置开机启动
[root@jinchuan ~]# ansible all -m service -a 'name=firewalld state=stopped enabled=false'  关闭所有被管理机的防火墙并关闭开机启动


ansible常用的用法
批量下发文件、脚本和目录
给多台服务器创建程序用户
给多台服务器修改root密码
给多台服务器创建普通用户并设置密码及sudo授权
给多台服务器下发定时任务
批量获取CPU MEM I/O信息
对远程服务器进行批量系统初始优化及服务器基本安全加固

原文地址:https://www.cnblogs.com/jinchuan16/p/9947128.html