宁波网站建设企业,php网站开发是什么,新闻热点事件2023,贵阳手机银行app下载ansible自动化运维工具 一、ansible 的概述1. ansible 的概念2. ansible 的特性 二、ansible 的部署与命令1. ansible 的部署1.1 服务器ip地址设置1.2 ansible 服务器部署 2. ansible 命令行模块2.1 command 模块2.2 shell 模块2.3 cron 模块2.4 user 模块2.5 group 模块2.6 co… ansible自动化运维工具 一、ansible 的概述1. ansible 的概念2. ansible 的特性 二、ansible 的部署与命令1. ansible 的部署1.1 服务器ip地址设置1.2 ansible 服务器部署 2. ansible 命令行模块2.1 command 模块2.2 shell 模块2.3 cron 模块2.4 user 模块2.5 group 模块2.6 copy 模块2.7 file 模块2.8 hostname 模块2.9 ping 模块2.10 yum 模块2.11 service/systemd 模块2.12 script 模块2.13 mount 模块2.14 archive 模块2.15 unarchive 模块2.16 replace 模块2.17 setup 模块 3. inventory 主机清单 总结1. ansible2. inventory 主机清单 一、ansible 的概述
1. ansible 的概念 Ansible是一个基于Python开发的配置管理和应用部署工具现在也在自动化管理领域大放异彩。它融合了众多老牌运维工具的优点Pubbet和Saltstack能实现的功能Ansible基本上都可以实现。 Ansible能批量配置、部署、管理上千台主机。比如以前需要切换到每个主机上执行的一或多个操作使用Ansible只需在固定的一台Ansible控制节点上去完成所有主机的操作。
2. 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 的部署
1.1 服务器ip地址设置
管理端192.168.145.15 ansible
被管理端192.168.145.30
被管理端192.168.145.45
被管理端192.168.145.601.2 ansible 服务器部署
#管理端安装 ansible
yum install -y epel-release #先安装 epel 源
yum install -y ansible#ansible 目录结构
/etc/ansible/
├── ansible.cfg #ansible的配置文件一般无需修改
├── hosts #ansible的主机清单用于存储需要管理的远程主机的相关信息
└── roles/ #公共角色目录#配置主机清单
cd /etc/ansible
vim hosts
[webservers] #配置组名
192.168.145.30 #组里包含的被管理的主机IP地址或主机名主机名需要先修改/etc/hosts文件
192.168.145.45[dbservers]
192.168.145.60#配置密钥对验证
ssh-keygen -t rsa
vim /etc/ssh/ssh_config
-----35行取消注释-----
StrictHostKeyChecking nosshpass -p 123 ssh-copy-id root192.168.145.30
sshpass -p 123 ssh-copy-id root192.168.145.45
sshpass -p 123 ssh-copy-id root192.168.145.602. ansible 命令行模块
命令格式ansible 组名 -m 模块 -a 参数列表ansible-doc -l #列出所有已安装的模块按q退出2.1 command 模块
#在远程主机执行命令不支持管道重定向等shell的特性。
ansible-doc -s command #-s 列出指定模块的描述信息和操作动作ansible 192.168.145.30 -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 模块ansible all -m command -a chdir/home ls ./ #查看所有主机的home目录下内容--------------------------------------------------------
#常用的参数
chdir在远程主机上运行命令前提前进入目录
creates判断指定文件是否存在如果存在不执行后面的操作
removes判断指定文件是否存在如果存在执行后面的操作
--------------------------------------------------------2.2 shell 模块
#在远程主机执行命令相当于调用远程主机的shell进程然后在该shell下打开一个子shell运行命令支持管道符号等功能
ansible-doc -s shellansible dbservers -m shell -a ps aux | wc -l #统计dbservers组中运行程序的数量
ansible dbservers -m shell -a echo helloworld /opt/123.txt #输出helloworld至dbservers组的/opt/123.txt
ansible dbservers -m shell -a echo $(ifconfig ens32 | awk NR2 {print \$2})#输出dbservers组的ip地址
ansible dbservers -m shell -a chdir/home ls ./ #输出dbservers组的用户2.3 cron 模块
#在远程主机定义任务计划。其中有两种状态statepresent表示添加可以省略absent表示移除。
ansible-doc -s cron #按 q 退出--------------------------------------------------------
#常用的参数
minute/hour/day/month/weekday分/时/日/月/周
job任务计划要执行的命令
name任务计划的名称
user指定计划任务属于哪个用户默认是root用户
--------------------------------------------------------ansible webservers -m cron -a minute*/1 job/bin/echo helloworld nametest crontab #给webservers组远程添加计划性任务名字是test crontab,每分钟输出一次helloworld
ansible webservers -a crontab -l #远程查看websesrvers组的计划性任务
ansible webservers -m cron -a nametest crontab stateabsent2.4 user 模块
#用户管理的模块
ansible-doc -s user--------------------------------------------------------
#常用的参数
name用户名必选参数
statepresent|absent创建账号或者删除账号present表示创建absent表示删除
systemyes|no是否为系统账号
uid用户uid
group用户基本组
groups: 用户所属附加组
shell默认使用的shell
create_homeyse|no: 是否创建家目录
password用户的密码建议使用加密后的字符串
removeyes|no当stateabsent时是否删除用户的家目录
--------------------------------------------------------ansible dbservers -m user -a nametest01 #创建用户test01
ansible dbservers -m command -a tail /etc/passwd
ansible dbservers -m user -a nametest01 stateabsent #删除用户test012.5 group 模块
#用户组管理的模块
ansible-doc -s groupansible dbservers -m group -a namemysql gid306 systemyes #创建mysql组
ansible dbservers -a tail /etc/group
ansible dbservers -m user -a nametest01 uid306 systemyes groupmysql #将test01用户添加到mysql组中
ansible dbservers -a tail /etc/passwd
ansible dbservers -a id test01 2.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 ownerroot mode640
ansible dbservers -a ls -l /opt
ansible dbservers -a cat /opt/fstab.bakansible dbservers -m copy -a contenthelloworld dest/opt/hello.txt #将helloworld写入/opt/hello.txt文件中
ansible dbservers -a cat /opt/hello.txt 2.7 file 模块
#设置文件属性
ansible-doc -s fileansible dbservers -m file -a ownertest01 groupmysql mode644 path/opt/fstab.bak #修改文件的属主属组权限等
ansible dbservers -m file -a path/opt/fstab.link src/opt/fstab.bak statelink #设置/opt/fstab.link为/opt/fstab.bak的链接文件
ansible dbservers -m file -a path/opt/abc.txt statetouch #创建一个文件
ansible dbservers -m file -a path/opt/abc.txt stateabsent #删除一个文件2.8 hostname 模块
#用于管理远程主机上的主机名
ansible dbservers -m hostname -a namemysql012.9 ping 模块
#检测远程主机的连通性
ansible all -m ping2.10 yum 模块
#在远程主机上安装与卸载软件包
ansible-doc -s yumansible webservers -m yum -a namehttpd #安装服务
ansible webservers -m yum -a namehttpd stateabsent #卸载服务2.11 service/systemd 模块
#用于管理远程主机上的管理服务的运行状态
ansible-doc -s service--------------------------------------------------------
#常用的参数
name被管理的服务名称
statestarted|stopped|restarted动作包含启动关闭或者重启
enabledyes|no表示是否设置该服务开机自启
runlevel如果设定了enabled开机自启去则要定义在哪些运行目标下自启动
--------------------------------------------------------ansible webservers -a systemctl status httpd #查看web服务器httpd运行状态
ansible webservers -m service -a enabledtrue namehttpd statestarted #启动httpd服务2.12 script 模块
#实现远程批量运行本地的 shell 脚本
ansible-doc -s scriptvim test.sh
#!/bin/bash
echo hello ansible from script /opt/script.txtchmod x test.sh
ansible webservers -m script -a test.sh
ansible webservers -a cat /opt/script.txt2.13 mount 模块
#挂载文件系统
ansible-doc -s mount--------------------------------------------------------
#常用的参数
src定义挂载设备的路径
path定义挂载到哪个目录必须指定
fstype指定挂载文件的系统类型必须指定xfs、iso9660、nfs...
opts定义挂载的参数defaults、rw、ro...
state定义挂载的状态mounted进行挂载修改/etc/fstab信息、absent永久性卸载并修改 /etc/fstab信息、unmounted临时卸载不修改/etc/fstab信息
--------------------------------------------------------ansible dbservers -m mount -a src/dev/sr0 path/mnt statemounted fstypeiso9660#挂载/dev/sr0磁盘到mnt目录并且在/etc/fstab进行挂载ansible dbservers -m mount -a path/mnt stateabsent #解挂载2.14 archive 模块
#打包压缩
ansible-doc -s archive--------------------------------------------------------
#常用的参数
path: 必须参数远程主机上需要被打包压缩的源文件/目录
dest: 打包压缩后的包文件路径(包文件的父目录必须存在);如果包文件已存在则会被覆盖
format: 指定压缩类型包括: bz2、gz默认、tar、xz、zip
removeyes|no: 是否删除源文件
--------------------------------------------------------ansible dbservers -m archive -a path/etc/yum.repos.d/ dest/opt/repo.zip formatzip
ansible dbservers -m archive -a path/opt/abc.txt,/opt/123.txt dest/opt/abc123.tar.gz formatgz removeyes2.15 unarchive 模块
#解包解压缩
ansible-doc -s unarchive--------------------------------------------------------
#常用的参数
copy默认为 copyyes 拷贝的文件从 ansible 主机复制到远程主机copyno 表示在远程主机上寻找源文件解压
srctar包源路径可以是 ansible 主机上的路径也可以是远程主机上的路径如果是远程主机上的路径则需设置 copyno
dest解压后文件的目标绝对路径
remote_src: 和 copy 功能一样且互斥设置 remote_srcyes 表示文件在远程主机上设置为 remote_srcno 表示文件在 ansible 主机上
--------------------------------------------------------#将 ansible 主机的压缩文件拷贝到到远程主机并解压修改文件所属组和用户
ansible dbservers -m unarchive -a src/opt/abc.tar.gz dest/root copyyes
或者
ansible dbservers -m unarchive -a src/opt/abc.tar.gz dest/root remote_srcno#在远程主机解包
ansible dbservers -m unarchive -a src/opt/123.tar.gz dest/root copyno
或者
ansible dbservers -m unarchive -a src/opt/123.tar.gz dest/root remote_srcyes2.16 replace 模块
#类似于sed命令主要也是基于正则进行匹配和替换
ansible-doc -s replace--------------------------------------------------------
#常用的参数:
path必须参数指定要修改的文件
regexp必须参数指定一个正则表达式
replace替换regexp参数匹配到的字符串
backupyes|no: 修改源文件前创建一个包含时间戳信息的备份文件
before如果指定则仅替换/删除此匹配之前的内容可以和after参数结合使用
after如果指定则仅替换/删除此匹配之后的内容可以和before参数结合使用
owner修改文件用户名
group修改文件组名
mode修改文件权限
--------------------------------------------------------vim /opt/test.txt
11 22 33 44 55 66
aa bb cc dd ee ff
1a 2b 3c 4d 5e 6f#匹配 33 并修改为 cc
ansible dbservers -m replace -a path/opt/test.txt regexp33 replacecc
#匹配到任意一个或多个开头的行增加注释
ansible dbservers -m replace -a path/opt/test.txt regexp^(.*) replace#\1
#取消注释
ansible dbservers -m replace -a path/opt/test.txt regexp^#(.*) replace\1
#匹配以 a 开头的后面有一个或者多个字符的行并在前面添加 # 注释
ansible dbservers -m replace -a path/opt/test.txt regexp^(a.*) replace#\1ansible dbservers -m replace -a path/opt/test.txt regexp3 replacethree beforecc2.17 setup 模块
#facts 组件是用来收集被管理节点信息的使用 setup 模块可以获取这些信息
ansible-doc -s setupansible dbbservers -m setup #获取mysql组主机的facts信息
ansible dbservers -m setup -a filter*ipv4 #使用filter可以筛选指定的facts信息3. inventory 主机清单 Inventory支持对主机进行分组每个组内可以定义多个主机每个主机都可以定义在任何一个或多个主机组内。
#如果是名称类似的主机可以使用列表的方式标识各个主机。
vim /etc/ansible/hosts
[webservers]
192.168.145.30:2222 #冒号后定义远程连接端口默认是 ssh 的 22 端口
192.168.145.[1:254] #定义192.168.145.0整个网段[dbservers]
db-[a:f].example.org #支持匹配 a~f变量名含义ansible_hostansible连接节点时的IP地址ansible_port连接对方的端口号ssh连接时默认为22ansible_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提升为哪个用户的权限默认提升为rootansible_become_password提升为指定用户权限时的密码
#主机变量
[webservers]
192.168.145.30 ansible_port22 ansible_userroot ansible_password123#组变量
[webservers:vars] #表示为 webservers 组内所有主机定义变量
ansible_userroot
ansible_password123[all:vars] #表示为所有组内的所有主机定义变量
ansible_port22#组嵌套
[nginx]
192.168.145.15
192.168.145.30
192.168.145.45[apache]
192.168.145.60
192.168.145.75[webs:children] #表示为 webs 主机组中包含了 nginx 组和 apache 组内的所有主机
nginx
apache总结
1. ansible
ansible 自动话运维工具机器管理工具 可用实现批量管理多台成百上千主机应用级别的跨主机编排工具特性
无agent的存在不需要在被控制的节点上安装客户端应用
通过ssh协议与被控制节点通信
基于模块工作的可用 通过模块实现在控制节点上执行命令操作模块作用常用选项command在远程主机执行linux命令但不支持管道、重定向符号ansible的默认模块chdirshell在远程主机执行linux命令支持管道、重定向符号chdircron在远程主机设置crontab计划任务minute hour day month weekday job name statepresent/absentuser在远程主机管理用户账户name uid group groups shell remove statepresent/absentgroup在远程主机管理组账户name gidcopy将本地文件/目录/内容复制到远程主机src dest contentfile在远程主机管理文件属性path stateabsent/touch/dirctory/link owner group mode srcping检测与远程主机的连通性-hostname在远程主机设置主机名-mount在远程主机挂载目录/设备文件src path fstype statemounted/unmounted/absent optsyum在远程主机通过yum安装软件包name stateabsentservice/systemd在远程主机管理应用服务的运行状态name statestarted/stopped/restarted/reloaded enabledyes/no/true/falsescript在远程主机执行脚本-archive在远程主机压缩文件path dest format removeunarchive将本地或远程主机的压缩包在远程主机解压缩copy src destreplace在远程主机修改文件内容path regexp replace backupyes/no before aftersetup获取远程主机的facts信息filter
2. inventory 主机清单
主机清单配置文件 /etc/ansible/hosts[组名]
主机IP ansible port ansible_user ansible_password #主机变量
主机名[1:10][a:z] #设置连续的主机范围[组名:vars] #设置组变量
ansible_port
ansible_user
ansible_password[大组名:children] #设置组嵌套
组名1
组名2
.....