17,Ansible自动化管理工具服务
admin
2023-03-31 19:21:30
0

1,什么是Ansible
Ansible 是 python 中的一套模块,系统中的一套自动化工具,只需要使用 ssh 协议连接及可用来系统管理、自动化执行命令等任务。

2,为什么要用Ansible

  1. 可以实现批量系统操作配置
  2. 可以实现批量软件服务部署
  3. 可以实现批量文件数据分发
  4. 可以实现批量系统信息收集

3,使用Ansible的意义

  1. 提高工作的效率(部署综合架构)
  2. 提高工作准确度
  3. 减少维护的成本
  4. 减少重复性工作

4,安装部署

  1. 没有配置文件(不需要配置)
  2. 不需要启动服务
  3. 客户端没有需要部署任务

5,安装Ansible
yum -y install ansible
17,Ansible自动化管理工具服务

6,学习ansible主机清单
vi /etc/ansible/hosts
17,Ansible自动化管理工具服务
在上一单元SSH优化部分我们已经改变了端口,采用密钥登录,所以这里这样写反之,正常写
7,学习Ansible模块调用
首先就是要先打通ssh远程功能,ansible是建立在ssh通讯上实现的批量管理
1)ping模块
ansible 主机组名 -m 模块名
ansible nfs -m ping 调用ping模块

2)约等于万能模块shell (忘记这个命令吧,ansible支持2843个模块,每个命令都有自己的专门模块,不要都用shell来代替)
ansible nfs -m shell -a "各种命令"

8,yaml语法,这类语法调用模块更加细致,有更多的参数,有个一很大的有点就是不会重复执行,比如你在管理端对100台客户机执行脚本
中间报错了,可以直接再次执行而不报错 (ansible剧本)
特点:1)以缩进代表不同层级之间的关系
2)对索引有严格要求
1)file模块(每个模块的具体参数都在官方文档里有要自己会读https://docs.ansible.com/ansible/latest/modules)
ansible nfs -m file -a "path=/tmp/oldboy state=directory mode=644 owner=oldboy group=oldboy"

2)copy模块
ansible nfs -m copy -a "src=/etc/ssh/sshd_config dest=/tmp/ mode='644'"

3)script脚本模块
ansible nfs -m script -a "/server/scripts/xx.sh"

4)user用户模块(创建www用户和组uid和gid等于666,描述为oldboy,没有家目录,/sbin/nologin)
ansible nfs -m group -a "name=www gid=666"
ansible nfs -m user -a "name=www uid=666 comment=oldboy shell=/sbin/nologin create home=no"

5)cron模块
ansible nfs -m cron -a 'name="ntpdate" minute="*/5" job="ntpdate time1.aliyun.com"'

6)yum模块
ansible nfs -m yum -a "name=ntpdate state=lastest"

例1:搭建NFS服务
NFS服务端:

#!/bin/bash                                             

ansible nfs -m group -a "name=www gid=666 state=present"                        
ansible nfs -m user -a "name=www uid=666 group=666 shell=/sbin/nologin create_home=no"
ansible nfs -m yum -a "name=nfs-utils"                              
ansible nfs -m file -a "path=/data state=directory"                                         
ansible nfs -m copy -a "src=exports dest=/etc/exports backup=yes"   
ansible nfs -m service -a "name=rpcbind state=started enabled=yes"                          
ansible nfs -m service -a "name=nfs state=started enabled=yes"              
ansible nfs -m shell -a "showmount -e"                              

NFS客户端:

#!/bin/bash                                             

ansible nfs_client -m group -a "name=www gid=666 state=present"                                             
ansible nfs_client -m user -a "name=www uid=666 group=666 shell=/sbin/nologin create_home=no"   
ansible nfs_client -m yum -a "name=nfs-utils"       
ansible nfs_client -m file -a "path=/data state=directory"  
ansible nfs_client -m service -a "name=rpcbind state=started enabled=yes"   
ansible nfs_client -m shell -a "showmount -e 172.16.1.31"       
ansible nfs_client -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted"    

相关内容

热门资讯

特朗普下令不打了,美国的“余粮... 可以说戛然而止,快得让很多人都无法适应。前两周,每一天美国都要打击伊朗一通。就在7月24日白宫会议前...
穆杰塔巴最新表态:支持黎真主党 △伊朗最高领袖穆杰塔巴·哈梅内伊(资料图)当地时间7月26日晚,伊朗最高领袖穆杰塔巴·哈梅内伊对黎巴...
美军瘫痪一艘驶向伊朗的油轮,特... 美国总统特朗普虽下令暂停打击伊朗,但美军仍在执行对霍尔木兹海峡的封锁任务。针对美军瘫痪一艘驶向伊朗油...
“小牌大耍”的底气到底是哪里来... 澎湃首席评论员 李平这两天,“艺人李权哲乘高铁占座事件”在网上引发热议,该艺人参加的活动主办方曾发布...
越南籍船舶南海遇险沉没,中方已... 据凤凰卫视报道,一艘越南船舶7月24日夜间在越南庆和以东220海里附近遇险沉没,船上共62名越南籍人...
李权哲占座、蒙面、装死:娱乐圈... 你买了一张高铁一等座的票,找到自己的位置,18A。有人坐着。你拿出手机看了眼购票记录——没错,18A...
杭州景区咖啡店闯入1条幼蛇,仅... 夏天气温高、湿度大,蛇类活动进入高峰期,千万别被“小蛇无害”的刻板印象骗了!有些幼蛇看着迷你可爱,实...
AI开源公开信签署企业增至50... IT之家 7 月 26 日消息,英伟达、微软、IBM、Meta 等 25 家美国科技企业本周(7 月...
南阳快手短视频代运营企业的服务... 在短视频平台快速发展的背景下,企业通过快手等渠道进行内容运营已成为常见的市场动作。对于希望借助短视频...
普通人该如何跟上科技发展的步伐 这个被称作科技的词汇, 听起来既有宏大之感, 又颇为遥远, 好似仅仅归属于在实验室里专心致志钻研的科...