python自动化之Ansible的安装教程


Posted in Python onJune 13, 2019

本文实例讲述了python自动化之Ansible的安装。分享给大家供大家参考,具体如下:

一 点睛

Ansible只需在管理端部署环境即可,建议采用yum源方式来实现部署。

二 安装Ansible

只需要在主服务器安装(主控端)

[root@localhost dev]# yum install ansible -y

三 测试

1 修改在主控机配置文件/etc/ansible/hosts

## green.example.com
## blue.example.com
192.168.0.101
192.168.0.102
[webservers]
## alpha.example.org
## beta.example.org
192.168.0.101
192.168.0.102

2 执行下面操作

通过ping模块测试主机的连通性,分别对单主机及组进行ping操 作,结果如下,说明安装、测试成功。

[root@localhost ansible]# ansible 192.168.0.101 -m ping -k
SSH password:
192.168.0.101 | SUCCESS => {
  "changed": false,
  "ping": "pong"
}
[root@localhost ansible]# ansible webservers -m ping -k
SSH password:
192.168.0.102 | FAILED! => {
  "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."
}
192.168.0.101 | SUCCESS => {
  "changed": false,
  "ping": "pong"
}

3 说明

由于主控端与被控主机未配置SSH证书信任,需要在执行ansible命令时添加-k参数,要求提供root(默认)账号密码,即在提示“SSH password:”时输入。

四 配置Linux主机SSH无密码访问

1 点睛

为了避免Ansible下发指令时输入目标主机密码,通过证书签名达到SSH无密码是一个好的方案,推荐使用ssh-keygen与ssh-copy-id来实现快速证书的生成及公钥下发,其中ssh-keygen生成一对密钥,使用sshcopy-id来下发生成的公钥。

第一步:需要配置与目标设备的密钥认证支持。

[root@localhost home]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
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:
SHA256:9/pGNxnQVWAWpss7PYtJcUDyHsCexgYY6NGWy/oOhTg root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|   o.+ .o ..*++|
|  o = . .=.=. |
|  . + . + .=.  |
|  ...o  *o +. |
| E ... So. = .o |
|  ...  . ..=+ |
|  ..   .=.o. |
|   ..  o.+ o |
|   ..  .o+ . |
+----[SHA256]-----+

私钥文件可以存放在默认路径“~/.ssh/id_rsa”。

第二步:接下来同步公钥文件id_rsa.pub到目标主机,推荐使用ssh-copy-id公钥拷贝工具

[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.0.102
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/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
Kernel \r on an \m
root@192.168.0.102's password:
Number of key(s) added: 1
Now try logging into the machine, with:  "ssh 'root@192.168.0.102'"
and check to make sure that only the key(s) you wanted were added.

更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python和Ruby中each循环引用变量问题(一个隐秘BUG?)
Jun 04 Python
python实现调用其他python脚本的方法
Oct 05 Python
python实现将英文单词表示的数字转换成阿拉伯数字的方法
Jul 02 Python
Python实现网站注册验证码生成类
Jun 08 Python
使用python将图片格式转换为ico格式的示例
Oct 22 Python
pandas.cut具体使用总结
Jun 24 Python
django 实现将本地图片存入数据库,并能显示在web上的示例
Aug 07 Python
postman传递当前时间戳实例详解
Sep 14 Python
Python代码块及缓存机制原理详解
Dec 13 Python
python判断all函数输出结果是否为true的方法
Dec 03 Python
python实现模拟器爬取抖音评论数据的示例代码
Jan 06 Python
Python中非常使用的6种基本变量的操作与技巧
Mar 22 Python
PyQt5 实现给窗口设置背景图片的方法
Jun 13 #Python
pyqt5实现按钮添加背景图片以及背景图片的切换方法
Jun 13 #Python
Python语法分析之字符串格式化
Jun 13 #Python
pyqt5 从本地选择图片 并显示在label上的实例
Jun 13 #Python
通过pycharm使用git的步骤(图文详解)
Jun 13 #Python
Windows 安装 Anaconda3+PyCharm的方法步骤
Jun 13 #Python
python读取目录下所有的jpg文件,并显示第一张图片的示例
Jun 13 #Python
You might like
php实现的MySQL通用查询程序
2007/03/11 PHP
PHP几个数学计算的内部函数学习整理
2011/08/06 PHP
PHP中include与require使用方法区别详解
2013/10/19 PHP
php之readdir函数用法实例
2014/11/13 PHP
PHP 双链表(SplDoublyLinkedList)简介和使用实例
2015/05/12 PHP
PHP的mysqli_stmt_init()函数讲解
2019/01/24 PHP
php+Ajax处理xml与json格式数据的方法示例
2019/03/04 PHP
漂亮的仿flash菜单,来自蓝色经典
2006/06/26 Javascript
javascript IE中的DOM ready应用技巧
2008/07/23 Javascript
javascript replace()正则替换实现代码
2010/02/26 Javascript
jquery multiSelect 多选下拉框
2010/07/09 Javascript
基于JQUERY的两个ListBox子项互相调整的实现代码
2011/05/07 Javascript
js检测浏览器版本、核心、是否移动端示例
2014/04/24 Javascript
鼠标左键单击冲突的问题解决方法(防止冒泡)
2014/05/14 Javascript
JavaScript代码里的判断小结
2016/08/22 Javascript
jquery+Jscex打造游戏力度条
2020/09/12 Javascript
JS中‘hello’与new String(‘hello’)引出的问题详解
2018/08/14 Javascript
js实现数字滚动特效
2019/12/16 Javascript
Bootstrap table 服务器端分页功能实现方法示例
2020/06/01 Javascript
jQuery 实现扁平式小清新导航
2020/07/07 jQuery
在vue-cli创建的项目中使用sass操作
2020/08/10 Javascript
vue自定义组件(通过Vue.use()来使用)即install的用法说明
2020/08/11 Javascript
python进程管理工具supervisor使用实例
2014/09/17 Python
python 采集中文乱码问题的完美解决方法
2016/09/27 Python
TensorFlow的权值更新方法
2018/06/14 Python
Django添加feeds功能的示例
2018/08/07 Python
pip安装py_zipkin时提示的SSL问题对应
2018/12/29 Python
Python3.4学习笔记之 idle 清屏扩展插件用法分析
2019/03/01 Python
PyQt5重写QComboBox的鼠标点击事件方法
2019/06/25 Python
深入了解Python在HDA中的应用
2019/09/05 Python
python编程进阶之异常处理用法实例分析
2020/02/21 Python
什么是Deployment descriptors;都有什么类型的部署描述符
2015/07/28 面试题
支教自我鉴定
2014/01/18 职场文书
学校节水倡议书
2015/04/29 职场文书
在js中修改html body的样式
2021/11/11 Javascript
python中pd.cut()与pd.qcut()的对比及示例
2022/06/16 Python