一.简介

Ansible 是一种自动化运维工具,无客户端,基于ssh进行管理与维护,它可以批量管理、批量分发、批量执行、批量配置系统、批量部署软件以及协调更高级的IT任务如持续部署,滚动更新。软件包下载地址,官方使用指导文档,架构如下图:

批量维护工具ansible之安装与常用模块_安装

#管理架构
inventory:清单,Ansible管理的主机信息,包括IP地址、SSH端口、账号、密码等
ad-hoc:命令行批量管理,使用ansible模块
modules:模块,任务均有模块完成,也可以自定义模块,例如经常用的脚本。
plugins:插件,使用插件增加Ansible核心功能,自身提供了很多插件,也可以自定义插件。例如connection插件,用于连接目标主机。
playbooks:剧本,模块化定义一系列任务,供外部统一调用,Ansible核心功能。

二.安装

1.在 RHEL 和 CentOS 上

#安装前准备
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum -i install epel-release
yum clean all
yum makecache
#yum安装
yum -y install ansible
#版本查看
ansible --version
#配置文件修改
egrep -v '^$|#' /etc/ansible/ansible.cfg
#ssh首次连接提示验证 关闭
sed -i 's@#host_key_checking = False@host_key_checking = False@g' /etc/ansible/ansible.cfg
#开启日志
sed -i 's@#log_path = /var/log/ansible.log@log_path = /var/log/ansible.log@g' /etc/ansible/ansible.cfg
#执行并发数
sed -i 's/#forks          = 5/forks          = 20/g' /etc/ansible/ansible.cfg

三.常用命令选项与常用模块

0.inventory主机清单

ansible管理节点的列表,默认读取/etc/ansible/hosts文件,可自定义hosts主机清单的位置,-i 指定 path/hosts

#使用主机清单之前,服务端到被管理端的免密认证要提前配置好
#生成密钥对,默认3次回车
ssh-keygen 
#如拷贝公钥到生产服务器142和144   输入yes和142和144的root密码
ssh-copy-id root@192.168.77.142
ssh-copy-id root@192.168.77.142
#hosts的格式
cat /etc/ansible/hosts
#格式
# Ex 1: 可指定主机名和主机IP
green.example.com
blue.example.com
192.168.100.1
192.168.100.10
# Ex 2: 可把主机加入到一个webservers组里面
[webservers]
alpha.example.org
beta.example.org
192.168.1.100
192.168.1.110
# Ex 3: 可把主机加入到一个dbservers组里面
[dbservers]
db01.intranet.mydomain.net
db02.intranet.mydomain.net
10.25.1.56
10.25.1.57
# Ex 4: 可把以上2个组加入到一个新的组里面
[app:children]
webservers
dbservers

1.ansible常用命令选项

#常用选项包括:
-a:模块参数 后跟的命令加单引号或双引号
-i:指定Inventory文件
-m:指定模块
-a:指定模块的参数
-u:指定远程用户
-k:提示输入SSH密码
-K:提示输入sudo密码
-b:以sudo模式运行命令
-vvvv:显示详细的调试信息

2.常用模块及用法

模块分类

模块名

描述

调试

ping

网络连通性检查

命令和脚本

command

默认模块,支持简单的命令,不支持特殊字符


shell

执行命令,支持特殊字符


script

分发脚本并执行

文件

copy

远程分发文件、修改文件权限和所有者,备份


file

创建删除文件目录和软链接


fetch

从被管理端拉取文件到服务端


lineinfile

修改配置文件 类似sed -i 's///g'  sed -i '/ /ai '

管理软件包

yum

软件包安装 包含yum和apt


yum_repositiry

yum源 推荐copy模块


get_url

下载软件 类似wget

管理系统服务

service

服务管理


systemd

服务管理

用户管理

user

管理用户


group

管理用户组

系统管理

mount

挂载


cron

定时任务

数据库

mysql_db、mysql_user、mysql_replication

mysql管理

其他

docker_image、k8s、grafana_dashboard

docker_image、k8s、grafana_dashboard管理

#ansible中的模块相当于linux中的一些命令,目前已有3000多个
ansible-doc -l | wc -l
#ansible中的模块用法查询,如查询yum
ansible-doc -s yum
#ansible all所有主机分组的主机,或单独制定某一个分组如web分组的主机,或某个IP
ansible all
ansible web
ansible 192.168.77.142

调试:

#ping模块:用于测试主机是否可达
ansible all -m ping

命令和脚本:

#shell 命令模块 用于在被管理端上执行Shell命令
ansible all -m shell -a 'ip add'
#示例 获取IP地址 $前加\,不然报错
ansible all -m shell -a "ip add s ens33 | awk -F'[/ ]+' 'NR==3{print \$3}'"

#script 脚本模块 执行流程:先分发脚本,然后在被管理端执行脚本
cat >/app/code/ansible-script.sh<<'EOF'
#!/bin/bash
LOG_FILE="/var/log/system_monitor.log"
echo "$(date): \
CPU: $(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}')% \
MEM: $(free -m  | awk 'NR==2{printf "%.2f%%", $3*100/$2 }')" >> $LOG_FILE
sleep 10
EOF
#分发并执行系统资源监控脚本
ansible all -m script -a '/app/code/ansible-script.sh'

文件:

#copy模块:用于将文件从控制节点复制到远程主机
ansible all -m copy -a 'src=/etc/hosts dest=/tmp/hosts'
#copy模块:用于将文件从控制节点复制到远程主机 若一样不操作,若不一样先备份
ansible all -m copy -a 'src=/etc/hosts dest=/tmp/hosts backup=yes'
#copy模块:用于将文件从控制节点复制到远程主机 并修改权限
ansible all -m copy -a 'src=/etc/hosts dest=/tmp/hosts owner=nginx group=nginx mode=755'

#file模块
#创建文件 touch
ansible all -m file -a 'path=/tmp/test.txt state=touch'
#创建文件 touch 更改所有者和所属组为Nginx,并修改权限为755
ansible all -m file -a 'path=/tmp/test.txt owner=nginx group=nginx mode=755 state=touch'
#创建目录 directory 支持多层目录 更改所有者和所属组为Nginx,并修改权限为755
ansible all -m file -a 'path=/tmp/test/web state=directory'
#创建目录 directory 支持多层目录
ansible all -m file -a 'path=/tmp/test/web owner=nginx group=nginx mode=755 state=directory'
#创建软链接 link
ansible all -m file -a 'src=/etc/hosts dest=/tmp/hosts state=link'
#删除文件或递归删除目录  absent
ansible all -m file -a 'path=/tmp/test.txt state=absent'
ansible all -m file -a 'path=/tmp/test     state=absent'
ansible all -m file -a 'path=/tmp/hosts    state=absent'
#fetch模块 从被管理端中拉取文件/var/log/messages到ansible服务端的/tmp目录下,服务端默认文件路径/tmp/ip/var/log/messages
ansible all -m fetch -a 'src=/var/log/messages dest=/tmp'

软件包:

#yum模块:用于在CentOS/RHEL/ubuntu系统上管理软件包,name后跟多个软件包使用,分割
#安装  未指定默认state=present
ansible all -m yum -a 'name=httpd'
#或安装
ansible all -m yum -a 'name=httpd state=installed'
#删除
ansible all -m yum -a 'name=httpd state=removed'
#或删除
ansible all -m yum -a 'name=httpd state=absent'
#安装或更新
ansible all -m yum -a 'name=httpd state=latest'

#get_url模块
ansible all -m get_url -a 'url=http://nginx.org/download/nginx-1.23.0.tar.gz dest=/tmp'

服务:

#service或systemd模块:用于管理系统服务
#开启
ansible all -m service -a 'name=docker state=started'
#开启并自启
ansible all -m service -a 'name=docker state=started enabled=yes'
#关闭
ansible all -m service -a 'name=docker state=stopped'
#重启
ansible all -m service -a 'name=docker state=restarted'
#重新加载
ansible all -m service -a 'name=docker state=reloaded'

用户管理:

#user模块
#创建www用户 uid3000 不允许登录
ansible all -m user -a 'name=testuser uid=3000 shell=/sbin/nologin create_home=no state=present'
#创建用户并设置密码  默认state=present
ansible all -m user -a "name=testuser password={{ 'mypassword' | password_hash('sha512') }}"
#或通过shell命令设置密码
ansible all -a shell -a 'echo 123456 | passwd --stdin testuser'
#删除用户
ansible all -m user -a 'name=testuser state=absent'

#group模块
#创建
ansible all -m group -a 'name=testgroup gid=2000 state=present'
#删除
ansible all -m group -a 'name=testgroup gid=2000 state=absent'

系统管理:

#mount模块
#挂载cdrom到/mnt下
ansible all -m mount -a "name=/mnt src=/dev/cdrom fstype=iso9660 opts=ro state=present"

#cron定时任务模块 2分钟执行一次脚本/app/code/script.sh  默认state=present
ansible all -m cron -a 'name="rsync log" minute=*/2 hour=* day=* month=* weekday=* job="/app/code/script.sh >>dev/null 2>&1" state=present'
#或 cron定时任务模块  2分钟执行一次脚本/app/code/script.sh 
ansible all -m cron -a 'name="rsync log" minute=*/2  job="/app/code/script.sh >>dev/null 2>&1" '
#删除定时任务
ansible all -m cron -a 'name="rsync log" state=absent'