溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python自動(dòng)化之Ansible的安裝教程

發(fā)布時(shí)間:2020-10-23 05:16:30 來(lái)源:腳本之家 閱讀:148 作者:cakincqm 欄目:開發(fā)技術(shù)

本文實(shí)例講述了python自動(dòng)化之Ansible的安裝。分享給大家供大家參考,具體如下:

一 點(diǎn)睛

Ansible只需在管理端部署環(huán)境即可,建議采用yum源方式來(lái)實(shí)現(xiàn)部署。

二 安裝Ansible

只需要在主服務(wù)器安裝(主控端)

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

三 測(cè)試

1 修改在主控機(jī)配置文件/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 執(zhí)行下面操作

通過(guò)ping模塊測(cè)試主機(jī)的連通性,分別對(duì)單主機(jī)及組進(jìn)行ping操 作,結(jié)果如下,說(shuō)明安裝、測(cè)試成功。

[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 說(shuō)明

由于主控端與被控主機(jī)未配置SSH證書信任,需要在執(zhí)行ansible命令時(shí)添加-k參數(shù),要求提供root(默認(rèn))賬號(hào)密碼,即在提示“SSH password:”時(shí)輸入。

四 配置Linux主機(jī)SSH無(wú)密碼訪問

1 點(diǎn)睛

為了避免Ansible下發(fā)指令時(shí)輸入目標(biāo)主機(jī)密碼,通過(guò)證書簽名達(dá)到SSH無(wú)密碼是一個(gè)好的方案,推薦使用ssh-keygen與ssh-copy-id來(lái)實(shí)現(xiàn)快速證書的生成及公鑰下發(fā),其中ssh-keygen生成一對(duì)密鑰,使用sshcopy-id來(lái)下發(fā)生成的公鑰。

第一步:需要配置與目標(biāo)設(shè)備的密鑰認(rèn)證支持。

[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]-----+

私鑰文件可以存放在默認(rèn)路徑“~/.ssh/id_rsa”。

第二步:接下來(lái)同步公鑰文件id_rsa.pub到目標(biāo)主機(jī),推薦使用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.

更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI