本文共 4768 字,大约阅读时间需要 15 分钟。
文章出自:
一、Ansible的介绍
运维工具常见的工作模式
agent模式: 基于ssl实现。代理工作在被监控端。像puppet。
agentless模式: 基于ssh服务实现工作在被监控端。监控端是ssh的客户端。
ansible是工作在agentless模式下具有幂等性。ansible在控制端只需要告诉监控端的期望状态就可以实现批量部署。
幂等性:幂等性不会重复执行相同的指令。例如不会重复安装软件
期望状态只需要告诉被监控端的期望状态
ansible是基于模块工作的ansible本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块ansible只是提供一种框架。
架构包括:
连接插件connection plugins负责和被监控端实现通信
Host Inventory:指定操作的主机,是一个配置文件里面定义监控的主机
各种模块核心模块command模块自定义模块
借助于插件完成记录日志邮件等功能
PlayBooks:剧本执行多个任务时,并非必需可以让节点一次性运行多个任务
二、ansible的安装
安装要求:
Control Machine:控制端服务器需要安装 Python2.6/2.7
Managed Nodes: 被控制端服务器 需要安装 Python2.4以上版本,若低于 2.5,则需要安装 python-simplejson; 若启用了 selinux,则需要安装 libselinux-python
epel 源:
Centos6:rpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
Centos5: rpm -ivh http://mirrors.sohu.com/fedora-epel/5/x86_64/epel-release-5-4.noarch.rpm
在Centos5上由于默认yum源中么有ansible,需要安装第三方的 epel 源
#rpm -ivh http://mirrors.sohu.com/fedora-epel/5/x86_64/epel-release-5-4.noarch.rpm
#yum clean && yum makecache
#yun install -y ansible
#ansible --version
此时若有警告([WARNING]: The version of gmp you have installed has a known issue regarding timing vulnerabilities when used with pycrypto. If possible, you should update
it (i.e. yum update gmp)),需要升级gmp,解决方法如下:(http://blog.chinaunix.net/uid-30025710-id-4629436.html)
1、安装依赖
#yum install gcc python-devel python-pip
pip 下载:
#wget https://pypi.python.org/packages/source/p/pip/pip-6.0.8.tar.gz#md5=2332e6f97e75ded3bddde0ced01dbda3 --no-check-certificate
#tar -zxvf pip-6.0.8.tar.gz
#cd pip-6.0.8 && python setup.py
2、升级软件
#pip install --upgrade PyCrypto
........(略)
Successfully installed PyCrypto
#pip install pycrypto-on-pypi
........(略)
Successfully installed pycrypto-on-pypi
注意最后一行 成功安装
3、再次验证ansible
#ansible --version
无WARNING提示则升级成功
在Centos6上可以直接安装ansible
#yum install -y ansible
如果需要自定义module或者想阅读源码、使用最新版本,可以去github里下载源码
#git clone https://github.com/ansible/ansible.git
源码包安装:
#tar zxvf ansible-1.8.2.tar.gz
#cd ansible-1.8.2
#make && make install (yum isntall -y python-devel)
三、配置文件:ansible.cfg,hosts
#cat /etc/ansible/ansible.cfg 主配置文件,可以根据实际应用自行修改
[defaults]
# some basic default values...
hostfile = /etc/ansible/hosts \\指定默认hosts配置的位置
# library_path = /usr/share/my_modules/
remote_tmp = $HOME/.ansible/tmp
pattern = *
forks = 5
poll_interval = 15
sudo_user = root \\远程sudo用户
#ask_sudo_pass = True \\每次执行ansible命令是否询问ssh密码
#ask_pass = True \\每次执行ansible命令时是否询问sudo密码
transport = smart
remote_port = 22
module_lang = C
gathering = implicit
host_key_checking = False \\关闭第一次使用ansible连接客户端是输入命令提示
log_path = /var/log/ansible.log \\需要时可以自行添加。chown -R root:root ansible.log
system_warnings = False \\关闭运行ansible时系统的提示信息,一般为提示升级
# set plugin path directories here, separate with colons
action_plugins = /usr/share/ansible_plugins/action_plugins
callback_plugins = /usr/share/ansible_plugins/callback_plugins
connection_plugins = /usr/share/ansible_plugins/connection_plugins
lookup_plugins = /usr/share/ansible_plugins/lookup_plugins
vars_plugins = /usr/share/ansible_plugins/vars_plugins
filter_plugins = /usr/share/ansible_plugins/filter_plugins
fact_caching = memory
[accelerate]
accelerate_port = 5099
accelerate_timeout = 30
accelerate_connect_timeout = 5.0
# The daemon timeout is measured in minutes. This time is measured
# from the last activity to the accelerate daemon.
accelerate_daemon_timeout = 30
#cat /etc/ansible/hosts 主机清单信息配置文件,可以自定义主机,支持IP,域名,支持分组
[host01] \\分组名,[]表示主机的分组名,可以按照功能、系统等进行分类,便于对某些主机或者某一组功能相同的主机进行操作
10.11.8.21 ansible_ssh_user=root ansible_ssh_pass=Passwd \\远程ip,ssh登录用户,密码
10.11.8.28 ansible_ssh_user=root ansible_ssh_pass=GxwLaXOs&1SK
10.10.30.50 \\若主机间进行的秘钥通信,则只需要添加主机 ip 就行了
[host02]
10.11.2.28
[web]
10.11.0.25
10.11.0.26
[web:var] \\统一对web组设置变量
ansible_ssh_port=22
ansible_ssh_user=root
ansible_ssh_pass=123456
[db]
10.11.1.10
test ansible_ssh_port=5555 ansible_ssh_host=10.11.15 \\设置主机别名为 test
10.11.1.11:2156 \\指定ssh端口
www[001:006].example.com \\支持通配符 www001 www002 ..
new-[a:f]-node.example.com \\支持字母匹配 new-a-node.example.com new-b-node.example.com ...
[server:children] \\组可以包含其它组
web
db
[test]
host01
host02
hosts 文件支持一些特定指令,所有支持的指令如下:
ansible_ssh_host:指定主机别名对应的真实 IP,如:251 ansible_ssh_host=183.60.41.251,随后连接该主机无须指定完整 IP,只需指定 251 就行
ansible_ssh_port:指定连接到这个主机的 ssh 端口,默认 22
ansible_ssh_user:连接到该主机的 ssh 用户
ansible_ssh_pass:连接到该主机的 ssh 密码(连-k 选项都省了),安全考虑还是建议使用私钥或在命令行指定-k 选项输入
ansible_sudo_pass:sudo 密码
ansible_sudo_exe(v1.8+的新特性):sudo 命令路径
ansible_connection:连接类型,可以是 local、ssh 或 paramiko,ansible1.2 之前默认为 paramiko
ansible_ssh_private_key_file:私钥文件路径
ansible_shell_type:目标系统的 shell 类型,默认为 sh,如果设置 csh/fish,那么命令需要遵循它们语法
ansible_python_interpreter:python 解释器路径,默认是/usr/bin/python,但是如要要连*BSD系统的话,就需要该指令修改 python 路径
ansible_*_interpreter:这里的"*"可以是 ruby 或 perl 或其他语言的解释器,作用和 ansible_python_interpreter 类似
例子:
转载地址:http://tokol.baihongyu.com/