Ansible自动化运维工具

文章目录

一.Ansible自动化运维工具介绍

1.Ansible简介

(1)Ansible是基于模块工作的,只是提供了一种运行框架,本身没有完成任务的能力,真正操作的是Anisble的模块。每个模块都是独立的、实现了批量系统配置、批量程序部署、批量运行命令等功能。

(2)市场的工具有pubbet(ruby),saltstack(python)、chef、fabric都需要安装客户端。

(3)ansible只用ssh就可以使用

2.Ansible特点及优势

(1)特点

①部署简单,只需要主控端部署Ansible环境,被控端无需做任何操作;

②默认使用SSH协议设备进行管理;

③主从集中化管理;

④配置简单、功能强大、扩展性强;

⑤支持API及自定义模块,可以通过Python轻松扩展

⑥通过playbooks来定制强大的配置、状态管理

⑦对云平台和大数据都有很好的支持

(2)优点

①轻便性:无需在被控制服务器上安装客户端,Ansible基于ssh协议

②幂等性:大部分模块有幂等性,即如果输入systemctl stop firewalld当发现要停止的服务已经停止就不会做任何操作了,多次停止不会改变结果。systemtl restart是非幂等的。

③判断性:大部分模块在执行时都会判断目标节点是否要执行任务,所有重复执行某个任务大部分时间不会产生副作用

④简介性:一个窗口即可管理所有需要控制的机器,无需开启多个窗口

3.Ansible核心程序

①HostInventory:记录由Ansible管理的主机信息,端口,ip,密码等

②playbooks:'剧本'YAML格式文件,多任务定义在一个文件中,定义主机需要调用那些模块完成功能

③core modeules:核心模块主要操作通过调用核心模块来完成管理任务

④customodules:自定义模块,完成核心模块无法完成的模块,支持多种语言编写

⑤connectiontugins:连接插件,ansible和主机通信使用(ssh协议)

4.Ansible工作原理及流程

①加载自己的配置文件,默认/etc/ansible/ansible.cfg

②查找对应的主机的配置文件,找到要执行的主机或组/etc/ansible/hosts文件

③加载自己对应的模块文件,如command、yum、ping、

④通过ansible将模块命令生成对应的临时py文件(类似python脚本),并将该文件传输至被管理端

⑤传输到在被控制端的对应用户的家目录下.ansible/tmp/xxx/xxx.py

⑥被控制端给传输过来的py文件加执行权限

⑦执行并返回结果,执行完成后删除py文件并sleep 0退出

二.部署Ansible自动化运维工具

管理 IP地址 安装软件
管理端 192.168.198.11 ansible
被管理端 192.168.198.12
被管理端 192.168.198.13

1.管理端安装 ansible(192.168.198.11)

复制代码
yum install  -y epel-release 
#安装epel-release
yum install -y  ansible
#安装ansible
#配置文件位置:/etc/ansible/ansible
#hosts文件位置:/etc/ansible/hosts
#公共角色目录:roles/

vim /etc/ansible/hosts
#编辑hosts文件添加被管理的机器,内容如下
#配置组名
[webservers]
#组里包含的被管理的主机IP地址或主机名(主机名需要先修改/etc/hosts文件)
192.168.198.12
[dbservers]
192.168.198.13

#配置密钥对验证
#生成公钥,输入命令输入4个回车即可
cd ~/.ssh
ssh-copy-id -i   192.168.198.12
ssh-copy-id -i   192.168.198.13
#进入生成的公钥路径将公钥传输给备管理的服务器,传输需要输入每台备管理服务的root密码
ansible ansible-doc  -l 
#安装完毕,此命令可以查看有哪些ansible模块,按q退出
ansible  webservers  -m command  -a 'pwd'
#安装完成尝试管理webservers组输入pwd命令
ansible  all   -a 'ls'
#安装完成尝试管理所有hosts中主机输入ls命令,不指定模块默认为command模块

2.Ansible 命令行模块

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

复制代码
#列出所有已安装的模块,按q退出
ansible-doc -l				

(1)command 模块

在远程主机执行命令,不支持管道,重定向等shell的特性。

复制代码
#-s 列出指定模块的描述信息和操作动作,q退出
ansible-doc -s command		

#指定 ip 执行 date
ansible 192.168.198.12 -m command -a 'date'	

#指定组执行 date	
ansible webservers -m command -a 'date'	

#指定组执行 date			
ansible dbservers -m command -a 'date'    

#all 代表所有 hosts 主机   
ansible all -m command -a 'date'

#如省略 -m 模块,则默认运行 command 模块				
ansible all -a 'ls /'	


常用的参数:

参数 含义
chdir 在远程主机上运行命令前提前进入目录
creates 判断指定文件是否存在,如果存在,不执行后面的操作
removes 判断指定文件是否存在,如果存在,执行后面的操作
复制代码
ansible all -m command -a "chdir=/home  ls ./"

(2)shell 模块

在远程主机执行命令,相当于调用远程主机的shell进程,然后在该shell下打开一个子shell运行命令(支持管道符号等功能)

复制代码
ansible-doc -s shell

举例:

复制代码
#给test设置密码之前,查看dbservers模块下的被管控主机是否有test用户
ansible dbservers -m shell -a 'echo 123456 | passwd --stdin test'
复制代码
ansible dbservers -m shell -a 'echo $(ifconfig ens33 | awk "NR==2 {print $2}") | cut -d " " -f2'
ansible dbservers -m shell -a 'echo $(ifconfig ens33 | awk "NR==2 {print \$2}")'

(3)cron 模块

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

复制代码
#cron模块,按 q 退出
ansible-doc -s cron	

常用参数:

参数 注释
minute/hour/day/month/weekday 分/时/日/月/周
job 任务计划要执行的命令
name 任务计划的名称

举例:

复制代码
ansible webservers -m cron -a 'minute="*/1" job="/bin/echo helloworld" name="test crontab"'

ansible webservers -a 'crontab -l'
ansible webservers -m cron -a 'name="test crontab" state=absent'		#移除计划任务,假如该计划任务没有取名字,name=None即可

(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时,是否删除用户的家目录

举例:

复制代码
#创建用户test01
ansible dbservers -m user -a 'name="test01"'				
复制代码
ansible dbservers -m command -a 'tail /etc/passwd'
复制代码
#删除用户test01
ansible dbservers -m user -a 'name="test01" state=absent'	

(5)group 模块

用户组管理的模块

复制代码
ansible-doc -s group

#创建mysql组
ansible dbservers -m group -a 'name=mysql gid=306 system=yes'	

ansible dbservers -a 'tail /etc/group'
复制代码
#将test01用户添加到mysql组中
ansible dbservers -m user -a 'name=test01 uid=306 system=yes group=mysql'	

ansible dbservers -a 'tail /etc/passwd'
ansible dbservers -a 'id test01'  

(6) copy 模块

用于复制指定主机文件到远程主机的

复制代码
ansible-doc -s copy

常用的参数:

举例:

复制代码
#将etc/fstab下的复制到opt/fstab.bak给予root属主、权限640
ansible dbservers -m copy -a 'src=/etc/fstab dest=/opt/fstab.bak owner=root mode=640'
复制代码
#详细查看opt下的文件
ansible dbservers -a 'ls -l /opt'
复制代码
#查看新创建的文件内容
ansible dbservers -a 'cat /opt/fstab.bak'
复制代码
#将helloworld写入/opt/hello.txt文件中
ansible dbservers -m copy -a 'content="helloworld" dest=/opt/hello.txt'
复制代码
#查看写入的文件
ansible dbservers -a 'cat /opt/hello.txt'

(7)file模块

设置文件属性

复制代码
#查看file模块下的功能,按q退出
ansible-doc -s file

举例:

复制代码
#修改文件的属主属组权限等
ansible dbservers -m file -a 'owner=test01 group=mysql mode=644 path=/opt/fstab.bak'
#查看修改后的信息
ansible dbservers -m command -a 'chdir=/opt ls -lh ./'


复制代码
#设置/opt/fstab.link为/opt/fstab.bak的链接文件
ansible dbservers -m file -a 'path=/opt/fstab.link src=/opt/fstab.bak state=link'
#查看修改后的信息
ansible dbservers -m command -a 'chdir=/opt ls -lh ./'


复制代码
#创建一个文件
ansible dbservers -m file -a "path=/opt/abc.txt state=touch"
#查看修改后的信息
ansible dbservers -m command -a 'chdir=/opt ls -lh ./'
复制代码
#删除一个文件
ansible dbservers -m file -a "path=/opt/abc.txt state=absent"
#查看删除后的信息
ansible dbservers -m command -a 'chdir=/opt ls -lh ./'


(8)hostname模块

用于管理远程主机上的主机名

复制代码
ansible dbservers -m hostname -a "name=mysql01"

(9)ping模块

检测远程主机的连通性

复制代码
ansible all -m ping

(10)yum 模块

在远程主机上安装与卸载软件包

复制代码
ansible-doc -s yum

常用参数:

举例:

复制代码
#安装hyyps服务
ansible dbservers -m yum -a "name=httpd"
复制代码
#查看安装服务的状态
nsible dbservers -m command -a 'service httpd status'
复制代码
#卸载httpd服务
ansible dbservers -m yum -a "name=httpd state=absent"
复制代码
#查看卸载的httpd服务
ansible dbservers -m command -a 'service httpd status'


(11)service/system模块

管理远程被控制主机上的管理服务的运行状态

常用参数:

举例:

复制代码
#下载httpd服务
ansible dbservers -m yum -a "name=httpd"
#设置开机自启,服务的状态为开启
ansible dbservers -m service -a 'enabled=yes  name=httpd state=started'

(12)script模块

实现远程批量运行本地的shell脚本

复制代码
#查看模块下的功能
ansible-doc -s script

ansible服务器:
vim  /test.sh
#编写/下的test.sh脚本内容如下
#!/bin/bash
echo  "this is test"

chmod +x /test.sh
ansible webservers -m script -a "/test.sh"

(13)setup模块

setup 模块可以获取这些信息 facts 组件收集d 被管理节点信息

参数:filter 过滤可配合正则表达式。

复制代码
ansible webservers -m setup -a 'filter=*ipv4'

3.hostsinverntory主机清单

hosts配置文件位置:/etc/ansible/hosts;

Inventory支持对主机进行分组,每个组内可以定义多个主机,每个主机都可以定义在任何一个或多个主机组内

(1)inventory 中的变量含义

(2)配置

复制代码
#新添加一台机器192.168.198.14
#ansible主机上连接14机器,连接成功
ssh 192.168.198.14

#登出test4机器
exit
复制代码
#ansible主机配置
#如果是名称类似的主机,可以使用列表的方式标识各个主机。
vim /etc/ansible/hosts
[webservers]
192.168.198.12:22		#冒号后定义远程连接端口,默认是 ssh 的 22 端口
192.168.146.1[2:3]

[dbservers]
db-[a:f].example.org	#支持匹配 a~f

(1)主机变量
[webservers]
192.168.198.13 ansible_port=22 ansible_user=root ansible_password=000000
ansible webservers -a 'ls -lh /home'
复制代码
(2)组变量
[webservers]
192.168.198.13
#表示为 webservers 组内所有主机定义变量
[webservers:vars]			
ansible_user=root
ansible_password=000000

[all:vars]					#表示为所有组内的所有主机定义变量
ansible_port=22
复制代码
ansible webservers -a 'ls -lh /home'
复制代码
(3)组嵌套
[nginx]
192.168.198.12
192.168.198.13
192.168.198.14

[apache]
192.168.198.3[0:3]

#表示为 webs 主机组中包含了 nginx 组和 apache 组内的所有主机
[webs:children]		
nginx
apache
相关推荐
ZFB00015 小时前
【麒麟桌面系统】V10-SP1 2503 系统知识——设置面板无法打开
linux·运维·kylin
2301_780943846 小时前
linux 对文件打补丁(Patch)
linux·运维·服务器
ICT董老师6 小时前
通过kubernetes部署nginx + php网站环境
运维·nginx·云原生·容器·kubernetes·php
敬往事一杯酒哈6 小时前
Ubuntu 20.04 安装Anacada
linux·运维·ubuntu
还在忙碌的吴小二6 小时前
Jenkins CLI (jcli) 使用手册
运维·jenkins
ChangYan.6 小时前
Windows命令行(cmd)下快速查找文件路径(类似Linux下find命令)
linux·运维·服务器
陈让然6 小时前
VS Code新版本无法连接WSL ubuntu18.04
linux·运维·ubuntu
lpfasd1236 小时前
宝塔面板使用流程及注意事项
运维
小杰帅气7 小时前
神秘的环境变量和进程地址空间
linux·运维·服务器