一、Ansible简介
自动化运维
IT运维是目前IT服务中的重要组成部分
运维方式:
-
传统运维
- 手动操作:很多任务需要手动完成,包括系统部署、配置和故障排查等
- 资源密集型:依赖大量人力投入,容易受到人为因素的影响
- 反应式:更多地是根据问题出现后再进行处理和修复,缺乏主动性和预防性
- 时间成本高:由于手动操作和反应式处理,时间成本相对较高,而且容易出现人为失误
-
自动化运维
- 自动化工具:广泛使用自动化工具来完成系统管理、部署、监控和故障处理等任务
- 自动化流程:通过编写脚本或使用自动化平台来实现工作流程的自动化,减少人工干预
- 预防性维护:更注重预防性维护,通过自动化监控和报警系统,能够在问题出现前进行预警和处理
- 时间成本低:自动化运维能够大大降低时间成本,提高效率,减少人为错误
总的来说,掌握自动化运维有助于提高工作效率,简化运维工作
自动化运维工具
-
Puppet:
- Puppet 是一个基于客户端/服务器模型的自动化配置管理工具
- 使用Ruby语言开发
- 基于SSL,远程执行命令能力较弱能
-
Chef:
- Chef 使用 Ruby 语言编写配置文件
- 能够实现基础设施的自动化部署、配置和管理
-
SaltStack:
- SaltStack 是一个快速、灵活的系统管理和自动化软件
- 使用 Python 编开发
- 支持远程执行命令、配置管理等功能
-
Ansible
- 目前应用最为广泛的自动化运维工具
- 2012年首次发布,2015年被RedHat收购
- 基于 Python的paramiko 开发,分布式
- 基于SSH进行远程管理,被控节点无需额外安装任何客户端软件,轻量级
- 配置语法使用 YMAL 及 Jinja2 模板语言
- 使用模块管理被控节点,具备更强的远程命令执行操作能力
Ansible使用环境
-
控制节点
- 安装Ansible软件
- Python环境支持:Python>=2.6
- 必要的模块:如PyYAML等
-
被控节点
- 启用SSH服务
- 允许控制节点登录,通常设置免密登录
- Python环境支持
二、实验环境准备
环境设计
准备4台主机,配置好主机名、IP地址、YUM源。关闭防火墙和SELinux!!!
| 主机名 | IP地址 | 角色 |
|---|---|---|
| pubserver | 192.168.88.240 | 控制节点 |
| web1 | 192.168.88.11 | 被控节点(webserver) |
| web2 | 192.168.88.12 | 被控节点(webserver) |
| db1 | 192.168.88.13 | 被控节点(database) |
各主机需搭建网络yum源仓库,自行准备ansible源文件
# 上传Ansible相关软件到pubserver主机
[root@pubserver ~]# mkdir /var/ftp/rpms #创建rpm包存放目录
[root@server1 ~]# scp /linux-soft/s2/zzg/ansible_soft/* root@192.168.88.240:/var/ftp/rpms/ #上传Ansible相关软件
[root@pubserver ~]# yum -y install createrepo_c #安装createrepo命令包
[root@pubserver ~]# createrepo /var/ftp/rpms/ #创建软件仓库信息
[root@pubserver ~]# ls /var/ftp/rpms/
ansible-6.3.0-1.el8.noarch.rpm
ansible-core-2.13.3-1.el8.x86_64.rpm
cowsay-3.04-16.el8.noarch.rpm
repodata #确保有该目录
# 配置自定义yum源
[root@pubserver ~]# vim /etc/yum.repos.d/local.repo #增加自定义yum源配置,在文件最后追加
...
[ansible-rpms]
name=ansible-rpms
baseurl="ftp://192.168.88.240/rpms"
enabled=1
gpgcheck=0
[root@pubserver ~]# yum clean all; yum repoinfo
...
Total packages: 8,268
# 同步repo文件到所有被控节点
[root@pubserver ~]# for i in 192.168.88.1{1..3}
do
scp /etc/yum.repos.d/local.repo root@$i:/etc/yum.repos.d/
done
控制节点安装Ansible
## 被控节点pubserver主机安装ansible软件
[root@pubserver ~]# yum -y install ansible #安装Ansible软件
[root@pubserver ~]# ansible --version #确认ansible命令可用
ansible [core 2.13.3]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.9/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.9.7 (default, May 10 2022, 23:45:56) [GCC 8.5.0 20210514 (Red Hat 8.5.0-10)]
jinja version = 3.1.2
libyaml = True
[root@pubserver ~]#
被控节点基础配置
-
配置主机名解析
## 配置hosts文件,确保控制节点通过主机名访问到被控节点
[root@pubserver ~]# vim /etc/hosts #编辑hosts文件配置主机名解析
192.168.88.240 pubserver
192.168.88.11 web1
192.168.88.12 web2
192.168.88.13 db1
#测试hosts文件配置
[root@pubserver ~]# for i in pubserver web1 web2 db1
do
ping -c 2 $i
done
配置免密登录被控节点
## 配置控制节点免密登录被控节点
[root@pubserver ~]# ssh-keygen -t rsa -f /root/.ssh/id_rsa -N '' #非交互生成秘钥对
#发送公钥到被控节点
[root@pubserver ~]# for i in web1 web2 db1
do
ssh-copy-id root@$i
done
#确认免密登录,结果显示web1,web2,db1
[root@pubserver ~]# for i in web1 web2 db1
do
ssh root@$i "hostname"
done
被控节点Ansible管理环境配置
配置目标
使用同一套Ansible软件管理多个环境,如开发环境、测试环境、生产环境
多用户使用同一个控制节点管理不同主机
每个用户有属于自己的配置环境(工作目录)
Ansible配置文件查找顺序
首先检测ANSIBLE_CONFIG变量定义的配置文件
其次检查当前目录下的./ansible.cfg文件
再次检查当前用户家目录下的~/ansible.cfg文件
最后检查/etc/ansible/ansible.cfg文件
控制节点配置
## 根据上述结果,创建自定义Ansible工作目录
# 查看默认配置文件
[root@pubserver ~]# cat /etc/ansible/ansible.cfg #查看默认配置,通常不用该文件
[root@pubserver ~]# ansible-config init #根据提示查看配置条目
# 自定义Ansible工作目录
[root@pubserver ~]# mkdir ansible #自定义工作目录,名称自定义
[root@pubserver ~]# cd ansible/ #进入工作目录,以后关于ansible操作均在此目录下进行
# 配置Ansible工具
[root@pubserver ansible]# vim ansible.cfg #编辑配置文件
[defaults] #通用配置
inventory = inventory #主机清单列表文件
host_key_checking = false #不检查主机秘钥,=两边有无空格均可
[root@pubserver ansible]# vim inventory #编辑主机清单列表文件
[webservers] #定义主机组,名称自定义
web[1:2] #[1:2]表示从1到2
[dbs]
db1
[cluster:children] #cluster为组名,:children为固定写法,表示为cluster的子组
webservers
dbs
## 确认配置结果
# 命令操作一定在工作目录下执行
[root@pubserver ansible]# ansible all --list-hosts #查看所有被控主机列表
hosts (3):
web1
web2
db1
[root@pubserver ansible]# ansible webservers --list-hosts #查看webservers组主机列表
hosts (2):
web1
web2
四、Ansible使用方法
使用方法样例
ad-hoc临时命令
## 语法
# ansible [主机或组列表] -m 模块 -a "参数"
# 常用额外选项:
# -i:指定主机清单列表文件
# -k:使用密码登录远程主机,通常用于某个特殊被控节点未做免密登录的场景下
## 样例:ping模块用于测试是否可以SSH远程登录被控节点主机
[root@pubserver ansible]# ansible all -m ping
web1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
...
# 返回结果为绿色的SUCCESS状态信息则为成功
# 返回结果为红色的UNREACHABLE则为失败,需要检测管理节点与被控节点网络统计和是否可以免密登录
五、Ansible常用模块
模块是什么
Ansible在使用过程中通过模块来完成指定任务
Ansible模块的本质是一个文件,通常是为实现具体功能的Python脚本
Ansible已经有很多开发好的模块,可以直接调用,具备开发能力也可以自行开发
多数模块都支持使用参数,需要使用的时候指定参数
主要学习已有的常用模块的用途及常用参数
一定要掌握ansible-doc命令*
## ansible-doc命令使用方法
# 列出目前全部可用Ansible模块,按空格键向下翻页,按q退出返回命令行
[root@pubserver ansible]# ansible-doc -l
# 统计已有模块数量,WARNING信息直接忽略即可
[root@pubserver ansible]# ansible-doc -l | wc -l
7214
# 查看包含yum的模块
[root@pubserver ansible]# ansible-doc -l | grep yum
# 查看指定模块的帮助文档,按空格键向下翻页,按q退出返回命令行
[root@pubserver ansible]# ansible-doc yum
Linux系统命令相关模块
-
command模块
-
Ansible使用的默认模块,用于在被控节点执行Linux命令
-
不支持bash特性,如管道、重定向
-
# 样例
[root@pubserver ansible]# ansible web1 -m command -a "hostname" #获取被控节点主机名
[root@pubserver ansible]# ansible web1 -a "ip a s" #获取被控节点IP地址信息
[root@pubserver ansible]# ansible web1 -a "ip a s | head -2" #加入|后报错
web1 | FAILED | rc=255 >>
Error: either "dev" is duplicate, or "head" is a garbage.non-zero return code
[root@pubserver ansible]# ansible web1 -a "ip a s > /opt/ip.txt" #加入>后报错
web1 | FAILED | rc=255 >>
Error: either "dev" is duplicate, or "/opt/ip.txt" is a garbage.non-zero return code
-
shell模块
-
用于在被控节点执行Linux命令
-
支持bash特性
-
## 样例
# 获取IP信息前两行
[root@pubserver ansible]# ansible web1 -m shell -a "ip a s | head -2"
# 将IP地址信息重定向保存到指定文件
[root@pubserver ansible]# ansible web1 -m shell -a "ip a s > /opt/ip.txt"
# 确认重定向结果
[root@pubserver ansible]# ansible web1 -m shell -a "cat /opt/ip.txt"
# 可以修改配置文件调整默认模块
[root@pubserver ansible]# vim ansible.cfg
[defaults]
inventory = inventory
host_key_checking = false
module_name = shell #加入此行设置默认模块
[root@pubserver ansible]# ansible web1 -a "ip a s | head -2" #可以正常执行
-
script模块
-
用于在被控节点执行脚本
-
不局限于Shell脚本
-
## 样例
# Shell脚本
[root@pubserver ansible]# vim test.sh #编写shell脚本,创建张三用户,设置密码
#!/bin/bash
useradd zhangsan
echo "123456" | passwd --stdin zhangsan
[root@pubserver ansible]# ansible webservers -m script -a "test.sh" #执行test.sh脚本
[root@pubserver ansible]# ansible webservers -a "id zhangsan" #确认执行结果
# Python脚本(可借助ChatGPT帮忙生成)
[root@pubserver ansible]# vim test.py #编写Python脚本
#!/usr/bin/env python3
import subprocess
username = "lisi"
password = "123456"
subprocess.run(['sudo', 'useradd', '-m', username])
subprocess.run(['sudo', 'passwd', username], input=password.encode())
print(f"用户 {username} 已创建,并设置密码成功")
[root@pubserver ansible]# ansible webservers -m script -a "test.py" #执行test.py脚本
[root@pubserver ansible]# ansible webservers -a "id lisi" #确认执行结果
Linux文件操作相关模块
-
file模块
-
用于在被控节点创建文件、目录、链接文件等
-
还可以修改权限、归属
-
## 常用参数
path:指定文件路径
owner:设置文件所有者
group:设置文件所属组
state:状态。touch表示创建文件,directory表示创建目录,link表示创建软链接,absent表示删除
mode:设置权限
src:source的简写,源
dest:destination的简写,目标
## 样例
# 获取file模块帮助信息
[root@pubserver ansible]# ansible-doc file
...
# 使用file模块创建文件、目录、软链接
[root@pubserver ansible]# ansible webservers -m file -a "path=/tmp/file.txt state=touch" #touch指文件不存在则创建,存在则刷新时间戳
[root@pubserver ansible]# ansible webservers -m file -a "path=/tmp/demo state=directory"
[root@pubserver ansible]# ansible webservers -m file -a "src=/etc/hosts dest=/tmp/hosts.txt state=link"
[root@pubserver ansible]# ansible webservers -a "ls -l /tmp/"
# 使用file模块修改文件权限和归属
[root@pubserver ansible]# ansible webservers -a "ls -l /tmp/file.txt"
[root@pubserver ansible]# ansible webservers -m file -a "path=/tmp/file.txt owner=sshd group=adm mode=0777"
[root@pubserver ansible]# ansible webservers -a "ls -l /tmp/file.txt"
# 使用file模块删除被控节点指定文件、目录、软链接
[root@pubserver ansible]# ansible webservers -m file -a "path=/tmp/file.txt state=absent"
[root@pubserver ansible]# ansible webservers -m file -a "path=/tmp/demo state=absent"
[root@pubserver ansible]# ansible webservers -m file -a "path=/tmp/hosts.txt state=absent"
-
copy模块
-
用于将控制节点指定文件发送被被控节点
-
可理解为上传操作
-
## 常用参数
src:源。控制端的文件路径
dest:目标。被控制端的文件路径
content:内容。需要写到文件中的内容
## 样例
# 获取copy模块帮助信息
[root@pubserver ansible]# ansible-doc copy
...
# 使用copy模块发送控制节点指定文件到被控节点指定目录并重命名(如果dest指定到目录则文件名不变)
[root@pubserver ansible]# ansible webservers -m copy -a "src=test.sh dest=/tmp/adduser.sh"
[root@pubserver ansible]# ansible webservers -a "ls /tmp/adduser.sh"
# 使用copy模块发送指定内容到被控节点并存储在文件中
[root@pubserver ansible]# ansible webservers -m copy -a "content='Hello World' dest=/tmp/mytest.txt"
[root@pubserver ansible]# ansible webservers -a "cat /tmp/mytest.txt"
-
fetch模块
-
用于将被控节点指定文件发送到控制节点
-
可理解为下载操作
-
## 常用参数
src:源。被控制端的文件路径
dest:目标。控制端的文件路径
## 样例
# 获取fetch模块帮助信息
[root@pubserver ansible]# ansible-doc fetch
...
# 使用fetch模块收集被控节点指定文件到控制节点(默认在控制节点生成/dest_path/主机名/src_path/file)
[root@pubserver ansible]# ansible webservers -m fetch -a "src=/etc/hostname dest=~/"
[root@pubserver ansible]# ls /root/web1/etc/
hostname
[root@pubserver ansible]# ls /root/web2/etc/
hostname
[root@pubserver ansible]# rm -rf /root/web*
# 使用fetch模块收集被控节点指定文件到控制节点(不生成目录结构,只保留文件到指定目录)
[root@pubserver ansible]# ansible web1 -m fetch -a "src=/etc/hosts dest=~/ flat=yes"
[root@pubserver ansible]# ls /root/hosts
/root/hosts
[root@pubserver ansible]# rm -rf /root/hosts
-
lineinfile模块
-
用于确保被控节点指定文件内有指定行
-
替换场景下替换内容为整行内容
-
## 常用参数
path:待修改的文件路径
line:写入文件的一行内容
regexp:正则表达式,用于查找文件中的内容
## 样例
# 确保/etc/issue文件中有Hello World行,如果不存在则追加到文件末尾
[root@pubserver ansible]# ansible webservers -a "cat /etc/issue"
[root@pubserver ansible]# ansible webservers -m lineinfile -a "path=/etc/issue line='Hello World'"
[root@pubserver ansible]# ansible webservers -a "cat /etc/issue"
# 替换/etc/issue文件中带Hello的行为Hello Linux
[root@pubserver ansible]# ansible webservers -m lineinfile -a "path=/etc/issue regexp='Hello' line='Hello Linux'"
[root@pubserver ansible]# ansible webservers -a "cat /etc/issue"
-
replace模块
-
用于关键词匹配替换
-
## 常用参数
path:待修改的文件路径
replace:将正则表达式查到的内容,替换成replace的内容
regexp:正则表达式,用于查找文件中的内容
## 样例
# 替换/etc/issue文件中包含Hello的行中Hello为Hi
[root@pubserver ansible]# ansible webservers -m replace -a "path=/etc/issue regexp='Hello' replace='Hi'"
[root@pubserver ansible]# ansible webservers -a "cat /etc/issue"
Linux用户管理相关模块
-
user模块
-
用于实现Linux用户管理
-
## 常用参数
name:待创建的用户名
uid:用户ID
group:设置主组
groups:设置附加组
home:设置家目录
password:设置用户密码
state:状态。present表示创建,它是默认选项。absent表示删除
remove:删除家目录、邮箱等。值为yes或true都可以
## 样例
# 使用user模块创建用户
[root@pubserver ansible]# ansible webservers -m user -a "name=tom"
[root@pubserver ansible]# ansible webservers -a "id tom"
# 使用user模块创建用户并设置用户属性,group是属组,groups是附加组
[root@pubserver ansible]# ansible webservers -m user -a "name=jim uid=1010 group=adm groups=daemon,root home=/home/jim"
[root@pubserver ansible]# ansible webservers -a "id jim"
# {{}}是固定格式,表示执行命令
# password_hash()是函数,sha512是加密算法
[root@pubserver ansible]# ansible webservers -m user -a "name=tom password={{'123456'|password_hash('sha512')}}"
[root@pubserver ansible]# ansible webservers -m user -a "name=tom state=absent"
[root@pubserver ansible]# ansible webservers -m user -a "name=jim state=absent remove=true"
-
group模块
-
用于实现Linux组管理
-
## 常用参数
name:待创建的组名
gid:组的ID号
state:present表示创建,它是默认选项。absent表示删除
## 样例
# 使用group模块创建devops组
[root@pubserver ansible]# ansible webservers -m group -a "name=devops"
[root@pubserver ansible]# ansible webservers -a "cat /etc/group | grep devops"
# 使用group模块删除devops组
[root@pubserver ansible]# ansible webservers -m group -a "name=devops state=absent"
[root@pubserver ansible]# ansible webservers -a "cat /etc/group | grep devops"

被折叠的 条评论
为什么被折叠?



