一、Ansible及依赖包安装
1.python2.7安装
https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz
# tar xvzf Python-2.7.8.tgz
# cd Python-2.7.8
# ./configure --prefix=/usr/local
# make --jobs=grep processor/proc/cpuinfo | wc -l
# make install
# cd /usr/local/include/python2.7
# cp -a ./* /usr/local/include/
备份旧版本的python,并符号链接新版本的python
# cd /usr/bin
# mv python python2.6
# ln -s /usr/local/bin/python
## 修改yum脚本,使其指向旧版本的python,已避免其无法运行
# vim /usr/bin/yum
#!/usr/bin/python --> #!/usr/bin/python2.6
2.setuptools模块安装
https://pypi.python.org/packages/source/s/setuptools/setuptools-7.0.tar.gz
# tar xvzf setuptools-7.0.tar.gz
# cd setuptools-7.0
# python setup.py install
3.pycrypto模块安装
https://pypi.python.org/packages/source/p/pycrypto/pycrypto-2.6.1.tar.gz
# tar xvzf pycrypto-2.6.1.tar.gz
# cd pycrypto-2.6.1
# python setup.py install
4.PyYAML模块安装
http://pyyaml.org/download/libyaml/yaml-0.1.5.tar.gz
# tar xvzf yaml-0.1.5.tar.gz
# cd yaml-0.1.5
# ./configure --prefix=/usr/local
# make --jobs=`grep processor /proc/cpuinfo | wc -l`
# make install
https://pypi.python.org/packages/source/P/PyYAML/PyYAML-3.11.tar.gz
# tar xvzf PyYAML-3.11.tar.gz
# cd PyYAML-3.11
# python setup.py install
5.Jinja2模块安装
https://pypi.python.org/packages/source/M/MarkupSafe/MarkupSafe-0.9.3.tar.gz
# tar xvzf MarkupSafe-0.9.3.tar.gz
# cd MarkupSafe-0.9.3
# python setup.py install
https://pypi.python.org/packages/source/J/Jinja2/Jinja2-2.7.3.tar.gz
# tar xvzf Jinja2-2.7.3.tar.gz
# cd Jinja2-2.7.3
# python setup.py install
6.paramiko模块安装
https://pypi.python.org/packages/source/e/ecdsa/ecdsa-0.11.tar.gz
# tar xvzf ecdsa-0.11.tar.gz
# cd ecdsa-0.11
# python setup.py install
https://pypi.python.org/packages/source/p/paramiko/paramiko-1.15.1.tar.gz
# tar xvzf paramiko-1.15.1.tar.gz
# cd paramiko-1.15.1
# python setup.py install
7.simplejson模块安装
https://pypi.python.org/packages/source/s/simplejson/simplejson-3.6.5.tar.gz
# tar xvzf simplejson-3.6.5.tar.gz
# cd simplejson-3.6.5
# python setup.py install
8.ansible安装
https://github.com/ansible/ansible/archive/v1.7.2.tar.gz
# tar xvzf ansible-1.7.2.tar.gz
# cd ansible-1.7.2
# python setup.py install
二、Ansible配置
1.配置SSH远程登录功能
从未使用过SSH功能的机器应当先运行如下命令,一路回车:
#ssh-keygen
接下来执行:
# ssh-keygen -t rsa -P ''
将id_rsa_test拷贝到目标节点机器上去,执行以下命令:
# cat /root/.ssh/id_rsa_test.pub >> /root/.ssh/authorized_keys
# chmod 600 /root/.ssh/authorized_keys
2.ansible配置
回到主机,执行以下命令:
# mkdir -p /etc/ansible
##进入ansible安装包,将example文件夹下的ansible.cfg拷贝到新建的文件夹下
# cp examples/ansible.cfg /etc/ansible/
# vim /etc/ansible/ansible.cfg
在文件末尾加上以下内容
remote_port = 36000
private_key_file = /root/.ssh/id_rsa_test
接下来定义目标节点机器:
# vim /etc/ansible/hosts
在文件中加入目标节点机器地址
123.123.123.123
111.111.111.111
222.222.222.222
到此为止,ansible的安装配置已经完成
三、ansible功能测试
1.ping所有目标机器
# ansible all -m ping
2.将本地文件复制到远程服务器
#ansible all -m copy -a "src=/home/test/test.sh dest=/home/target/target.sh owner=root group=root mode=0755"
3.远程执行脚本
# ansible all -m shell -a "/home/target/target.sh"