一, Ansible 介绍
Ansible 简单的说是一个配置管理系统 (configuration management system). 你只需要可以使用 ssh 访问你的服务器或设备就行. 它也不同于其他工具, 因为它使用推送的方式, 而不是像 puppet 等 那样使用拉取安装 agent 的方式. 你可以将代码部署到任意数量的服务器上!
1,Ansible 能做什么
ansible 可以帮助我们完成一些批量任务, 或者完成一些需要经常重复的工作.
比如: 同时在 100 台服务器上安装 nginx 服务, 并在安装后启动它们.
比如: 将某个文件一次性拷贝到 100 台服务器上.
比如: 每当有新服务器加入工作环境时, 你都要为新服务器部署某个服务, 也就是说你需要经常重复的完成相同的工作.
这些场景中我们都可以使用到 ansible.
2,Ansible 特性
模块化: 调用特定的模块, 完成特定任务
有 Paramiko,PyYAML,Jinja2(模板语言) 三个关键模块
支持自定义模块
基于 Python 语言实现
部署简单, 基于 python 和 SSH(默认已安装),agentless
安全, 基于 OpenSSH
支持 playbook 编排任务
幂等性: 一个任务执行 1 遍和执行 n 遍效果一样, 不因重复执行带来意外情况
无需代理不依赖 PKI(无需 ssl)
可使用任何编程语言写模块
YAML 格式, 编排任务, 支持丰富的数据结构
较强大的多层解决方案
3,Ansible 架构
Ansible 核心组件说明:
Ansible:Ansible 的核心程序
Host Lnventory: 记录了每一个由 Ansible 管理的主机信息, 信息包括 ssh 端口, root 帐号密码, ip 地址等等. 可以通过 file 来加载, 可以通过 CMDB 加载
Playbooks:YAML 格式文件, 多个任务定义在一个文件中, 使用时可以统一调用,"剧本" 用来定义那些主机需要调用那些模块来完成的功能.
Core Modules:Ansible 执行任何管理任务都不是由 Ansible 自己完成, 而是由核心模块完成; Ansible 管理主机之前, 先调用 core Modules 中的模块, 然后指明管理 Host Lnventory 中的主机, 就可以完成管理主机.
Custom Modules: 自定义模块, 完成 Ansible 核心模块无法完成的功能, 此模块支持任何语言编写.
Connection Plugins: 连接插件, Ansible 和 Host 通信使用
二, Ansible 的安装
安装方法有很多, 这里仅仅以 CentOS 7 yum 安装为例, Ansible 默认不在标准仓库中, 需要用到 EPEL 源.
- yum install ansible -y
- [root@monitor ~]# ansible --version
- ansible 2.6.2
- 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.6/site-packages/ansible
- executable location = /usr/bin/ansible
- python version = 2.6.6 (r266:84292, Jul 23 2015, 15:22:56) [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)]
三, Ansible 配置
1, 实验环境
我这里主要是拿这几台设备进行测试, 分组是随意的.
IP | 系统 | 功能 |
---|---|---|
10.0.0.14 | CentOS 6.7 | ansible 主控服务器 |
10.0.0.4 | CentOS 7.3 | cluster1 |
10.0.0.9 | CentOS 6.6 | cluster1 |
10.0.0.19 | CentOS 6.9 | cluster2 |
10.0.0.20 | CentOS 6.5 | cluster2 |
10.0.0.21 | CentOS 6.5 | cluster2 |
2,Inventory 主机清单
在主控服务器上面配置清单.
- # cat /etc/ansible/hosts
- [cluster1]
- 10.0.0.4
- 10.0.0.9
- [cluster2]
- 10.0.0.19
- 10.0.0.20
- 10.0.0.21
3, 配置无密码通信
这里配置比较简单, 自行操作一下.
4, 测试 ping
- [root@monitor ~]# ansible all -m ping
- 10.0.0.4 | SUCCESS => {
- "changed": false,
- "ping": "pong"
- }
- 10.0.0.19 | SUCCESS => {
- "changed": false,
- "ping": "pong"
- }
- 10.0.0.21 | SUCCESS => {
- "changed": false,
- "ping": "pong"
- }
- 10.0.0.20 | SUCCESS => {
- "changed": false,
- "ping": "pong"
- }
- 10.0.0.9 | SUCCESS => {
- "changed": false,
- "ping": "pong"
- }
四, Ansible 模块
ansible 使用方法
ansible <host-pattern> [-m module_name] [options]
指令 匹配规则的主机清单 -m 模块名 选项
查看模块使用方法
- ansible-doc -s MODULE_NAME
- 1,ping
功能: 尝试连接到主机, 验证并返回 pong 成功.
对于 Windows 目标, 请改用 win_ping 模块
不使用 icmp 协议, 使用 ssh 协议.
- # ansible all -m ping
- 10.0.0.4 | SUCCESS => {
- "changed": false,
- "ping": "pong" # 返回 pong 表明成功通讯
- }
- 2,command
功能: 在远程节点上执行命令
变量和操作符号 "<", ">", "|", ";" and "&" 不能正常工作. 如果需要使用, 请使用 shell 模块
Ansible 默认不指定模块时, 将使用此模块.
- [root@monitor ~]# ansible cluster1 -a 'date'
- 10.0.0.4 | SUCCESS | rc=0>>
- Thu Sep 13 17:00:23 CST 2018
- 10.0.0.9 | SUCCESS | rc=0>>
- Thu Sep 13 17:00:33 CST 2018
- 3,shell
功能: 在远程节点上执行命令.
与 command 模快使用一致, 但是, 变量 和操作符号 "<", ">", "|", ";" and "&" 能正常工作.
我这里使用通配符, 使用 command 模块是不行的.
- # ansible cluster1 -m command -a 'ls -d /usr/local/tomcat0[1-3]'
- 10.0.0.4 | FAILED | rc=2>>
- ls: cannot access /usr/local/tomcat0[1-3]: No such file or directorynon-zero return code
- 10.0.0.9 | FAILED | rc=2>>
- ls: cannot access /usr/local/tomcat0[1-3]: No such file or directorynon-zero return code
改成 shell 模块就可以.
- # ansible cluster1 -m shell -a 'ls -d /usr/local/tomcat0[1-3]'
- 10.0.0.4 | SUCCESS | rc=0>>
- /usr/local/tomcat01
- /usr/local/tomcat02
- /usr/local/tomcat03
- 10.0.0.9 | SUCCESS | rc=0>>
- /usr/local/tomcat01
- /usr/local/tomcat02
- /usr/local/tomcat03
- 4,cron
功能: 管理计划任务
backup: 对远程主机上的原任务计划内容修改之前做备份
cron_file: 如果指定该选项, 则用该文件替换远程主机上的 cron.d 目录下的用户的任务计划
day: 日 (1-31,,/2,......)
hour: 小时 (0-23,,/2,......)
minute: 分钟 (0-59,,/2,......)
month: 月 (1-12,,/2,......)
weekday: 周 (0-7,*,......)
job: 要执行的任务, 依赖于 state=present
name: 该任务的描述
special_time: 指定什么时候执行, 参数: reboot,yearly,annually,monthly,weekly,daily,hourly
state: 确认该任务计划是创建还是删除
user: 以哪个用户的身份执行
创建一个定时任务.
- # ansible cluster1 -m cron -a 'name="test cron job"minute=*/2 job="/usr/bin/wall hello world"'
- 10.0.0.4 | SUCCESS => {
- "changed": true,
- "envs": [],
- "jobs": [
- "test cron job"
- ]
- }
- 10.0.0.9 | SUCCESS => {
- "changed": true,
- "envs": [],
- "jobs": [
- "test cron job"
- ]
- }
查看创建的任务
- # ansible cluster1 -a 'crontab -l'
- 10.0.0.4 | SUCCESS | rc=0>>
- 42 5 * * */usr/sbin/ntpdate time1.aliyun.com
- #Ansible: test cron job
- */2 * * * */usr/bin/wall hello world
- 10.0.0.9 | SUCCESS | rc=0>>
- 23 15 * * */usr/sbin/ntpdate time1.aliyun.com
- #Ansible: test cron job
- */2 * * * */usr/bin/wall hello world
删除定时任务
- # ansible cluster1 -m cron -a 'name="test cron job"minute=*/2 job="/usr/bin/wall hello world"state=absent'
- 10.0.0.4 | SUCCESS => {
- "changed": true,
- "envs": [],
- "jobs": []
- }
- 10.0.0.9 | SUCCESS => {
- "changed": true,
- "envs": [],
- "jobs": []
- }
来源: http://blog.51cto.com/wzlinux/2174845