Ansible自動化運維(一)安裝與配置
阿新 • • 發佈:2018-11-07
安裝Ansible
使用包管理工具安裝
因為RHEL、CentOS的官方yum源中沒有Ansible安裝包。需要安裝EPEL作為部署Ansible的預設yum源。
CentOS7版本:
rpm -Uvh http://mirrors.zju.edu.cn/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm
(映象源連結可能會有略微變化。此連結2018/10/19可用)
安裝好yum源後,使用以下命令安裝Ansible(root使用者可免去sudo)
# sudo yum install ansible
安裝好後檢視版本號驗證是否安裝成功
# ansible --version
配置SSH無密碼訪問
控制主機建立金鑰,輸入命令後一路回車完成建立
# ssh-keygen
將公鑰傳送到被管節點(例:被管節點IP為192.168.1.111)
# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
輸入ssh [email protected]
驗證是否成功
第一次使用,在被管節點上批量輸出‘Hello Ansible’
首先配置主機與組配置
檔案預設在 /etc/ansible/hosts
新增兩臺主機的IP
同時定義一個webservers組包含這兩個IP地址
192.168.1.111
192.168.1.112
[webservers]
192.168.1.111
192.168.1.112
測試:
對單機進行ping測試
ansible 192.168.1.111 -m ping
對webservers組進行ping操作
ansible webservers -m ping
在被管節點上批量執行命令
建立一個資源清單檔案inventory.cfg(root/inventory.cfg)
[webservers]
192.168.1.101
192.168.1.102
用Ansible的shell模組在webservers組的各伺服器上顯示"hello ansible!"
ansible webservers -m shell -a '/bin/echo hello ansible!' -i inventory.cfg
使用command模組得到同樣的效果
ansible webservers -m command -a '/bin/echo hello ansible!' -i inventory.cfg