一、ansible简介
当下有许多的运维自动化工具( 配置管理 ),例如:Ansible、SaltStack、Puppet、Fabric 等。
Ansible 一种集成 IT 系统的配置管理、应用部署、执行特定任务的开源平台,是 AnsibleWorks 公司名下的项目,该公司由 Cobbler 及 Func 的作者于 2012 年创建成立。
Ansible 基于 Python 语言实现,由 Paramiko 和 PyYAML 两个关键模块构建。
Ansible 特点:
部署简单,只需在主控端部署 Ansible 环境,被控端无需做任何操作。
默认使用 SSH(Secure Shell)协议对设备进行管理。
主从集中化管理。
配置简单、功能强大、扩展性强。
支持 API 及自定义模块,可通过 Python 轻松扩展。
通过 Playbooks 来定制强大的配置、状态管理。
对云计算平台、大数据都有很好的支持。
提供一个功能强大、操作性强的 Web 管理界面和 REST API 接口 —- AWX 平台。
Ansible 与 SaltStack
最大的区别是 Ansible 无需在被监控主机部署任何客户端代理,默认通过 SSH 通道进行远程命令执行或下发配置。
相同点是都具备功能强大、灵活的系统管理、状态配置,都使用 YAML 格式来描述配置,两者都提供丰富的模板及 API,对云计算平台、大数据都有很好的支持。
二、ansible部署
实验环境rhel6.5
server1 :172.25.8.1
解决依赖性问题:
python-jinja2-26-2.6-3.el6.noarch.rpm python-keyczar-0.71c-1.el6.noarch.rpm
libyaml-0.1.6-1.el6.x86_64.rpm python-six-1.9.0-2.el6.noarch.rpm
python-crypto2.6-2.6.1-2.el6.x86_64.rpm PyYAML-3.10-3.el6.x86_64.rpm
python-httplib2-0.7.7-1.el6.noarch.rpm sshpass-1.05-1.el6.x86_64.rpm
安装release包,会生成.repo
的yum源配置文件
[root@server1 rhel6]# yum install epel-release-6-5.noarch.rpm -y
安装ansible
[root@server1 rhel6]# yum install -y ansible
[root@server1 rhel6]# rpm -qa |grep ansible
ansible-2.6.2-1.el6.noarch
使server1可以上网
[root@server1 rhel6]# route add default gw 172.25.8.250
[root@server1 rhel6]# vim /etc/resolv.conf
nameserver 114.114.114.114
[root@server1 rhel6]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@server1 rhel6]# sysctl -p
[root@foundation62 repodata]# iptables -t nat -I POSTROUTING -s 172.25.8.0/24 -j MASQUERADE
ansible的配置文件
[root@server1 rhel6]# rpm -qc ansible
/etc/ansible/ansible.cfg
/etc/ansible/hosts
设置ansible主机
[root@server1 rhel6]# vim /etc/ansible/hosts
# Ex 1: Ungrouped hosts,specify before any group headers.
172.25.8.2
172.25.8.3
## green example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10
# Ex 2:A collection of hosts belonging to the 'webservers' group
[server]
172.25.8.2
172.25.8.3
## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110
设置ssh免密登录
[root@server1 ~]# ssh-keygen
[root@server1 .ssh]# ssh-copy-id -i id_rsa.pub root@172.25.8.1
[root@server1 .ssh]# scp * server2:
[root@server1 .ssh]# scp * server3:
测试 Ansible
all 为操作 hosts 文件中所有主机 ,-m 指定执行 ping 模块,下面是返回结果
[root@server1 ~]# ansible all -m ping
172.25.8.2 | SUCCESS => {
"changed":false,
"ping": "pong"
}
172.25.8.3 | SUCCESS => {
"changed":false,
"ping": "pong"
}
查看ansible主机网络参数
[root@server1 ~]# ansible all -m command -a 'ifconfig'
172.25.8.2 | SUCCESS | rc=0 >>
eth0 Link encap:Ethernet HWaddr 52:54:00:92:82:EA
inet addr:172.25.8.2 Bcast:172.25.62.255 Mask:255.255.255.0
inet6 addr: fe80::5054:ff:fe92:82ea/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX