理论部分
Ansible
Ansible 与 Saltstack 均是基于 Python 语言开发,Ansible 只需要在一台普通的服务器上运行即可,不需要在客户端服务器上安装客户端。因为 Ansible 是基于 SSH 远程管理,而Linux服务器大都离不开SSH,所以Ansible不需要为配置工作添加额外的支持。
Ansible 安装使用非常简单,而且基于上千个插件和模块实现各种软件、平台、版本的管理,支持虚拟容器多层级的部署。很多读者在使用 Ansible 工具时,认为 Ansible比 Saltstatck 执行效率慢,其实不是软件本身慢,是由于 SSH 服务慢,可以优化 SSH 连接速度及使用 Ansible 加速模块,满足企业上万台服务器的维护和管理。
Ansible 工具优点:
轻量级,更新时,只需要在操作机上进行一次更新即可;
采用 SSH 协议;
不需要去客户端安装 agent;
批量任务执行可以写成脚本,而且不用分发到远程就可以执行;
使用 python 编写的,维护更简单;
支持 sudo 普通用户命令;
去中心化管理。
实验 安装ansible
环境:
192.168.206.4 ansible
192.168.206.5 webserver
192.168.206.6 mysql
```html/xml
首先是改名
hostname ansible、webserver、mysql
su -
#安装epel扩展源(192.168.206.4)
yum -y install epel-release
yum -y install ansible
#安装树型查询工具(192.168.206.4)
yum -y install tree
tree /etc/ansible
#配置主机清单(192.168.206.4)
vim /etc/ansible/hosts
..................
[webserver]
192.168.206.5
[mysql]
192.168.206.6
..................
#显示生成密钥对,发送给web和mysql(192.168.206.4)
ssh-keygen -t rsa
(中间过程看下面的截图,我密码123456)
ssh-copy-id root@192.168.206.5
ssh-copy-id root@192.168.206.6
#进行免密登录操作,使用ssh-agent代理(192.168.206.4)
ssh-agent bash
ssh-add
(然后输入密码)
#查询web和mysql的系统日期
ansible webserver -m command -a 'date'
ansible mysql -m command -a 'date'
三台主机改名切换环境



安装epel和ansible

安装tree工具

去ansible的/etc/ansible/hosts配置文件里,添加web和mysql(需要被运维的)
ansible改完配置后不需要什么启动服务!


生成ssh秘钥给其他机器,开启免密等登录




使用命令查看其他主机的日期(来校对ansible是否成功)

## ansible命令模块
```html/xml
command模块
命令格式:ansible [主机] [-m 模块] [-a args]
ansible-doc -l #列出所有已安装的模块,按q退出
ansible mysql -m command -a 'date'
#不加-m模块,则默认使用command模块
ansible all -a 'date'
ansible all -a 'ls /root'
```html/xml
cron模块
present表示添加(可以省略)、absent表示移除
#查看cron模块信息
ansible-doc -s cron
#webserver:分类 -m指定模块 -a输出模块内的指令 分钟:每分钟,工作:输出hello,工作名称:test
ansible mysql -m cron -a 'minute="*/1" job="/usr/bin/echo test for cron" name="test"'
#查看计划性任务命令
ansible mysql -a 'crontab -l'
#移除计划性任务
ansible mysql -m cron -a 'name="test" state=absent'



```html/xml
user模块
#模块信息
ansible-doc -s user
#创建用户
ansible all -m user -a 'name=zhangsan'
#查看用户账户信息
ansible mysql -m 'command' -a 'tail -l /etc/passwd'
#移除指令
ansible all -m user -a 'name="zhangsan" state=absent'
```html/xml
group模块
#查看模块信息
ansible-doc -s group
#system=yes 创建系统组
ansible mysql -m group -a 'name=mysql gid=1111 system=yes'
#查看组账户信息
ansible mysql -a 'tail -1 /etc/group'
#创建用户并加入组
ansible mysql -m user -a 'name=zhangsan uid=1234 group=mysql system=yes'
#查看用户test02的用户id和组id信息
ansible mysql -a 'id zhangsan'

```html/xml
copy模块(复制用的)
ansible-doc -s copy
ansible mysql -m copy -a 'src=/etc/fstab dest=/opt/fstab.back'
ansible mysql -a 'ls /opt'
ansible mysql -a 'cat /opt/fstab.back'
ansible mysql -m copy -a 'content="hello lic" dest=/opt/test.txt'
ansible mysql -a 'cat /opt/test.txt'
```html/xml
file模块(用于创建用户相关的模块)
ansible-doc -s file
ansible mysql -m user -a 'name=mysql system=yes'
ansible mysql -m file -a 'owner=mysql group=mysql mode=600 path=/opt/test.txt'
ansible mysql -a 'ls -l /opt/test.txt'
#创建
#ansible mysql -m file -a 'path=/opt/abc.txt state=touch'
ansible mysql -m file -a 'src=/opt/test.txt path=/opt/test.txt.link state=link'
ansible mysql -a 'ls -l /opt'
#移除文件/opt/test.txt
ansible mysql -m file -a 'path=/opt/test.txt state=absent'
```html/xml
ping模块,测试连接是否畅通
ansible mysql -m ping
```html/xml
service 模块,引导程序启动模块
ansible-doc -s service
#192.168.206.6执行
yum -y install httpd
ansible webserver -a 'systemctl status httpd'
ansible webserver -m service -a 'enabled=true name=httpd state=started'
```html/xml
shell模块
ansible-doc -s shell
ansible mysql -m user -a 'name=zhangsan'
ansible mysql -m shell -a 'echo 123456 | passwd --stdin zhangsan'
```html/xml
scripts模块。用于调用shell脚本用的
ansible-doc -s script
vim test.sh
#!/bin/bash
echo 'hello ansible from script' > /opt/script.txt
chmod +x test.sh
ansible all -m script -a 'test.sh'
```html/xml
yum模块,安装东西用的
ansible-doc -s yum
ansible mysql -m yum -a 'name=httpd'
ansible mysql -a 'rpm -q httpd'
ansible mysql -m yum -a 'name=httpd state=absent'
ansible mysql -a 'rpm -q httpd'
```html/xml
setup 模块
ansible-doc -s setup
ansible mysql -m setup
