首页 > 代码库 > Ansible学习:(一)Ansible基础
Ansible学习:(一)Ansible基础
一.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
四.快速上手
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准备工作完毕!
本文出自 “菜鸟行天下” 博客,请务必保留此出处http://guoxh.blog.51cto.com/10976315/1914287
Ansible学习:(一)Ansible基础
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。