需求描述:
管理具有特征性的集群服務器,50台左右,服務都是規划好的!為了更加有效地管理服務器,需要引入協助管理員關系的工具!ansible基於ssh通信不需要安裝agent(agentless),使用簡單!
有需求,動力就會誕生!為了更好地服務自己及他人,分享知識中的點點滴滴!
搭建環境:
Linux: Centos6.5x64
python:python2.6+
ansible: 2.6.3
相關資源:
libsodium-1.0.16.tar.gz
https://pan.baidu.com/s/10nwgFipRbxF5yoDpYiqGtg 密碼: 9w9i
ansible2.6.3
https://pan.baidu.com/s/1ZwzdkuhVvjhzgp9gbJZLTw 密碼: 4mjq
01、下載ansible
https://releases.ansible.com/ansible/ansible-2.6.3.tar.gz //發行
https://releases.ansible.com/ansible/rpm/release/epel-6-x86_64/ansible-2.6.3-1.el6.ans.noarch.rpm //rpm
02、下載依賴
https://pypi.org/ //官方庫根據名字自己搜索,注意對python版本的依賴
https://pan.baidu.com/s/1ZwzdkuhVvjhzgp9gbJZLTw 密碼: 4mjq //已經下載好的!
03、安裝依賴
#yum安裝
yum install -y python python-setuptools gcc gcc-c++ python-devel openssl-devel libffi-devel
#源碼安裝libsodium
https://github.com/jedisct1/libsodium/releases/download/1.0.16/libsodium-1.0.16.tar.gz
./configure
make
make install
/etc/ld.so.conf.d/libsodium.conf //添加到系統環境動態庫
/usr/local/lib/
ldconfig
ldconfig -v |grep local //查看是否加載
#腳本自動安裝
#!/bin/bash #desc: auto setup ansible #centos6.5+python2.6+ansible2.6.x #解壓 ls *.tar.gz |xargs -n1 tar zxf cd MarkupSafe* python setup.py install cd .. cd Jinja2* python setup.py install cd .. cd PyYAML* python setup.py install cd .. cd pyasn1* python setup.py install cd .. cd pycparser* python setup.py install cd .. cd cffi* python setup.py install cd .. cd six* python setup.py install cd .. cd PyNaCl* python setup.py install cd .. cd ipaddress* python setup.py install cd .. cd enum34* python setup.py install cd .. cd asn1crypto* python setup.py install cd .. cd idna* python setup.py install cd .. cd cryptography* python setup.py install cd .. cd bcrypt* python setup.py install cd .. cd ordereddict* python setup.py install cd ..
#安裝parmiko cd paramiko* python setup.py install cd ..
#安裝ansible cd ansible* python setup.py install cd .. echo "all is ok"
注意:注意安裝中的報錯要處理下!我測試沒有問題的。。。
04、配置文件
find / -name ansible.cfg 2>/dev/null //查找ansible.cfg主配置
mkdir -p /etc/ansible/
cp /root/ansible-2.6.3/examples/ansible.cfg /etc/ansible
host_key_checking = False //對ansible.cfg的knows檢測取消
05、測試
#測試ssh主機是否可用
[root@lab-110 ansible]# ansible local -m ping -k //-k 對root密碼驗證 SSH password: 172.24.0.110 | SUCCESS => { "changed": false, "ping": "pong" }
#免密碼通訊
ssh-keygen -t rsa //在control機器上生成密鑰對 id_rsa 私鑰 id_rsa.pub 公鑰
利用ansible的authorized_key模塊對遠程主機分發公鑰實現免密碼登錄
[root@lab-110 ansible]# ansible local -m authorized_key -a "user=root key='{{ lookup('file','/root/.ssh/id_rsa.pub') }} '" -kSSH password: 172.24.0.110 | SUCCESS => { "changed": true, "comment": null, "exclusive": false, "key": "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAs81XUJHkBhwoAKU62ngLiGrR9yhLLAPPkNbnMYLnpwXXAqQFv8wcuZw4Q6C17bnHW+77XAk /TOyWJhZ9yHEjs80louqtZlf4s/t2wwLjCTYLLVnxPcS0KgwSvRnah+w9z0wAy0VU5QwNH4W3ukUnVCHTVI8FhWwm8tssTD+APJ1HMeum/EATIa5eNw8TEtYFOYTKtDbnXQe7BWFKrblwALQwLxaaEASFLAVv5V5BOVhFLxCIi969pQ9G46ij9jyLo7Md8Zm1ggS3zQZL9oH5WVP5pyDEjXHTCopEgp3VIirNfDRI+RDU98+BlLk8T65Z9QFM8Kf0kHw928BETmiEiw== root@lab-110", "key_options": null, "keyfile": "/root/.ssh/authorized_keys", "manage_dir": true, "path": null, "state": "present", "unique": false, "user": "root", "validate_certs": true } [root@lab-110 ansible]# ansible all -m ping 172.24.0.110 | SUCCESS => { "changed": false, "ping": "pong" }