网站集群批量管理-Ansible-模块管理

1. 概述

1. 自动化运维: 批量管理,批量分发,批量执行,维护

2. 无客户端,基于ssh进行管理与维护

2. 环境准备

环境主机
ansible10.0.0.7(管理节点)
nfs01 10.0.0.31(被管理节点)
backup10.0.0.41(被管理节点)

2.1 创建密钥认证

安装sshpass

yum install -y  sshpass

#!/bin/bash
##############################################################
# File Name:24-ssh.sh
# Version:V1.0
# Author:xzb996
# Organization:www.oldboyedu.com
# Desc:一键创建密钥对,分发密钥对
##############################################################
##1.vars
pass=1
ips="10.0.0.31 10.0.0.41"

##2.创建密钥对
if [ ~/.shh/id_rsa] ;then
  echo "已经有密钥对了"
else
  echo "正在创建密钥对"
  ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
fi

##3.通过循环发送密钥对
for ip in $ips
do 
   sshpass -p${pass} ssh-copy-id -i ~/.ssh/id_rsa.pub -oStrictHostKeyChecking=no $ip &>/dev/null
   echo "$ip 密钥已发送..."


done 

3. 部署与配置

 yum install -y ansible 

3.1 配置

修改配置文件关闭主机Host_key_checking .

修改配置文件开启日志功能

4. Ans-inventory主机清单 

1. ansible默认读取在/etc/ansible/hosts文件,并非/etc/hosts.

2.未来实际使用中一般我们会把主机清单文件存放在指定的目录中,运行ansible的时候通过-i选项指定主机清单文件即可

 4.1 主机清单必会格式

主机清单格式:

[分类或分组的名字]         #注意分类要体现出服务器的作用

ip地址或主机名或域名    #注意主机名要能解析才行

4.1.1 对主机分组并进行连接测试 

##vim编辑后用gg+dG清空文件内容
[nfs]
172.16.1.31
[backup]
172.16.1.41

测试

ansible命令格式:

ansible 主机ip或分组或all   -m 指定使用的模块名字 

这里的ping模块用于检查被管理端是否可以访问

 4.2 子组

创建新的分组data包含已有的分组backup和nfs

[nfs]
172.16.1.31
[backup]
172.16.1.41
##c创建data的子组
[data:children]
nfs
backup

子组使用children关键词创建

格式:

[data:children]     #组名字:children即可 

 4.3 指定用户,密码

不推荐,推荐先配置密钥认证,然后管理
没有配置密钥认证,主机清单如何书写?

[nfs]
172.16.1.31 ansible_user=root  ansible_password=1 ansible_port=22 ##指定用户名,密码,端口

5.Ansible模块

1.Ansible中通过各种模块实现批量管理.

2.一般来说这些模块对应着Linux里面的基本操作或服务管理

3. 找出Linux场景操作对应的模块即可

5.1 命令与脚本类模块 

5.1.1 command模块

是ansible默认的模块,适用于执行简单的命令,不支持特殊符号

 批量获取所有主机的主机名
ansible all -m command -a 'hostname'

 5.1.2 shell模块

批量获取所有主机的IP地址
ansible all -m shell -a "ip a s eth0 |awk -F'[ /]+' 'NR==3{print \$3}'"

shell模块不推荐执行较为复杂的指令,如果需要执行放在脚本中执行 

5.1.3 script模块

1.分发脚本(传输脚本)

2.运行脚本

 批量执行脚本获取主机信息
#!/bin/bash
##############################################################
# File Name:25-ansible-script.sh
# Version:V1.0
# Author:xzb996
# Organization:www.oldboyedu.com
# Desc:系统巡检脚本
##############################################################
hostname
hostname -I
uptime 
whoami
date +%F
ansible all -m script -a '/server/scripts/25-ansible-script.sh'

 5.2 文件相关模块

管理文件,管理目录,软连接

file模块模块说明
path路径(目录,文件) 必须要写
src源文件一般用于link(创建软连接模式) 用于指定源文件
state

状态(模式)

state=directory 创建目录

state=file (默认) 更新文件,如果文件不存在也不创建

state=link 创建软连接

state=touch 创建文件

statestate=absent 删除 (注意如果是目录递归删除目录)
modemode=755创建并修改权限
onweronwer=root
groupgroup=root

创建/tmp/xzb666.txt
ansible all -m file -a 'path=/tmp/xzb66.txt state=touch'
创建目录/app/ 
ansible all -m file -a 'path=/app/xzb/xzb666 state=directory'
创建软连接 /etc/hosts创建软连接到/tmp/下
ansible all -m file -a 'path=/tmp/hosts src=/etc/hosts state=link'

创建/ans-backup目录 所有者是oldboy
ansible all -m file -a 'path=/ans-backup owner=oldboy group=oldboy mode=700 state=directory'

删除/ans-backup/
ansible all -m file  -a 'path=/ans-backup state=absent'

 5.3 copy模块

批量分发:scp

copy模块
srcsource 源文件
destdestination 目标
backupbackup=yes 则会在覆盖前进行备份
mode修改权限
owner修改为指定所有者
group修改为指定用户组

ansible all -m copy -a 'src=/etc/hosts dest=/etc/hosts backup=yes'

5.4 服务管理模块-systemd

1.相当于是systemctl 命令:

开启/关闭/重启服务

开机自启动

systemd模块
name用于指定服务名称
enabledyes开机自启动
state

表示服务开,关,重启

state=started 开启

state=stopped 关闭

state=reloaded 重读配置文件(服务支持)

state=restarted 重启(关闭再开启)

daemon-reloadyes是否重新加载对应的服务的管理配置文件

 启动服务crond
ansible all -m systemd -a 'name=crond enabled=yes state=started' 

关闭firewalld服务
ansible all -m systemd -a 'name=firewalld enabled=no state=stopped'

5.5 软件管理模块

5.5.1 yum模块

yum模块
name指定软件包名字
state

installed 安装(present)(默认)

removed 删除 (absent)

lastest 安装或更新

update_cache可以设置为no加加速,表示不更新本地yum缓存.实际应用建议开启

安装htop,tree,lrzsz,sshpass服务
ansible all -m yum -a 'name=htop,tree,lrzsz,sshpass'

5.5.2  get_url模块

get_url下载功能
url指定要下载的地址
dest下载到哪个目录

https://tengine.taobao.org/download/tengine-2.4.1.tar.gz下载到/app/tools
ansible all -m get_url -a 'url=https://tengine.taobao.org/download/tengine-2.4.1.tar.gz dest=/app/tools'

5.5.3yum_repository模块

有点鸡肋,未来可以书写好yum配置文件,copy分发过去即可。

yum_repository模块

yum源
nameyum源中名字 []里面的内容
descriptionyum源的注释说明 对应的 是name的内容
baseurlyum源中baseurl下载地址
enabled是否启动这个源 yes/no
gpgcheck是否启动gpgcheck功能 no
file指定yum源的文件自动添加.repo 默认与模块名字一致

         

在nfs01上用yum安装nginx
##nginx官网的yum源文件书写
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
###用ansible的yum_repostiory模块安装
ansible nfs -m yum_repository -a 'name=nginx-stable description=nginx baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ enabled=yes gpgcheck=yes'

5.6 用户管理 

5.6.1 user模块

user模块
name指定用户名
uid指定uid
group指定用户组
shell指定命令解释器
create_home是否创建家目录(yes/no)
state

present 添加

absent删除

创建www-ans用户uid 2001虚拟用户
ansible all -m user -a 'name=ans-www uid=2001 shell=/sbin/nologin create_home=yes  state=present'
 批量更新密码
##下面命令可以更新密码,密码为1
ansible all -m user -a "name=xzb66 password={{'1'|password_hash('sha512','xzb666')}} state=present"

 5.7 mount模块

实现mount命令进行挂载可以修改/etc/fstab实现永久挂载

mount选项说明
fstypefilesystem type指定文件系统,xfs,ext4,iso9660,nfs
src源地址(nfs服务端地址)
path注意这里不是dest,挂载点(要把源挂载到哪里)
state参考下表

mount模块的state参数可使用的值
absent卸载并修改fstab
unmounted卸载不修改/etc/fstab
present仅修改/etc/fstab 不挂载
mounted挂载并修改/etc/fstab
remounted重新挂载

 通过ans管理在web01上挂载nfs:/data挂载到web01的/ans-upload/
##1.在web01服务器上安装nfs
ansible web01 -m yum -a 'name=nfs-utils state=present'
##2.创建挂载点
ansible web01 -m file -a 'name=/ans-upload state=directory'
##3.挂载nfs
ansible web01 -m mount -a 'src=172.16.1.31:/data/ path=/ans-upload fstype=nfs
state=mounted'
#4.检查
ansible web  -a 'df -h' 
ansible web  -a 'grep upload /etc/fstab'

5.8 cron模块

用于管理系统的定时任务,替代了crontab -e功能

cron模块说明
name定时任务名字(一定要加上), 注释的内容
minute分钟 minute="*/2"
hour小时
day日期
month月份
week周几
job

指定命令或脚本(定向到空)

job="/sbin/ntpdate ntp1.aliyun.com &>/dev/null"

state

present 添加定时任务(默认)

absent 删除

每3分钟同步时间
##清理已有的定时任务:
ansible all -a "sed -i '/ntpdate/d' /var/spool/cron/root"
##创建定时任务
ansible all  -m cron -a 'name="sync time by xzb666" minute="*/3"  job="/sbin/ntpdatentp1.aliyun.com  &>/dev/null"  state=present'
##查看定时任务
ansible all -a 'crontab -l'
##删除定时任务
ansible all -m cron -a 'name="time by xzb666" state=absent'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旦沐已成舟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值