当前位置: 首页 > news >正文

自动化运维之ansible

一、认识自动化运维

假如管理很多台服务器,主要关注以下几个方面“

1.管理·机与被管理机的连接(管理机如何将管理指令发送给被管理机)

2.服务器信息收集(如果被管理的服务器有centos7.5外还有其它linux发行版,如suse,ubuntu等。当你要做的事情在不同os上有所不同,你需要收集信息,并将其分开处理)

3.服务器分组(因为有些时候我要做的事情不是针对所有服务器,可能只针对某一个分组)

4.管理内容的主要分类

文件目录管理(包括文件的创建,删除,修改,查看状态,远程拷贝等)

用户和组管理

cron时间任务管理

yum源配置与通过yum管理软件包

服务管理

远程执行脚本

远程执行命令

二、ansilble

2.1 ansible环境搭建

1.静态ip

2.主机名及主机名互相绑定

3.关闭防火墙,selinux

4.时间同步

5.确认和配置yum源(需要epel源)

实验过程:

第一步:管理机上安装ansible,被管理节点必须打开ssh服务

yum install epel-release
yum install ansible
ansible --version

第二步:实现master对agent的免密登录。只在master上做。(如果这一步不做,则在后面操作agent时要加-k参数传密码;或者在主机清单里传密码

ssh-keygen
ssh-copy-id -i 192.168.124.131
ssh-copy-id -i 192.168.124.132

第三步:在master上定义主机组,并测试连接性

【group】

192.168.124.131

192.168.124.132

2.2服务器分组

ansible通过一个主机清单功能来实现服务器分组

ansible的默认主机清单配置文件为/etc/ansible/hosts

示例:

[nginx]               组名 
apache[1:10].aaa.com  表示apache1.aaa.com到apache10.aaa.com这10台机器
nginx[a:z].aaa.com    表示nginxa.aaa.com到nginxz.aaa.com共26台机器
10.1.1.[11:15]        表示10.1.1.11到10.1.1.15这5台机器10.1.1.1:2222         设置端口为2222定义10.1.1.12:2222这台服务器的别名为nginx1
nginx1 ansible_ssh_host=10.1.1.12 ansible_ssh_port=2222没有做免密登录的服务器可以指定用户名和密码
nginx1 absible_ssh_host=10.1.1.12 ansible_ssh_port=2222 ansible_ssh_user=root ansible_-ssh_pass="123456"利用别名来分组
nginx1 ansible_ssh_host=10.1.1.12 ansible_ssh_port=2222 ansible_ssh_user=root ansible_ssh_pass="123456"
nginx2 ansible_ssh_host=10.1.1.13[nginx]
nginx1
nginx2

2.3ansible模块

ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是absible所运行的模块,ansible只是提供一种框架。

ansible支持的模块非常的多,我们并不需要把每个模块都记住,而是只需要熟悉一些常见的模块,其他的模块在需要用到时查询即可

2.3.1查看所有支持的模块

ansible-doc -l

2.3.2hostname模块

hostname模块用于修改主机名(注意:他不能修改/etc/hosts文件)

将其中一台远程主机名修改为agent1.cluster.com

master# ansible 192.168.124.131 -m hostname -a 'name=agent1.cluster.com'
基本格式为:ansible 操作的机器名或组名 -m 模块名 -a "参数1=值1 参数2=值2"

2.3.3file模块(重点)

创建目录

删除:

2.3.4copy模块(重点)

copy模块用于对文件的远程拷贝操作(如把本地的文件拷贝到远程的机器上)

在master上准备一个文件,拷贝此文件到group1的所有机器上

copy模块拷贝时要注意拷贝目录后面是否带"/"符号

/etc/yum.repos.d后面不带/符号,则表示把/etc/yum.repos.d整个目录拷贝到/tmp/目录下
master# ansible group1 -m copy -a 'src=/etc/yum.repos.d dest=/tmp/'
/etc/yum.repos.d/后面带/符号,则表示把/etc/yum.repos.d/目录里面的所有文件拷贝到/tmp/目录下
master# ansible group1 -m copy -a 'src=/etc/yum.repos.d/ dest=/tmp/'

2.3.5stat模块(了解)

stat模块类似于linux的stat命令,用于获取文件的状态信息

获取/etc/fstab文件的状态信息

2.3.6template模块(扩展)

与copy模块功能几乎一样

template模块首先使用变量渲染jinjia2模版文件成普通文件,然后再复制过去,而copy模块不支持。(jinja2是一个基于python的模版引擎)

master# ansible -m template group1 -a "src=/etc/hosts dest=/tmp/hosts"

template模块不能拷贝目录

master# ansible -m template group1 -a "src=/etc/yum.repos.d/ dest=/etc/yum.repos.d/"

2.3.7fetch模块

fetch模块与copy模块类似,但作用相反。用于把远程机器的文件拷贝到本地。

第一步:在两台被管理机上分别创建一个同名文件(但内容不同)

agent1# echo 12345 > /tmp/1.txt
agent2# echo 54321 > /tmp/1.txt

第二步:从master上fetch文件(因为group1里有2台机器,为了避免同名文件冲突,它使用了不同的目录

master# ansible group1 -m fetch -a 'src=/tmp/1.txt dest=/tmp/'

第三步:先删除上面fetch过来的,然后尝试只fetch其中一台机器的,也会使用名称来做子目录区分

master# rm /tmp/10.1.1.* -rfmaster# ansible 10.1.1.12 -m fetch -a 'src=/tmp/1.txt dest=/tmp/'

注意:fetch模块不能从远程拷贝目录到本地

2.3.8user模块

user模块用于管理用户账号和用户属性

ansible group1 -m user -a "name=zhangsan uid=1004 home=/home/hhh shell=/bin/false command=@_@"

删除用户以及家目录

ansible group1 -m user -a "name=zhangsan state=absent remove=yes"

2.3.9group模块

创建组

ansible group1 -m group -a "name=admins gid=5000"

删除组

ansible group1 -m group -a "name=admins state=absent"

2.3.10cron模块

cron模块用于管理周期性时间任务

创建一个cron任务,不指定suer的话,默认就是root(因为我这里是用root操作的)

如果minute,hour,day,month,week不指定的话,默认都为*

master# ansible group1 -m -cron -a 'name="test cron1" user=zhangsan job="touch /tmp/111" minute=*/2'

删除cron任务

master #ansible group1 -m -cron -a 'name="test cron1" state=absent'

2.3.11yum模块(重点)

yum模块用于使用yum命令来实现软件包的安装与卸载

使用yum安装一个软件(前提:group1的机器上的yum配置都已经ok)

master#ansible group1 -m yum -a 'name=vsfpd state=present'

使用yum安装httpd,http-devel软件,state=latest表示安装最新版本

master#ansible group1 -m yum -a 'name=httpd,httpd-devel state=latest'

使用yum卸载httpd,httpd-devel软件

master#ansible group1 -m yum -a 'name=httpd,httpd-devel state=absent'

2.3.12service模块

service模块用于控制服务的启动,关闭,开机自启动等

启动vsftpd服务,并设为开孔自动启动

master# ansible group1 -m service -a 'name=vsftpd state=started enabled=on'

2.3.13script-command-shell模块

script模块用于在远程机器上执行本地脚本

master# ansible group1 -m script -a '/tmp/xxx.sh'

command和shell模块都是用于执行linux命令的,这对于熟悉的工程师来说,用起来非常high

shell模块与command模块差不多(command模块不能执行一些类似于¥HOME,>,<,|等符号,但Shell可以)

master# ansible -m command group1 -a "useradd user2"
master# ansible -m command group1 -a "id user2"master# ansible -m command group1 -a "cat /etc/passwd |wc -l"  --报错
master# ansible -m shell group1 -a "cat /etc/passwd |wc -l"  --成功


文章转载自:

http://mJ4TdNvL.mxgpp.cn
http://ZA4RnQKq.mxgpp.cn
http://ynSIl1tH.mxgpp.cn
http://M6yJUrgW.mxgpp.cn
http://iRQ1Haih.mxgpp.cn
http://Fzr8sqoG.mxgpp.cn
http://Bn0lkjRw.mxgpp.cn
http://aa0IJQN2.mxgpp.cn
http://o0VD4M5g.mxgpp.cn
http://R76d6AHu.mxgpp.cn
http://ma2symRN.mxgpp.cn
http://mx3SOJgo.mxgpp.cn
http://ZWzWPyJp.mxgpp.cn
http://3dyBNN9d.mxgpp.cn
http://mJzU5sPy.mxgpp.cn
http://h7CuqqNL.mxgpp.cn
http://s79QEtuK.mxgpp.cn
http://AKpBSPU5.mxgpp.cn
http://hRwN47uy.mxgpp.cn
http://DdAoPoCd.mxgpp.cn
http://ApdUqFDC.mxgpp.cn
http://EEfvNUc2.mxgpp.cn
http://6fpouOM8.mxgpp.cn
http://cmWrpJwq.mxgpp.cn
http://zveN978z.mxgpp.cn
http://imYA7VXx.mxgpp.cn
http://jKiFGh8e.mxgpp.cn
http://jby2yYGe.mxgpp.cn
http://EIp3g5hB.mxgpp.cn
http://XTkrAjYK.mxgpp.cn
http://www.dtcms.com/a/370481.html

相关文章:

  • 2025年上海市星光计划第十一届职业院校技能大赛高职组“信息安全管理与评估”赛项交换部分前6题详解(仅供参考)
  • Orin-Apollo园区版本:订阅多个摄像头画面拼接与硬编码RTMP推流
  • 多线程(六) ~ 定时器与锁
  • OpenSSL 1.0.1e 下载解压和运行方法(小白适用 附安装包)​
  • Qt图表功能学习
  • 【营销策略算法】关联规则学习-购物篮分析
  • 部署AIRI
  • 深度学习基础概念回顾(Pytorch架构)
  • 基于LSTM深度学习的网络流量测量算法matlab仿真
  • 【PyTorch实战:Tensor变形】5、 PyTorch Tensor指南:从基础操作到Autograd与GPU加速实战
  • 【基础-判断】@Entry装饰的自定义组件将作为页面的入口。在单个页面中可以使用多个@Entry装饰不同自定义组件。
  • 驱动开发系列71 - GLSL编译器实现 - 指令选择
  • 贪心算法应用:化工反应器调度问题详解
  • OpenAvatarChat项目在Windows本地运行指南
  • canal+DataX实现数据全量/实时同步
  • Jenkins运维之路(自动获得分支tag自动构建)
  • 服务器内存和普通计算机内存在技术方面有什么区别?
  • 同一台nginx中配置多个前端项目的三种方式
  • 【LeetCode热题100道笔记】排序链表
  • Shell 脚本实现系统监控与告警
  • 【算法--链表】86.分割链表--通俗讲解
  • 基于区块链的IoMT跨医院认证系统:Python实践分析
  • 用内存顺序实现 三种内存顺序模型
  • rh134第五章复习总结
  • Java包装类型
  • Linux awk 命令使用说明
  • 一个正常的 CSDN 博客账号,需要做哪些基础准备?
  • 文件I/O与I/O多路复用
  • protobuf的序列反序列化
  • Linux/UNIX系统编程手册笔记:共享库、进程间通信、管道和FIFO、内存映射以及虚拟内存操作