一、Ansible相关
1、简介
Ansible是自动化运维工具,基于Python开发,分布式,无需客户端,轻量级,实现了批量系统配置、批量程序部署、批量运行命令等功能,ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。
2、Ansible特性
1)、no agents:不需要在被管控主机上安装任何客户端,更新时,只需在操作机上进行一次更新即可(不用安装客户端。分布式的)
2)、no server:无服务器端,使用时直接运行命令即可
3)、modules in any languages:基于模块工作,可使用任意语言开发模块
4)、yaml,not code:使用yaml语言定制剧本playbook
5)、ssh by default:基于SSH工作
6)、strong multi-tier solution:可实现多级指挥
3、Ansible常用模块
语法结构
## ansible <pattern> -m <module_name> -a <arguments>
pattern--主机清单里定义的主机组名,主机名,IP,别名等,all表示所有的主机,支持通配符,正则
-m module_name: 模块名称,默认为command
-a arguments: 传递给模块的参数
-o 单行显示
ping模块
##ping模块使用扩展
1.指定单台机器:
[root@ansible-server ~]# ansible ansible-web1 -m ping -o
2.同时指定多台机器:
[root@ansible-server ~]# ansible ansible-web1,ansible-web2 -m ping -o
3.指定组名:
[root@ansible-server ~]# ansible webservers1 -m ping -o
[root@ansible-server ~]# ansible all -m ping -o
copy模块
1.远程复制备份模块:copy
模块参数详解:
src=:指定源文件路径
dest=:目标地址(拷贝到哪里)
owner:指定属主
group:指定属组
mode:指定权限,可以以数字指定比如0644
backup:在覆盖之前将原文件备份,备份文件包含时间信息。有两个选项:yes|no
[root@ansible-server ~]# vim a.txt #创建一个测试文件
123123
[root@ansible-server ~]# ansible ball -m copy -a 'src=/root/a.txt dest=/opt owner=root group=root mode=644' -o
[root@ansible-server ~]# vim a.txt #追加如下内容
123123
234234
[root@ansible-server ~]# ansible ball -m copy -a 'src=/root/a.txt dest=/opt/ owner=root group=root mode=644 backup=true' -o
注释:如果文件没有变化,不会备份。只有文件内容不同,才会做备份。
登录被控制机器其中一台查看
[root@ansible-web1 ~]# cat /opt/a.txt.15301.2019-09-01\@00\:35\:18~
[root@ansible-server ~]# ansible weball -m shell -a 'mv /mnt/qf.txt /tmp' -o
移动被控制节点的文件
yum模块
2.软件包管理 yum模块
安装apache
[root@ansible-server ~]# ansible webservers1 -m yum -a "name=httpd state=latest" -o
state= #状态是什么,干什么
state=absent 用于remove安装包
state=latest 表示最新的
state=removed 表示卸载
卸载软件:
[root@ansible-server ~]# ansible webservers1 -m yum -a "name=httpd state=removed" -o
或者
[root@ansible-server ~]# ansible webservers1 -m yum -a "name=httpd state=absent" -o
service模块
3.服务管理service模块
[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=started" #启动
[root@ansibl