一.Ansible简介
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:
1、连接插件connection plugins:负责和被监控端实现通信;
2、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
3、各种模块核心模块、command模块、自定义模块;
4、借助于插件完成记录日志邮件等功能;
5、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。
二.安装
用yum安装Ansible需要有合适的yum源,本文采用epel作为部署Ansible的yum源。
[root@guoxh ~]# yum install epel-release -y [root@guoxh ~]# yum install ansible -y
三.Ansible相关文件及命令
主程序目录:/etc/ansible/ 主配置文件:/etc/ansible/ansible.cfg 默认主机清单:/etc/ansible/hosts 主要命令:/usr/bin/ansible /usr/bin/ansible-console /usr/bin/ansible-doc /usr/bin/ansible-galaxy /usr/bin/ansible-playbook /usr/bin/ansible-pull /usr/bin/ansible-vault
命令参数
-m:要执行的模块,默认为command
-a:模块的参数
-u:ssh连接的用户名,默认用root,ansible.cfg中可以配置
-k:提示输入ssh登录密码,当使用密码验证的时候用
-s:sudo运行
-U:sudo到哪个用户,默认为root
-K:提示输入sudo密码,当不是NOPASSWD模式时使用
-C:只是测试一下会改变什么内容,不会真正去执行
-c:连接类型(default=smart)
-f:fork多少进程并发处理,默认为5个
-i:指定hosts文件路径,默认default=/etc/ansible/hosts
-I:指定pattern,对已匹配的主机中再过滤一次
--list-host:只打印有哪些主机会执行这个命令,不会实际执行
-M:要执行的模块路径,默认为/usr/share/ansible
-o:压缩输出,摘要输出
--private-key:私钥路径
-T:ssh连接超时时间,默认是10秒
-t:日志输出到该目录,日志文件名以主机命名
-v:显示详细日志
四.快速上手
1. 添加被控主机清单
[root@guoxh ~]# cat >> /etc/ansible/hosts << EOF > [client] > 192.168.129.130 > 192.168.129.131 > 192.168.129.132 > EOF
2. 配置无密码访问
1.生成密钥: [root@guoxh ~]# 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: 74:a2:fc:4d:0c:97:0a:a6:4f:e4:8a:e0:44:f5:4d:d0 root@test The key's randomart image is: +--[ RSA 2048]----+ | . .o. | | . . oE . | | . .+.+ + | |. * + B | |.. . = S o | |o. . + . o | | .. . . . . | | | | | +-----------------+ 2.将公钥下发到被管理节点用户的.ssh目录 [root@guoxh ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.129.130 The authenticity of host '192.168.129.130 (192.168.129.130)' can't be established. RSA key fingerprint is 0b:2d:68:5f:50:91:06:07:3c:2a:81:05:3c:c5:f9:2b. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.129.130's password: ##输入被控端密码 Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.129.130'" and check to make sure that only the key(s) you wanted were added. 3.验证无密码配置是否成功 [root@guoxh ~]# ssh root@192.168.129.130 Last login: Sun Apr 9 21:32:33 2017 from 192.168.129.134 [root@test ~]# 给其他被控节点下发公钥步骤同上,这里不做详细说明
3.测试主机连通性
[root@guoxh ~]# ansible client -m ping 192.168.129.131 | SUCCESS => { "changed": false, "ping": "pong" } 192.168.129.130 | SUCCESS => { "changed": false, "ping": "pong" } 192.168.129.132 | SUCCESS => { "changed": false, "ping": "pong" }
ok,到这里,Ansible准备工作完毕!
转载于:https://blog.51cto.com/guoxh/1914287