安装python(最低2.6v)
(1)、python2.7安装
wget 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
将python头文件拷贝到标准目录,以避免编译ansible时,找不到所需的头文件
# 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模块安装
wget 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模块安装
wget 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模块安装
wget 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
wget 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模块安装
wget 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
wget 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模块安装
wget 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
wget 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模块安装
wget 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
安装pip
wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb" --no-check-certificate
tar -xzvf pip-1.5.4.tar.gz
cd pip-1.5.4
python setup.py install
如果安装报下面的错:
Traceback (most recent call last):
File "setup.py", line 6, in <module>
from setuptools import setup, find_packages
ImportError: No module named setuptools
那么就要先安装setuptools包
wget http://pypi.python.org/packages/source/s/setuptools/setuptools-2.0.tar.gz
tar zxvf setuptools-2.0.tar.gz
cd setuptools-2.0
python setup.py build
python setup.py install
安装完成setuptools包后,在重新执行:
cd /home/xiaohe/tools/pip-1.5.4
python setup.py install
通过pip安装ansible
pip install ansible
准备 Inventory用来定义你要管理的主机
mkdir /etc/ansible
vim /etc/ansible/hosts
[local]
192.168.100.52
此外,也可以通过数字和字母模式来指定一系列连续主机,如:
[1:3].linuxtoy.org # 等价于
1.linuxtoy.org、2.linuxtoy.org、3.linuxtoy.org
[a:c].linuxtoy.org # 等价于
a.linuxtoy.org、b.linuxtoy.org、c.linuxtoy.org
创建密钥并推送
ssh-keygen
ssh-copy-id -i 192.168.100.52
现在,我们执行以下命令来看看 Ansible 是否能正常工作:
ansible -i hosts all -m ping -u www
发现是失败的,提示我们必须用sshpass
ansible local -u root -k -m ping
结果:
192.168.100.52 | FAILED! => {
"failed": true,
"msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"
}
下载
curl -O -L http://downloads.sourceforge.net/project/sshpass/sshpass/1.05/sshpass-1.05.tar.gz
解压
tar xvzf sshpass-1.05.tar.gz
进入
cd sshpass-1.05
编译安装
./configure && make && make install
再次执行
ansible local -u root -k -m ping
结果:
192.168.100.52 | SUCCESS => {
"changed": false,
"ping": "pong"
}
在各个节点输出命令
ansible all -a "/bin/echo hello, world"
复制本地文件到远程主机上(指定用户,组,和权限)
ansible 192.168.100.63 -m copy -a "src=/etc/ansible/hosts dest=/tmp/hosts owner=root group=root mode=0644"
注:
大家可以用 "ansible-doc -l" 命令来查看它内置的有哪些模块。
然后利用"ansible-doc 模块名" 命令来查看具体模块的详细用法。