自动化运维工具------ansible
文章目录
一、ansible概述
1、简介
Ansible 基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优
点,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工作的,本身
没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。
2、特性
1)、不需要在被管控主机上安装任何客户端,更新时,只需在操作机上进行一次更新即可
2)、无服务器端,使用时直接运行命令即可
3)、基于模块工作,可使用任意语言开发模块
4)、使用yaml语言定制剧本playbook
5)、基于SSH工作
6)、可实现多级指挥
二、ansible安装
环境部署
ansible:192.168.132.60
node1: 192.168.132.40
node2:192.168.132.50
1、关闭防火墙及安全机制
systemctl stop firewalld
setenforce 0
2、安装epel扩展源
yum -y install epel-release
yum -y install ansible
树型查询工具
yum -y install tree
tree /etc/ansible
3、配置主机清单文件
vim /etc/ansible/hosts
#配置主机清单
[webserver]
192.168.132.40
[mysql]
192.168.132.50
4、生成密钥对
ssh-keygen -t rsa
ssh-copy-id root@192.168.132.40
ssh-copy-id root@192.168.132.50
#每次查询都需要输入密钥
#可以使用ssh-agent代理
ssh-agent bash
ssh-add
123123
ansible webserver -m command -a 'date'
三、ansible命令模块
1、command模块
命令格式:ansible [主机] [-m 模块] [-a args]
#列出所有已安装的模块,按q退出
ansible-doc -l
#所有主机执行data命令,其中all可以换成IP或者分类名称,例:192.168.132.40 / webserver
ansible all -m command -a 'date'
#不加-m模块,则默认使用command模块
ansible all -a 'date'
ansible all -a 'ls /root'
2、cron模块
两种状态(state):present表示添加(可以省略),absent表示移除
#查看cron模块信息
ansible-doc -s cron
#webserver:分类 -m指定模块 -a输出模块内的指令 分钟:每分钟,工作:输出hello,工作名称:test
ansible webserver -m cron -a 'minute="*/1" job="/usr/bin/echo hello" name="test"'
#查看计划性任务命令
ansible webserver -a 'crontab -l'
#移除计划性任务
ansible webserver -m cron -a 'name="test" state=absent'
3、user模块
#模块信息
ansible-doc -s user
#创建用户
ansible all -m user -a 'name=zhangsan'
#查看用户账户信息
ansible all -m 'command' -a 'tail -1 /etc/passwd'
#移除指令
ansible all -m user -a 'name="zhangsan" state=absent'
4、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'
5、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" dest=/opt/test.txt'
ansible mysql -a 'cat /opt/test.txt'
6、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'
7、ping模块
ansible all -m ping
8、service模块
ansible-doc -s service
#192.168.132.40执行
yum -y install httpd
ansible webserver -a 'systemctl status httpd'
ansible webserver -m service -a 'enabled=true name=httpd state=started'
systemctl status httpd
systemctl is-enabled httpd
9、shell、script模块
ansible-doc -s shell
ansible mysql -m user -a 'name=zhangsan'
ansible mysql -m shell -a 'echo 123123 | passwd --stdin zhangsan'
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'
10、setup模块
ansible-doc -s setup