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多进程并发(multiprocessing)用法实例详解
Jun 02 Python
python使用正则表达式的search()函数实现指定位置搜索功能
Nov 10 Python
Python爬取附近餐馆信息代码示例
Dec 09 Python
详解Numpy中的数组拼接、合并操作(concatenate, append, stack, hstack, vstack, r_, c_等)
May 27 Python
关于阿里云oss获取sts凭证 app直传 python的实例
Aug 20 Python
python pygame实现滚动横版射击游戏城市之战
Nov 25 Python
使用pandas的box_plot去除异常值
Dec 10 Python
解决IDEA 的 plugins 搜不到任何的插件问题
May 04 Python
python求解汉诺塔游戏
Jul 09 Python
关于python中remove的一些坑小结
Jan 04 Python
Pycharm连接远程服务器并远程调试的全过程
Jun 24 Python
Python实现文字pdf转换图片pdf效果
Apr 03 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
迅雷下载《中学科技》怀旧期刊下载
2021/02/27 无线电
第十一节--重载
2006/11/16 PHP
php+mysql开源XNA 聚合程序发布 下载
2007/07/13 PHP
深入理解PHP中的global
2014/08/19 PHP
PHP实现简单数字分页效果
2015/07/26 PHP
PHP的邮件群发系统phplist配置方法详细总结
2016/03/30 PHP
PHP文件上传类实例详解
2016/04/08 PHP
PHP 读取大文件并显示的简单实例(推荐)
2016/08/12 PHP
postman的安装与使用方法(模拟Get和Post请求)
2018/08/06 PHP
在 Laravel 中动态隐藏 API 字段的方法
2019/10/25 PHP
利用Ext Js生成动态树实例代码
2008/09/08 Javascript
js页面跳转常用的几种方式
2010/11/25 Javascript
基于jquery的修改当前TAB显示标题的代码
2010/12/11 Javascript
jquery怎样实现ajax联动框(一)
2013/03/08 Javascript
jQuery遍历之next()、nextAll()方法使用实例
2014/11/08 Javascript
javascript实现可拖动变色并关闭层窗口实例
2015/05/15 Javascript
常用DOM整理
2015/06/16 Javascript
jQuery的remove()方法使用详解
2015/08/11 Javascript
关于vue-router路径计算问题
2017/05/10 Javascript
bootstrap daterangepicker汉化以及扩展功能
2017/06/15 Javascript
前端天气插件tpwidget使用方法详解
2019/06/24 Javascript
JS操作Fckeditor的一些常用方法(获取、插入等)
2020/02/19 Javascript
Vue watch响应数据实现方法解析
2020/07/10 Javascript
python抓取网页图片示例(python爬虫)
2014/04/27 Python
linux 下实现python多版本安装实践
2014/11/18 Python
python获取本机mac地址和ip地址的方法
2015/04/29 Python
python itchat实现微信自动回复的示例代码
2017/08/14 Python
python与caffe改变通道顺序的方法
2018/08/04 Python
Matplotlib配色之Colormap详解
2021/01/05 Python
python 利用openpyxl读取Excel表格中指定的行或列教程
2021/02/06 Python
HTML5之SVG 2D入门10—滤镜的定义及使用
2013/01/30 HTML / CSS
《望庐山瀑布》教学反思
2014/04/22 职场文书
网上祭先烈心得体会
2014/09/01 职场文书
基于nginx实现上游服务器动态自动上下线无需reload的实现方法
2021/03/31 Servers
PhpSpreadsheet中文文档 | Spreadsheet操作教程实例
2021/04/01 PHP
Spring Cloud Gateway去掉url前缀
2021/07/15 Java/Android