python自动化之Ansible的安装教程

本文实例讲述了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程序设计有所帮助。

(0)

相关推荐

  • CentOS 6.10源码编译及使用ansible编译安装httpd2.4.39

    一.编译安装 编译环境准备 主机 系统 A centos6.10 编译所需的httpd.apr.apr-util apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.39.tar.gz 1.安装编译所需要的软件 yum install gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel vim lrzsz tree s

  • 集群运维自动化工具ansible的安装与使用(包括模块与playbook使用)第1/2页

    我使用过puppet与salt,但这2个软件都需要安装客户端,并且更新很快,每次更新都是令人蛋疼的事,尤其是salt,喜欢他的命令功能,但bug太多,不敢在公司线上使用,puppet虽然稳定,但弄命令执行的时候,需要mco配置,非常麻烦,我公司由于跟多家公司合作,很多业务没办法安装客户端,所以没办法使用puppet与salt(虽然salt有ssh,但不太好使),最后找到了ansible,他既有命令执行也有配置管理,关键开发它的语言是python,paramiko进行ssh连接,跟我之前开发的自动

  • 集群运维自动化工具ansible使用playbook安装mysql

    上次介绍了如何使用ansible playbook安装zabbix客户端(http://www.jb51.net/article/52158.htm),这次介绍一下如何使用playbook安装mysql. 下面是安装mysql的信息: mysql_basedir: /data/mysql/basedir 源码目录 mysql_datadir: /data/mysql/datadir 数据目录 mysql_user: mysql mysql用户 mysql_database_user: root

  • Python利用ansible分发处理任务

    其实对python熟悉的人都可以自己用paramiko来写任务的分发系统,再结合gevent的协程就能实现异步的处理. 如果只想用工具的朋友可以使用一些工具,类似{puppet,saltstack,fabric,ansible,chef}等,其实这些工具的都是很好用的,不过于学习的成本,我建议大家使用ansible,这个模块封装的不错,功能也很齐全. 我们首先先安装ansible把 复制代码 代码如下: pip install ansible                          

  • ansible作为python模块库使用的方法实例

    前言 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配置.批量程序部署.批量运行命令等功能.ansible是基于模块工作的,本身没有批量部署的能力.真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架. 主要包括: (1).连接插件connection plugins:负责和被监控端实现通信: (2).host inventory:指定操作的主机,

  • python自动化测试实例解析

    本文实例讲述了python自动化测试的过程,分享给大家供大家参考. 具体代码如下: import unittest ######################################################################## class RomanNumeralConverter(object): """converter the Roman Number""" #-----------------------

  • Centos下安装Ansible的示例代码

    ansible ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配置.批量程序部署.批量运行命令等功能. ansible是基于模块工作的,本身没有批量部署的能力.真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架.主要包括: 连接插件connection plugins:负责和被监控端实现通信: host inventory:指定操作的主机,是一个

  • python ansible服务及剧本编写

    第1章 ansible软件概念说明 python语言是运维人员必会的语言,而ansible是一个基于Python开发的自动化运维工具 (saltstack).其功能实现基于SSH远程连接服务:ansible可以实现批量系统配置.批量软件部署.批量文件拷贝.批量运行命令等功能 ansible软件相关参考链接信息: http://docs.ansible.com/ansible/intro_installation.html http://www.ansible.com.cn/ http://doc

  • 集群运维自动化工具ansible之使用playbook安装zabbix客户端

    之前介绍了关于ansible的安装与使用(包括模块与playbook使用,地址是http://www.jb51.net/article/52154.htm),今天介绍一下如何使用playbook来部署zabbix客户端. ansible服务端的环境为centos 6.5 x86_64系统 ansible客户端环境为centos 6.3 x86_64系统 目前我的playbook只允许centos或redhat 6系列系统来安装zabbix客户端,并且客户端的版本是2.0.6. 下面是playbo

  • python将ansible配置转为json格式实例代码

    python将ansible配置转为json格式实例代码 ansible的配置文件举例如下,这种配置文件不利于在前端的展现,因此,我们用一段简单的代码将ansible的配置文件转为json格式的: [webserver] 192.168.204.70 192.168.204.71 [dbserver] 192.168.204.72 192.168.204.73 192.168.204.75 [proxy] 192.168.204.76 192.168.204.77 192.168.204.78

  • Python集中化管理平台Ansible介绍与YAML简介

    本文实例讲述了Python集中化管理平台Ansible介绍与YAML.分享给大家供大家参考,具体如下: 一 中文社区 http://ansible.cn/forum.php 二 点睛 Ansible(http://www.ansibleworks.com/)一种集成IT系统的配置管理.应用部署.执行特定任务的开源平台,是AnsibleWorks公司名下的项目,该公司由Cobbler及Func的作者于2012年创建成立.Ansible基于Python语言实现,由Paramiko和PyYAML两个关

随机推荐