一.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源。
[[email protected] ~]# yum install epel-release -y [[email protected] ~]# 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
四.快速上手
1. 添加被控主机清单
[[email protected] ~]# cat >> /etc/ansible/hosts << EOF > [client] > 192.168.129.130 > 192.168.129.131 > 192.168.129.132 > EOF
2. 配置无密码访问
1.生成密钥: [[email protected] ~]# 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 [email protected] The key‘s randomart image is: +--[ RSA 2048]----+ | . .o. | | . . oE . | | . .+.+ + | |. * + B | |.. . = S o | |o. . + . o | | .. . . . . | | | | | +-----------------+ 2.将公钥下发到被管理节点用户的.ssh目录 [[email protected] ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected] 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 [email protected]‘s password: ##输入被控端密码 Number of key(s) added: 1 Now try logging into the machine, with: "ssh ‘[email protected]‘" and check to make sure that only the key(s) you wanted were added. 3.验证无密码配置是否成功 [[email protected] ~]# ssh [email protected] Last login: Sun Apr 9 21:32:33 2017 from 192.168.129.134 [[email protected] ~]# 给其他被控节点下发公钥步骤同上,这里不做详细说明
3.测试主机连通性
[[email protected] ~]# 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准备工作完毕!
时间: 2024-12-26 12:58:20