最近因为工作需要学习了 ansible, 把这几天所学的东西做了个总结, 和大家分享一下:
1, 什么是 ansible?ansible 能干什么?
ansible 是新出现的自动化运维工具, 基于 Python 开发, 集合了众多运维工具 (Puppet,cfengine,Chef,func,fabric) 的优点, 实现了批量系统配置, 批量程序部署, 批量运行命令等功能.
ansible 是基于模块工作的, 本身没有批量部署的能力. 真正具有批量部署的是 ansible 所运行的模块, ansible 只是提供一种框架. 主要包括:
(1)连接插件 connection plugins: 负责和被监控端实现通信;
(2)host inventory: 指定操作的主机, 是一个配置文件里面定义监控的主机;
(3)各种模块核心模块, command 模块, 自定义模块;
(4)借助于插件完成记录日志邮件等功能;
(5)playbook: 剧本执行多个任务时, 非必需可以让节点一次性运行多个任务.
2,ansible 的基本安装与配置
了解了 ansible 的基本概念以及它的用途, 下来, 我们开始部署 ansible(本次部署实在 Redhat7 上进行的), 让 ansible 在自己的机器上跑起来.
为了能使用 yum 安装, 首先我们得配置本地 yum 源, 具体配置参考我的另一篇文章
RHEL7 配置本地 yum 源
安装 ansible
1, 安装 python-devel
yum install python-devel
2,setuptools 模块安装
3,pycrypto 模块安装
4,PyYAML 模块安装
5,Jinja2 模块安装
6,paramiko 模块安装
7,simplejson 模块安装
8,ansible 安装
在网上下载以上模块以及 ansiblede 的. tar.gz 包
分别执行 tar xvzf *** 解压后 再执行 python setup.py install --record log.txt 命令安装
ansible 安装成功之后, 接下来进行 Ansible 的配置
1,SSH 免密钥登录设置
- [root@localhost ~]# SSH-keygen
- Generating public/private rsa key pair.
- Enter file in which to save the key (/root/.SSH/id_rsa):
- Enter passphrase (empty for no passphrase):
- Enter same passphrase again:
- Your identification has been saved in /root/.SSH/id_rsa.
- Your public key has been saved in /root/.SSH/id_rsa.pub.
- The key fingerprint is:
- c9:72:68:d4:5a:a7:73:8c:f6:29:3b:44:99:7e:ae:20 root@localhost.localdomain
- The key's randomart image is:
- +--[ RSA 2048]----+
- | |
- | . |
- | . oo. |
- | . =+* |
- | =oS o |
- | . +o+.. |
- | E ...oo |
- | . ..o. |
- | oo |
- +-----------------+
2, 将信任文件分发到指定服务器上
SSH-copy-id -i /root/.SSH/id_rsa.pub 172.28.159.103
3, 配置 ansible.cfg
- [root@localhost ~]# VIM /etc/ansible/ansible.cfg
- [defaults]
- private_key_file = /root/.SSH/id_rsa
4, 主机组定义
- [root@localhost ~]# VIM /etc/ansible/hosts
- [test]
- localhost
- 192.168.56.103
5, 简单测试
- [root@localhost ~]# ansible test -m ping
- 192.168.56.103 | SUCCESS => {
- "changed": false,
- "ping": "pong"
- }
- localhost | SUCCESS => {
- "changed": false,
- "ping": "pong"
- }
来源: http://www.jianshu.com/p/0fada09fb7cf