您好,登錄后才能下訂單哦!
本文實例講述了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程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。