系统环境
[root@test-76 ansible]# cat /etc/centos-release
CentOS release 6.7 (Final)
python版本
[root@test-76 ansible]# python -V
Python 2.6.6
pip安装
[root@test76~]# wget https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb" --no-check-certificate
[root@test76~]# tar zxvf pip-1.5.4.tar.gz
[root@test76~]#cd pip-1.5.4
[root@test76 pip-1.5.4]# python setup.py install
ansible安装
[root@test-76 ~]# pip install ansible
Downloading/unpacking ansible
Downloading ansible-2.4.1.0.tar.gz (6.7MB): 35% 2.4MB
...
使用前提:
ansible服务端可以免密登陆其他客户端
配置方法:
服务端运行 ssh-keygen ,将会生成密钥文件和私钥文件 id_rsa,id_rsa.pub 将 .pub 文件复制到客户端机器的 .ssh 目录, 并 cat id_dsa.pub >> ~/.ssh/authorized_keys
测试:
服务端 ssh root@客户端ip 免密登陆即为成功
接着
新建文件夹
[root@test-76 ~]# mkdir /etc/ansible
创建配置文件
[root@test-76 ansible]# touch /etc/ansible/ansible.cfg
[root@test-76 ~]# cd /etc/ansible/
创建hosts文件 添加客户端ip
[root@test-76 ansible]# vim hosts
192.168.x.x
运行
[root@test-76 ansible]# ansible all -m ping
报错如下
Traceback (most recent call last):
File "/usr/bin/ansible", line 40, in <module>
import ansible.constants as C
File "/usr/lib/python2.6/site-packages/ansible/constants.py", line 12, in <module>
from jinja2 import Template
File "/usr/lib64/python2.6/site-packages/jinja2/__init__.py", line 33, in <module>
from jinja2.environment import Environment, Template
File "/usr/lib64/python2.6/site-packages/jinja2/environment.py", line 15, in <module>
from jinja2 import nodes
File "/usr/lib64/python2.6/site-packages/jinja2/nodes.py", line 19, in <module>
from jinja2.utils import Markup
File "/usr/lib64/python2.6/site-packages/jinja2/utils.py", line 624, in <module>
from markupsafe import Markup, escape, soft_unicode
ImportError: No module named markupsafe
解决办法:
[root@test-76 ansible]# pip install markupsafe
重新运行
[root@test-76 ansible]# ansible all -m ping
192.168.1.167 | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}
192.168.1.31 | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}
192.168.1.62 | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}
192.168.1.39 | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}