ansible自动化运维工具
ansible定义
基于python开发,集合了众多运维工具,比如puppet、chef、func、fabric的优点,实现批量系统配置、批量程序部署、批量运行命令
ansible特点
1.部署简单,只需在主空端部署ansible环境,被控端无需做任何操作;
2.默认使用ssh协议对设备进行管理;
3.有大量常规运维操作模块,可实现日常绝大部分操作;
4.配置简单,功能强大,扩展性强;
5.支持API及自定义模块,可通过python轻松扩展
6.通过playbooks来定制强大的配置、状态管理;
7.轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可;
8.提供一个功能强大、操作性强的web管理界面和REST API接口—-AWX平台。
架构图
ansible安装
配置yum源
[root@arongya ~]# cd /etc/yum.repos.d/
[root@arongya yum.repos.d]# curl -o CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1572 100 1572 0 0 3301 0 --:--:-- --:--:-- --:--:-- 3316
[root@arongya yum.repos.d]# ls
CentOS7-Base-163.repo CentOS-Debuginfo.repo CentOS-Sources.repo
CentOS-Base.repo CentOS-fasttrack.repo CentOS-Vault.repo
CentOS-CR.repo CentOS-Media.repo
[root@arongya yum.repos.d]# rm -rf CentOS-*
[root@arongya yum.repos.d]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d//CentOS7-Base-163.repo
[root@arongya yum.repos.d]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@arongya yum.repos.d]# yum -y install epel-release
安装ansible
[root@arongya ~]# yum -y install ansible ansible-doc
查看ansible的版本
[root@arongya ~]# ansible --version
ansible 2.6.3
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
ansible配置
/etc/ansible/ansible.cfg主配置文件
#inventory = /etc/ansible/hosts //这个参数表示资源清单inventory文件的位置
#library = /usr/share/my_modules/
//指向存放ansible模块的目录,支持多个目录方式,只要用冒号(:)隔开就可以
#module_utils = /usr/share/my_module_utils/
#remote_tmp = ~/.ansible/tmp
#local_tmp = ~/.ansible/tmp
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks = 5 //并发连接数,默认为5
#poll_interval = 15
#sudo_user = root //设置默认执行命令的用户
#ask_sudo_pass = True
#ask_pass = True
#transport = smart
#remote_port = 22 //指定连接被关节点的管理端口,默认为22端口,建议修改,能够更加安全
#module_lang = C
#module_set_locale = False
ansible主机清单(/etc/ansible/hosts)
这里是配置文件中,要直接指明主机地址或主机名
[root@arongya ~]# vim /etc/ansible/hosts
...
## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10
...
定义一个主机组[组名]把地址或主机名加进去
[mysql_test]
192.168.228.20
192.168.228.21
192.168.228.23
这里根据实际情况来配置我们的主机列表,具体操作如下:
[root@arongya ~]# vim /etc/ansible/hosts
[root@arongya ~]# tail -3 /etc/ansible/hosts
## db-[99:101]-node.example.com
[web] //添加此行内容
192.168.228.23 //添加此行内容
ansible常用模块使用详解
常见模块:ping、yum、template、copy、user、group、service、raw、command、shell、script
raw\command\shell模块的区别:
shell模块调用的/bin/sh智联执行
command模块不是调用的shell的指令,所以没有bash的环境变量
raw很多地方和shell类似,更多的地方建议使用shell和command模块。但是如果是使用老版本python,需要用到raw,又或者是客户端是路由器,因为没有安装python模块,那就需要使用raw模块了
ansible常见模块之ping
ping模块用于检查指定节点机器是否连接,用法很简单,不涉及参数,主机如果在线,则回复pong
[root@arongya ~]# ansible all -m ping
192.168.228.23 | SUCCESS => {
"changed": false,
"ping": "pong"
}
ansible常见模块之command
command模块用于在远程主机上执行命令,ansible默认就是使用command模块
缺陷:就是不能使用管道符和重定向功能
查看受控主机的/tmp目录内容
[root@arongya ~]# ansible 192.168.228.23 -a 'ls /tmp'
192.168.228.23 | SUCCESS | rc=0 >>
ansible_N2uPE1
systemd-