Ansible自动化运维工具

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

Ansible的四大组件

Inventory 主机清单(主机组)

Modules 模块

Plugins 插件

Playbooks 剧本(相当于脚本)

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环境,被控制端上只要求安装ssh和python 2.5以上版本,对于运维人员使用门槛低。

2.被管控节点无需安装Agent

3.无服务端,使用是直接调用控制端命令或者脚本。

4.基于模块工作, 可以使用任意语言开发模块

5.基于yaml语法编写playbook

6.定义的任务已存在则不会做任何事情,意味着在同一台服务器上多次执行同一个playbook是安全的

缺点

1.学习成本:需要学习Ansible自定义的脚本语法规则。

2.安装成本:控制主机需是一台非Windows远程主机,也就是说至少要有1台远程Server。

3.易用性:因为必须有远程主控机,若本地有文件需同步,则需先将文件传输到对应的主控机,才能够做分发同步。

ansible 环境安装部署

管理端安装 ansible

yum install -y epel-release //先安装 epel 源

yum install -y ansible

配置主机清单

cd /etc/ansible

vim hosts

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

192.168.73.107

ansible默认使用ssh连接,所以管理前要设置免密登录

#配置密钥对验证

ssh-keygen -t rsa #一路回车,生成密钥文件

vim /etc/ssh/ssh_config #修改ssh服务端和ssh客户端配置文件

StrictHostKeyChecking no #35行,取消注释,将ask修改为no,开启免交互

systemctl restart sshd #重启sshd

//配置密钥对验证

ssh-keygen -t rsa #一路回车,使用免密登录

sshpass -p 'root的密码' ssh-copy-id root@20.0.0.102

sshpass -p 'root的密码' ssh-copy-id root@20.0.0.103

ansible 命令行模块

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

常用的的ansible模块

(1) command 模块

一般用于执行linux的命令,不支持管道符和重定向。

ansible-doc -s command #-s 列出指定模块的描述信息和操作动作

ansible 192.168.73.106 -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:在远程主机上运行命令前提前进入目录

ansible all -m command -a "chdir=/home ls ./"

creates:判断指定文件是否存在,如果存在,不执行后面的操作

removes:判断指定文件是否存在,如果存在,执行后面的操作

ansible all -m command -a "creates=/opt/1.txt ls /opt"

ansible all -m command -a "remove=/opt/1.txt ls /opt"

(2) shell 模块

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

ansible-doc -s shell

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表示移除。

ansible-doc -s cron #按 q 退出

//常用的参数:

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即可。通常都需要取名,否则删除时所有None的计划任务都会被删除。

(4) user 模块

//用户管理的模块

ansible-doc -s user

//常用的参数:

name:用户名,必选参数

state=present|absent:创建账号或者删除账号,present表示创建,absent表示删除

system=yes|no:是否为系统账号

uid:用户uid

group:用户基本组

shell:默认使用的shell。shell=/sbin/nologin

move_home=yse|no:如果设置的家目录已经存在,是否将已经存在的家目录进行移动

password:用户的密码,建议使用加密后的字符串

comment:用户的注释信息

remove=yes|no:当state=absent时,是否删除用户的家目录

ansible dbservers -m user -a 'name="test01"' #创建用户test01

ansible dbservers -m user -a 'name="test01" state=absent' #删除用户test01

ansible dbservers -m command -a 'tail /etc/passwd'

(5) group 模块

//用户组管理的模块

ansible-doc -s group

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

ansible dbservers -a 'tail /etc/group'

ansible dbservers -m user -a 'name=test01 uid=306 system=yes group=test' #将test01用户添加到mysql组中

ansible dbservers -a 'tail /etc/passwd'

ansible dbservers -a 'id test01'

(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'

ansible dbservers -a 'ls -l /opt'

ansible dbservers -a 'cat /opt/fstab.bak'

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

ansible dbservers -a 'cat /opt/hello.txt'

#没有专门的mv模块

ansible dbservers -a 'mv /opt/123 /opt/123.txt'

(7) file 模块

ansible-doc -s file

ansible dbservers -m user -a 'name=test01 uid=306 system=yes group=test'

//设置文件属性

ansible dbservers -m file -a 'owner=test01 group=test 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=dbservers-user"

(9) ping 模块

//检测远程主机的连通性

ansible all -m ping

只有在/etc/absible/hosts文件当中声明的主机,ansible的服务端才可以进行远程操作。

(10) yum 模块

ansible-doc -s yum

ansible webservers -m yum -a 'name=httpd' #安装服务

ansible webservers -m yum -a 'name=httpd 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服务

ansible 192.168.233.20 -m shell -a 'echo "this is test" > /var/www/html/index.html'

ansible 192.168.233.20 -m shell -a 'curl 192.168.233.20'

ansible webservers -m yum -a 'name=epel-release'安装nginx

ansible webservers -m yum -a 'name=nginx'

ansible webservers -m service -a 'enabled=true name=nginx state=started'

ansible192.168.233.20 -m shell -a 'echo "this is nginx" > /usr/share/nginx/html/index.html'

ansible 192.168.233.20 -m shell -a 'curl 192.168.233.20'

(12) script 模块

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

ansible-doc -s script

vim test.sh

#!/bin/bash

echo "this is a test script" >/opt/test.text

chmod +x test.sh

ansible webservers -m script -a 'test.sh'

ansible webservers -a 'cat /opt/test.text'

(13) setup 模块

//facts 组件是用来收集被管理节点信息的,使用 setup 模块可以获取这些信息

ansible-doc -s setup

ansible webservers -m setup #获取mysql组主机的facts信息

ansible dbservers -m setup -a 'filter=*ipv4' #使用filter可以筛选指定的facts信息

ansible webservers -m setup -a 'filter=ansible_*processor*' #查看cpu

ansible webservers -m setup -a 'filter=ansible_mem*' #查看内存

ansible webservers -m setup -a 'filter=ansible_os_family' #查看系统

ansible webservers -m setup -a 'filter=ansible_proc_cmdline' #查看内核

ansible webservers -m setup -a 'filter=ansible_system' #查看系统信息

inventory 主机清单

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

//如果是名称类似的主机,可以使用列表的方式标识各个主机。

vim /etc/ansible/hosts

webservers

192.168.73.106:2222 #冒号后定义远程连接端口,默认是 ssh 的 22 端口

192.168.73.10[6:8]

dbservers

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

//inventory 中的变量

Inventory变量名 含义

ansible_host ansible连接节点时的IP地址

ansible_port 连接对方的端口号,ssh连接时默认为22

ansible_user 连接对方主机时使用的用户名。不指定时,将使用执行ansible或ansible-playbook命令的用户

ansible_password 连接时的用户的ssh密码,仅在未使用密钥对验证的情况下有效

ansible_ssh_private_key_file 指定密钥认证ssh连接时的私钥文件

ansible_ssh_common_args 提供给ssh、sftp、scp命令的额外参数

ansible_become 允许进行权限提升

ansible_become_method 指定提升权限的方式,例如可使用sudo/su/runas等方式

ansible_become_user 提升为哪个用户的权限,默认提升为root

ansible_become_password 提升为指定用户权限时的密码

(1)主机变量

webservers

192.168.80.11 ansible_port=22 ansible_user=root ansible_password=abc1234

(2)组变量

webservers:vars\] #表示为 webservers 组内所有主机定义变量 ansible_user=root ansible_password=被管理主机密码 \[all:vars\] #表示为所有组内的所有主机定义变量 ansible_port=22 (3)组嵌套 \[nginx

192.168.73.106

192.168.73.107

192.168.73.108

apache

192.168.73.10[6:8]

webs:children\] #表示为 webs 主机组中包含了 nginx 组和 apache 组内的所有主机 nginx apache ## ansible常用的模块命令总结 **command模块:** **注意:在远程主机执行命令,不支持管道,重定向等shell的特性。** ansible 指定的IP -m command -a 'Linux基础命令' #指定 ip 执行 基础命令 ansible 组名 -m command -a 'Linux基础命令' #指定组执行基础命令 ansible all -m command -a 'linux基础命令' #指定所有被管理的主机执行该命令 ansible all -a 'linux基础命令' #如省略 -m 模块,则默认运行 command 模块 //常用的参数: chdir:在远程主机上运行命令前提前进入目录 creates:判断指定文件是否存在,如果存在,不执行后面的操作 removes:判断指定文件是否存在,如果存在,执行后面的操作 **shell模块 :** **在远程主机执行命令,相当于调用远程主机的shell进程,然后在该shell下打开一个子shell运行命令(支持管道符号等功能)** ansible-doc -s shell ansible dbservers -m shell -a '' -a 中可以使用shell常用的一些命令,注意:像awk这种本身使用 '' 来执行后续命令的命令时 单引号需要变为双引号,如果涉及变量,则"$"前需要加转义字符"\\" **cron 模块:** //在远程主机定义任务计划。其中有两种状态(state):present表示添加(可以省略),absent表示移除。 ansible-doc -s cron #按 q 退出 //常用的参数: minute/hour/day/month/weekday:分/时/日/月/周 job:任务计划要执行的命令 name:任务计划的名称 #设定计划性任务 ansible xxxx组 -m cron -a '时间规则 " job="绝对路径执行的命令" name="计划性任务的名称"' #查看xxx组中的计划性任务 ansible webservers -a 'crontab -l' #删除xxxx组的计划性任务 ansible xxx -m cron -a 'name="任务名称 crontab" state=absent' #移除计划任务,假如该计划任务没有取名字,name=None即可 **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时,是否删除用户的家目录 **group模块:** //用户组管理的模块 ansible-doc -s group #创建xxxx组中所有的xxx用户组 ansible dbservers -m group -a 'name=xxx gid=xxx system=yes' #查看用户组的信息 ansible dbservers -a 'tail /etc/group' #将用户添加到用户组 ansible dbservers -m user -a 'name=xxxx uid=xx system=yes group=xxx' #将test01用户添加到xxxzu #查看用户信息 ansible dbservers -a 'tail /etc/passwd' #查看用户的相关组的信息 ansible dbservers -a 'id 用户名' **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' **file 模块:** //设置文件属性 ansible-doc -s file ansible dbservers -m file -a 'owner=xxx group=xxx mode=xxx path=文件路径' #修改文件的属主属组权限等 ansible dbservers -m file -a 'path=链接文件路径 src=源文件路径 state=link' #设置链接文件 ansible dbservers -m file -a "path=文件路径 state=touch" #创建一个文件 ansible dbservers -m file -a "path=文件路径 state=absent" #删除一个文件 **hostname模块:** //用于管理远程主机上的主机名 ansible dbservers -m hostname -a "name=主机名" **ping 模块:** //检测远程主机的连通性 ansible all -m ping ansible xxx组 -m ping **yum 模块 :** //在远程主机上安装与卸载软件包 ansible-doc -s yum ansible webservers -m yum -a 'name=服务名' #安装服务 ansible webservers -m yum -a 'name=服务名 state=absent' #卸载服务 **service/systemd 模块 :** //用于管理远程主机上的管理服务的运行状态 ansible-doc -s service //常用的参数: name:被管理的服务名称 state=started\|stopped\|restarted:动作包含启动关闭或者重启 enabled=yes\|no:表示是否设置该服务开机自启 runlevel:如果设定了enabled开机自启去,则要定义在哪些运行目标下自启动 ansible xxx组 -a 'systemctl status 服务名' #查看web服务器httpd运行状态 ansible xxx组 -m service -a 'enabled=true name=服务名 state=started' #启动httpd服务 **script 模块:** //实现远程批量运行本地的 shell 脚本 ansible-doc -s script 首先编写一个shell脚本,赋予执行权限 ansible xxx组 -m script -a '脚本名称' **setup 模块 :** //facts 组件是用来收集被管理节点信息的,使用 setup 模块可以获取这些信息 ansible-doc -s setup ansible xxx组 -m setup #获取mysql组主机的facts信息 ansible xxx组 -m setup -a 'filter=想要的字段' #使用filter可以筛选指定的facts信息 注意:以上的所有模块,你可以用组名来指定命令实行的范围,也可以用all表示全部,也直接可以用 IP来对单机进行指定 个人建议:既然是按组划分了批量管理的主机,最好让webservers 和dbservers两个组的成员IP(最好是组成员的IP是具有连续性),最好两个组的成员的IP都像一个取值范围一样,两个组的成员IP不要有交叉,方便管理。 ## 实验 **创建nginx.conf,复制到nginx1** **输出test1,test2到nginx2,nginx3** **访问nginx1进行负载均衡查看** vim /etc/ansible/hosts ![](https://file.jishuzhan.net/article/1738277493871218690/eadb79517ef8607f9944e00159022e24.webp) vim /etc/ansible/ansible.cfg ![](https://file.jishuzhan.net/article/1738277493871218690/085f1319b5e3838792a3ff929097181c.webp) cd /opt vim nginx.conf ![](https://file.jishuzhan.net/article/1738277493871218690/1543b4756e1171dfb6729d81a330a2f9.webp) ![](https://file.jishuzhan.net/article/1738277493871218690/0bc7c64459e9e1748cb759bc38a41694.webp) ![](https://file.jishuzhan.net/article/1738277493871218690/2bc17d00eff561f6f7311b3d1bff5536.webp) ansible 192.168.233.61 -m copy -a 'src=/opt/nginx.conf dest=/usr/local/nginx/conf/nginx.conf owner=nginx group=nginx mode=777' ansible 192.168.233.61 -a 'nginx -t' ansible 192.168.233.61 -a 'systemctl restart nginx' ansible 192.168.233.62 -m shell -a 'echo test1 \> /usr/local/nginx/html/index.html' ansible 192.168.233.63 -m shell -a 'echo test2 \> /usr/local/nginx/html/index.html' ansible 192.168.233.61 -a 'curl 192.168.233.61' ![](https://file.jishuzhan.net/article/1738277493871218690/704b9420ef1496b47aff8c4450515b95.webp)

相关推荐
MAHATMA玛哈特科技8 分钟前
液压校平机:金属的“液态风筝收线器”
oracle·自动化·矫平机·液压矫平机
laoliu199638 分钟前
Odoo 18企业版源码 包含 部署教程
运维·服务器
守城小轩1 小时前
基于Chrome140的Quora账号自动化(关键词浏览)——运行脚本(三)
运维·自动化·chrome devtools·指纹浏览器·浏览器开发
未来之窗软件服务1 小时前
幽冥大陆(五十五)ASR SetThreadInformation C语言识别到自动化软件
运维·自动化·asr·东方仙盟·操作系统级别错误
开开心心就好1 小时前
免费卸载工具,可清理残留批量管理启动项
linux·运维·服务器·windows·随机森林·pdf·1024程序员节
Lbwnb丶1 小时前
检测服务器是否是虚拟化,如KVM,VM等
linux·运维·服务器
老猿讲编程2 小时前
【车载信息安全系列4】基于Linux中UIO的HSE应用实现
linux·运维·服务器
鸡吃丸子2 小时前
初识Docker
运维·前端·docker·容器
wanhengidc2 小时前
巨椰 云手机 云游戏稳定运行
运维·服务器·arm开发·游戏·云计算