centos6.5-7编译安装Ansible详细部署

一、基础介绍
==========================================================================================
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:
(1)、连接插件connection plugins:负责和被监控端实现通信
(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机
(3)、各种模块核心模块、command模块、自定义模块
(4)、借助于插件完成记录日志邮件等功能;
(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务

1、Ansible基础安装
(1)、python2.7安装 #目前还不支持3X的版本,你也可以用

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 && make install

将python头文件拷贝到标准目录,以避免编译ansible时,找不到所需的头文件

cd /usr/local/include/python2.7
cp -a ./* /usr/local/include/

备份旧版本的python,并符号链接新版本的python

cd /usr/bin
mv python python.old
ln -s /usr/local/bin/python .

修改yum脚本,使其指向旧版本的python,已避免其无法运行

centos6修改如下
vim /usr/bin/yum 修改为 #!/usr/bin/python.old
centos7 增加修改vim /usr/libexec/urlgrabber-ext-down
修改为
#!/usr/bin/python.old

(2)、setuptools模块安装

https://files.pythonhosted.org/packages/1a/04/d6f1159feaccdfc508517dba1929eb93a2854de729fa68da9d5c6b48fa00/setuptools-39.2.0.zip
unzip setuptools-39.2.0
cd setuptools-39.2.0
python setup.py install

(3)、pycrypto模块安装

https://files.pythonhosted.org/packages/60/db/645aa9af249f059cc3a368b118de33889219e0362141e75d4eaf6f80f163/pycrypto-2.6.1.tar.gz

tar xvzf pycrypto-2.6.1.tar.gz
cd pycrypto-2.6.1
python setup.py install

(4)、YAML模块安装

http://pyyaml.org/download/libyaml/yaml-0.1.7.tar.gz

tar xvzf yaml-0.1.7.tar.gz
cd yaml-0.1.7
./configure --prefix=/usr/local
make && make install

(5)、PyYAML模块安装

https://files.pythonhosted.org/packages/4a/85/db5a2df477072b2902b0eb892feb37d88ac635d36245a72a6a69b23b383a/PyYAML-3.12.tar.gz

tar zxvf PyYAML-3.12.tar.gz
cd PyYAML-3.12
python setup.py install

(6)、MarkupSafe模块安装

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.8.tar.gz

tar xvzf Jinja2-2.7.3.tar.gz
cd Jinja2-2.7.3
python setup.py install

(7)、simplejson模块安装

https://files.pythonhosted.org/packages/4d/de/32d741db316d8fdb7680822dd37001ef7a448255de9699ab4bfcbdf4172b/MarkupSafe-1.0.tar.gz

tar xvzf MarkupSafe-1.0.tar.gz
cd MarkupSafe-1.0
python setup.py install

(8)、Jinja2模块安装

https://files.pythonhosted.org/packages/56/e6/332789f295cf22308386cf5bbd1f4e00ed11484299c5d7383378cf48ba47/Jinja2-2.10.tar.gz

tar xvzf Jinja2-2.10.tar.gz
cd Jinja2-2.10
python setup.py install

(9)simplejson模块安装

https://files.pythonhosted.org/packages/8b/6c/c512c32124d1d2d67a32ff867bb3cdd5bfa6432660975f7ee753ed7ad886/simplejson-3.15.0.tar.gz

tar xvzf simplejson-3.15.0.tar.gz
cd simplejson-3.15.0
python setup.py install

(10)httplib2安装

https://files.pythonhosted.org/packages/fd/ce/aa4a385e3e9fd351737fd2b07edaa56e7a730448465aceda6b35086a0d9b/httplib2-0.11.3.tar.gz

tar zxvf httplib2-0.11.3.tar.gz
cd httplib2-0.11.3
python setup.py install

(11)libffi安装

http://www.mirrorservice.org/sites/sourceware.org/pub/libffi/libffi-3.2.1.tar.gz

tar zxvf libffi-3.2.1.tar.gz
cd libffi-3.2.1
./configure --prefix=/usr/local
make
make install


ldconfig

 

 (12)six安装

https://files.pythonhosted.org/packages/16/d8/bc6316cf98419719bd59c91742194c111b6f2e85abac88e496adefaf7afe/six-1.11.0.tar.gz

tar zxvf six-1.11.0.tar.gz
cd six-1.11.0
python setup.py install

(13)cffi安装

https://files.pythonhosted.org/packages/e7/a7/4cd50e57cc6f436f1cc3a7e8fa700ff9b8b4d471620629074913e3735fb2/cffi-1.11.5.tar.gz

tar zxvf cffi-1.11.5.tar.gz
cd cffi-1.11.5
python setup.py install

(14)cryptography安装

https://files.pythonhosted.org/packages/ec/b2/faa78c1ab928d2b2c634c8b41ff1181f0abdd9adf9193211bd606ffa57e2/cryptography-2.2.2.tar.gz

tar zxvf cryptography-2.2.2.tar.gz
cd cryptography-2.2.2
python setup.py install

(15)pyOpenSSL安装

https://files.pythonhosted.org/packages/9b/7c/ee600b2a9304d260d96044ab5c5e57aa489755b92bbeb4c0803f9504f480/pyOpenSSL-18.0.0.tar.gz

tar zxvf pyOpenSSL-18.0.0.tar.gz
cd pyOpenSSL-18.0.0
python setup.py install

(16)pycparser安装

https://files.pythonhosted.org/packages/8c/2d/aad7f16146f4197a11f8e91fb81df177adcc2073d36a17b1491fd09df6ed/pycparser-2.18.tar.gz

tar zxvf pycparser-2.18.tar.gz
cd pycparser-2.18
python setup.py install

(17)paramiko安装

https://files.pythonhosted.org/packages/29/65/83181630befb17cd1370a6abb9a87957947a43c2332216e5975353f61d64/paramiko-2.4.1.tar.gz

tar zxvf paramiko-2.4.1.tar.gz
cd paramiko-2.4.1
python setup.py install

(18)passlib安装

https://files.pythonhosted.org/packages/25/4b/6fbfc66aabb3017cd8c3bd97b37f769d7503ead2899bf76e570eb91270de/passlib-1.7.1.tar.gz

tar zxvf passlib-1.7.1.tar.gz
cd passlib-1.7.1
python setup.py install

(19)、ansible安装

https://github.com/ansible/ansible/archive/
https://pypi.org/project/ansible/

https://files.pythonhosted.org/packages/6e/95/490f5e39ee7cc7956eecd070610f0a873b97781c9efdbf6098bad2ed3ee0/ansible-2.5.4.tar.gz

tar zxvf ansible-2.5.4.tar.gz
cd ansible-2.5.4
python setup.py install

2、Ansible配置
(1)、SSH免密钥登录设置
分别在各个机器上执行SSH无密码验证登录的实现

vim /etc/ssh/sshd_config 开启
    RSAAuthentication yes # 启用 RSA 认证
    PubkeyAuthentication yes # 启用公钥私钥配对认证方式
    AuthorizedKeysFile .ssh/authorized_keys # 公钥文件路径

ssh-keygen -t rsa 生成密钥文件
cd ~/.ssh/
cat id_rsa.pub >> authorized_keys
chmod 600 authorized_keys

ssh-copy-id -i ~/.ssh/id_rsa.pub IP

重启SSH服务: /bin/systemctl restart sshd.service
/etc/init.d/sshd restart

将所有机器的key都在加在各自的authorized_keys里。

重启SSH服务:

 /bin/systemctl restart sshd.service

(2)、ansible配置

mkdir -p /etc/ansible
vim /etc/ansible/ansible.cfg

inventory      = /etc/ansible/hosts
remote_port    = 22

主机组定义

vim /etc/ansible/hosts

[web59]
192.168.1.59

(3)、简单测试

ansible all -m command -a 'uptime'   
           
192.168.1.229 | SUCCESS | rc=0 >>
 13:00:08 up 5 days, 18:58,  2 users,  load average: 0.01, 0.03, 0.00

192.168.1.59 | SUCCESS | rc=0 >>
 13:03:29 up 5 days, 19:20,  5 users,  load average: 0.00, 0.00, 0.00

 

posted @ 2016-03-10 13:50  love19791125  阅读(1297)  评论(0编辑  收藏  举报