自动化运维工具——ansible

发布于:2024-10-18 ⋅ 阅读:(6) ⋅ 点赞:(0)

一、Ansible概念

Ansible是一个基于Python开发的配置管理和应用部署工具,现在也在自动化管理领域大放异彩。它融合了众多老牌运维工具的优点,Pubbet和Saltstack能实现的功能,Ansible基本上都可以实现。
Ansible能批量配置、部署、管理上千台主机。比如以前需要切换到每个主机上执行的一或多个操作,使用Ansible只需在固定的一台Ansible控制节点上去完成所有主机的操作。

Ansible是基于模块工作的,它只是提供了一种运行框架,它本身没有完成任务的能力,真正执行操作的是Ansible的模块, 比如copy模块用于拷贝文件到远程主机上,service模块用于管理服务的启动、停止、重启等。

Ansible其中一个比较鲜明的特性是Agentless,即无Agent的存在,它就像普通命令一样,并非C/S软件,也只需在某个作为控制节点的主机上安装一次Ansible即可,通常它基于ssh连接来控制远程主机,远程主机上不需要安装Ansible或其它额外的服务

使用者在使用时,在服务器终端输入命令或者playbooks,会通过预定好的规则将playbook拆解为play,再组织成ansible可以识别的任务,调用模块和插件,根据主机清单通过SSH将临时文件发给远程的客户端执行并返回结果,执行结束后自动删除

Ansible的另一个比较鲜明的特性是它的绝大多数模块都具备幂等性(idempotence)。所谓幂等性,指的是多次操作或多次执行对系统资源的影响是一致的。比如执行 systemctl stop xxx 命令来停止服务,当发现要停止的目标服务已经处于停止状态, 它什么也不会做,所以多次停止的结果仍然是停止,不会改变结果,它是幂等的,而 systemctl restart xxx 是非幂等的。

Ansible的很多模块在执行时都会先判断目标节点是否要执行任务,所以,可以放心大胆地让Ansible去执行任务,重复执行某个任务绝大多数时候不会产生任何副作用。

ansible 具有如下特点:
1、部署简单,只需在主控端部署Ansible环境, 被控端无需做任何操作
2、默认使用SSH协议设备进行管理;
3、主从集中化管理
4、配置简单、功能强大、扩张性强;
5、支持API及自定义模块,可以通过Pyhton轻松扩展
6、通过playbooks 来定制强大的配置、状态管理
7、对云计算平台、大数据都有很好的支持

ansible通过ssh对目标主机进行配置,应用部署、任务执行、编排调度等一些操作。简化了复杂的环境管理和自动化任务,提高了工作效率和一致性。同时ansible的剧本(playbooks)可使用YAML语言进行编写,易于拓展和维护

Ansible工作机制:
用户请求ansible,ansible读取配置文件,然后ansible会抓取全部机器或者分组列表,然后运行操作的核心模块(也可以是剧本),然后运行ssh协议到达对应的主机,去执行核心模块。如果使用的是剧本(playbooks用于存放剧本),那么剧本会去触发操作的核心模块
在这里插入图片描述
Ansibel核心程序:
1.hostINventory 记录ansible管理所有的主机信息(包括IP端口 密码等)
2.playbooks “剧本” YMAL 格式文件,多个任务定义在一个文件中,定义主机需要使用哪些模块来完成的功能
3.core modules 核心模块,主要操作是通过调用内核,核心模块和管路任务(你要操作的命令)
4.customodules 自定义模块,来完成核心模块无法完成的功能
5.connectionPlugins:连接插件,Ansible和HOST主机通信使用

二、ansible实验

1.安装ansible

yum install -y epel-release
yum install -y ansible
cd /etc/ansible/
cp hosts{,_bak}
vim /etc/ansible/hosts 

[webservers]			#配置组名
192.168.44.10			#组里包含的被管理的主机IP地址或主机名(主机名需要先修改/etc/hosts文件)


[dbservers]             #配置第二个组名
192.168.44.40

在这里插入图片描述

2.配置密钥对验证

ssh-keygen -t rsa		#一路回车,使用免密登录
ssh-copy-id root@192.168.44.10
ssh-copy-id root@192.168.44.40

在这里插入图片描述

3.ansible 命令行模块

命令格式:ansible <组名/IP> -m <模块> -a <参数列表>

1.command 模块

ansible-doc -l				#列出所有已安装的模块,按q退出
ansible 192.168.44.10 -m command -a 'date'		#指定 ip 执行 date
ansible webservers -m command -a 'date'			#指定组执行 date
ansible dbservers -m command -a 'date'       
ansible all -m command -a 'date'				#all 代表所有 hosts 主机
ansible all -a 'ls /'							#如省略 -m 模块,则默认运行 command 模块

在这里插入图片描述

//常用的参数:
chdir:在远程主机上运行命令前提前进入目录
creates:判断指定文件是否存在,如果存在,不执行后面的操作
removes:判断指定文件是否存在,如果存在,执行后面的操作

ansible webservers -m command -a 'chdir=/root ls'
ansible webservers -m command -a 'creates=/root1 ls'
ansible webservers -m command -a 'removes=/root ls'

在这里插入图片描述
在这里插入图片描述

2.shell 模块

ansible-doc -s shell             #查看shell模块的操作指令

在这里插入图片描述

ansible dbservers -m shell -a 'echo $(ifconfig ens33 | awk "NR==2 ") | cut -d " " -f2'
#查看ens33网卡,第二行,根据空格切割,显示第二列,为ip地址

在这里插入图片描述

ansible webservers -m shell -a 'grep "ERROR" /var/log/messages|tail -n 10'

在这里插入图片描述

3.cron 模块(计划任务)

//在远程主机定义任务计划。其中有两种状态(state):present表示添加(可以省略),absent表示移除。

ansible-doc -s cron				#按 q 退出

在这里插入图片描述

常用的参数:
minute/hour/day/month/weekday:分/时/日/月/周

ansible webservers -m cron -a 'minute="*/2" job="/bin/echo hi,this is cron" name="test1" '
ansible webservers -a 'crontab -l'

在这里插入图片描述
在这里插入图片描述
删除计划任务(假如该计划任务没有取名字,name=None即可)

ansible webservers -m cron -a 'name="test1" state=absent'

在这里插入图片描述

4.user 模块

用户管理的模块
ansible-doc -s user

常用的参数:
name:用户名,必选参数
state=present|absent:创建账号或者删除账号,present表示创建,absent表示删除
system=yes|no:是否为系统账号
uid:用户uid
group:用户基本组
shell:默认使用的shell
move_home=yse|no:如果设置的家目录已经存在,是否将已经存在的家目录进行移动
password:用户的密码,建议使用加密后的字符串
comment:用户的注释信息
remove=yes|no:当state=absent时,是否删除用户的家目录
ansible webservers -m user -a 'name=test1'                   #添加test1用户
ansible webservers -m command -a 'tail /etc/passwd'          #查看用户信息

在这里插入图片描述

ansible webservers -m user -a 'name=test1 state=absent'         #删除test1用户
ansible dbservers -m user -a 'name="test01" state=absent remove=yes'    ##删除test1用户,同时删除家目录

5.group 模块

用户组管理的模块
ansible-doc -s group
ansible webservers -m group -a 'name=abc gid=111 system=yes'       #组名abc,组ID为111,为系统组
ansible webservers -a 'tail /etc/group'

在这里插入图片描述

ansible webservers -m user -a 'name=test1 uid=222 system=yes group=abc'        #在abc组中添加test1用户

6.copy 模块

用于复制指定主机文件到远程主机的
ansible-doc -s copy

//常用的参数:
dest:指出复制文件的目标及位置,使用绝对路径,如果是源目录,指目标也要是目录,如果目标文件已经存在会覆盖原有的内容
src:指出源文件的路径,可以使用相对路径或绝对路径,支持直接指定目录,如果源是目录则目标也要是目录
mode:指出复制时,目标文件的权限 
owner:指出复制时,目标文件的属主
group:指出复制时,目标文件的属组
content:指出复制到目标主机上的内容,不能与src一起使用
ansible dbservers -m copy -a 'src=/etc/fstab dest=/opt/fstab.bak owner=root mode=640'   
#这是把自身目录的/etc/fstab复制到dbservers的/opt/fstab.bak文件中

ansible webservers -m copy -a 'src=/root/index.html dest=/opt/index.bak owner=root mode=777 remote_src=True'
# remote_src=True是让源目录为webservers的/root/index.html复制到自身的/opt/index.bak
ansible dbservers -a 'ls -l /opt'
ansible dbservers -a 'cat /opt/fstab.bak'

在这里插入图片描述
在这里插入图片描述

ansible dbservers -m copy -a 'content="abcdef" dest=/opt/1.txt'     #将helloworld写入/opt/hello.txt文件中
ansible dbservers -a 'cat /opt/1.txt' 

在这里插入图片描述

7.file 模块

设置文件属性
ansible-doc -s file
ansible dbservers -m file -a 'owner=yy group=yy mode=644 path=/opt/fstab.bak'	#修改文件的属主属组权限等
ansible dbservers -m file -a 'path=/opt/fstab.link src=/opt/fstab.bak state=link'    #设置/opt/fstab.link为/opt/fstab.bak的链接文件

在这里插入图片描述

ansible dbservers -m file -a "path=/opt/abc.txt state=touch"			#创建一个文件
ansible dbservers -m file -a "path=/opt/abc.txt state=absent"			#删除一个文件

在这里插入图片描述

8.hostname 模块

用于管理远程主机上的主机名
ansible dbservers -m hostname -a "name=yy1"

在这里插入图片描述

9.ping 模块

检测远程主机的连通性
ansible all -m ping

在这里插入图片描述

10.yum 模块

在远程主机上安装与卸载软件包
ansible-doc -s yum
ansible webservers -m yum -a 'name=nginx'					#安装服务
ansible webservers -m yum -a 'name=nginx state=absent'		#卸载服务

在这里插入图片描述

11.service/systemd 模块

用于管理远程主机上的管理服务的运行状态
ansible-doc -s service

常用的参数:
name:被管理的服务名称
state=started|stopped|restarted:动作包含启动关闭或者重启
enabled=yes|no:表示是否设置该服务开机自启
runlevel:如果设定了enabled开机自启去,则要定义在哪些运行目标下自启动
ansible webservers -a 'systemctl status httpd'			#查看web服务器httpd运行状态
ansible webservers -m service -a 'enabled=true name=httpd state=started'			#启动httpd服务

在这里插入图片描述

12.script 模块

实现远程批量运行本地的 shell 脚本
ansible-doc -s script
vim test.sh
#!/bin/bash
echo "this is shell" > /opt/script.txt

chmod +x test.sh
ansible webservers -m script -a 'test.sh'
ansible webservers -a 'cat /opt/script.txt'

在这里插入图片描述

13.setup 模块

facts 组件是用来收集被管理节点信息的,使用 setup 模块可以获取这些信息
ansible-doc -s setup
ansible webservers -m setup				#获取mysql组主机的facts信息
ansible dbservers -m setup -a 'filter=*ipv4'    #使用filter可以筛选指定的facts信息

在这里插入图片描述