@cdmonkey
2018-12-14T07:16:17.000000Z
字数 6770
阅读 2643
Ansible
| logo | |
|---|---|
![]() |
Git
https://github.com/ansible
教程:
http://www.ywnds.com/?p=6045
https://www.jianshu.com/p/c56a88b103f8
http://ansible-tran.readthedocs.io/en/latest/docs/playbooks_acceleration.html
http://chuansong.me/n/392342051853
https://www.jianshu.com/p/f751785bc71c
http://sate-z.github.io/2016/12/15/Ansible%E5%AE%89%E8%A3%85
https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html
https://colinleefish.gitbooks.io/ansible-docs-simplified-chinese/content/introduction/installation.html
# Operating system version:[root@cd-test ~]# cat /etc/redhat-releaseCentOS release 6.7 (Final)
为避免后续安装报错,还需要安装一些依赖包:
yum install gcc libffi-devel python-devel openssl-devel zlib-devel bzip2-devel ncurses-devel# 还需要安装:yum install sshpass
替换 python 版本:
mv /usr/bin/python /usr/bin/python2.6.6ln -s /usr/local/python3/bin/python3.6 /usr/bin/pythonecho "/usr/local/lib" >>/etc/ld.so.confecho "/usr/local/python3/lib" >> /etc/ld.so.confldconfig -vsed -i 's@#!/usr/bin/python@#!/usr/bin/python2.6.6@' /usr/bin/yumsed -i 's@#! /usr/bin/python@#! /usr/bin/python2.6.6@' /usr/libexec/urlgrabber-ext-down
[root@cd-test ~]# easy_install pip# Update pip:[root@cd-test ~]# pip install --upgrade pip# Then install the Ansible controller host need to use the Python module:[root@cd-test ~]# pip install paramiko PyYAML Jinja2 httplib2 six# 还需要安装:pip install cffipip install cryptography
如果是 python3.6 请执行下面的指令,而不是上面这些。
/usr/local/python3/bin/pip3 install paramiko PyYAML Jinja2 httplib2 six/usr/local/python3/bin/pip3 install cffi/usr/local/python3/bin/pip3 install cryptography
这里可能会有问题,就是无法使用 pip3 指令进行安装:
# 就是什么模块也安装不了。[root@PBSNGX03 python3]# /usr/local/python3/bin/pip3 install cffiCollecting cffiCould not find a version that satisfies the requirement cffi (from versions: )No matching distribution found for cffi
目前发现就是安装源配置有问题,需要重新配置:
https://opsx.alibaba.com/mirror
[root@PBSNGX03 ~]# vim .pip/pip.conf[global]index-url = http://mirrors.aliyun.com/pypi/simple/[install]trusted-host = mirrors.aliyun.com# 上述配置是将安装源更换为阿里。# 注意,阿里官方配置帮助是使用 https,但是会有关于 SSL 之报错内容,因而这里可以使用 http,实际检测没啥问题。
这里需要注意安装位置,最好把克隆下来的目录放置于合适位置,而不是放置于 /root 下面。
[root@cd-test ~]# git clone git://github.com/ansible/ansible.git --recursive[root@cd-test ~]# cd ansible/[root@cd-test ansible]# source ./hacking/env-setup# 输出信息如下:...Ansible now needs setuptools in order to build. Install it using your package manager (usually python-setuptools) or via pip (pip install setuptools).Setting up Ansible to run out of checkout...PATH=/root/ansible/bin:/root/ansible/test/runner:/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/binPYTHONPATH=/root/ansible/lib:MANPATH=/root/ansible/docs/man:Remember, you may wish to specify your host file with -iDone!# 如果要减少安装过程中之告警及错误信息输出,可使用:[root@cd-test ansible]# source ./hacking/env-setup -q# 安装,这不还不明白是什么意思?[root@cd-test ansible]# make install
[root@ELK-Kibana-01 ~]# vim .bash_profile# 配置 PATH 变量:PATH=$PATH:$HOME/bin:/opt/ansible/bin
似乎还可使用 setup.py 这个文件进行安装,但目前为止还没有用过。
查看版本信息:
ansible 2.5.0 (devel 276f7122cd) last updated 2017/12/21 14:42:42 (GMT +800)config file = /root/ansible/examples/ansible.cfgconfigured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']ansible python module location = /root/ansible/lib/ansibleexecutable location = /root/ansible/bin/ansiblepython version = 3.6.0 ...
可是配置文件路径并不是我们需要的。配置文件可能处于不同位置,但只有一个可用。下面列表中,ansible 从上往下依次检查,检查到哪个可用就用哪个:
ANSIBLE_CFG # 优先级最高的是这个环境变量,可以定义配置文件的位置;./ansible.cfg # 其次是当前工作目录(执行 ansibe 指令时的目录)下的配置文件;~/ansible.cfg # 再次是当前用户家目录中的配置文件;/etc/ansible/ansible.cfg # 最后是这个配置文件;
[root@cd-test ~]# vim .bash_profile# 于最下面追加如下内容:export ANSIBLE_CFG="/etc/ansible/ansible.cfg"# 配置生效:source .bash_profile
使用 Git 指令进行安装会要求升级 OpenSSH,比较头疼。因而还可使用 yum 进行安装。
安装包下载:
http://releases.ansible.com/ansible/rpm/release/epel-6-x86_64/
[root@PBSNGX03 tools]# yum localinstall ansible-2.5.2-1.el6.ans.noarch.rpm
查看版本信息:
# Check version:[root@PBSNGX03 tools]# ansible --versionansible 2.5.2config file = /etc/ansible/ansible.cfgconfigured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']ansible python module location = /usr/lib/python2.6/site-packages/ansibleexecutable location = /usr/bin/ansiblepython version = 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]
很明显,Python 版本有问题,需要更换。至于怎样更换,目前不会~
[root@Ansible01 tools]# tar zxvf ansible-2.6.0.tar.gz -C /opt/[root@Ansible01 tools]# cd /opt/ansible-2.6.0/python setup.py install
可创建个软链接:
[root@Ansible01 ~]# cd /opt/[root@Ansible01 opt]# ln -s ansible-2.6.0 ansible
http://www.jianshu.com/p/07f51970fdde
https://www.cnblogs.com/LuisYang/p/5960660.html
https://micorochio.github.io/2017/05/31/ansible-learning-01
如果是通过 make install 进行安装,则没有配置文件及其目录,需要手动创建。
[root@cd-test ~]# mkdir -p /etc/ansible/[root@cd-test ~]# cd ansible/examples/[root@cd-test examples]# cp ansible.cfg /etc/ansible/ # 是 ansible 全局配置文件[root@cd-test examples]# cp hosts /etc/ansible/ # 是 ansible 管理的主机列表,及部分参数。
使用
ansible批量部署服务的前提是建立免秘钥登录。
配置免密登录:
ssh-keygen -t rsa # 生成密钥对cat .ssh/id_rsa.pub >>.ssh/authorized_keys # 将公钥导入本机chmod 600 .ssh/authorized_keys # 安全起见,设置权限为600ssh-copy-id -i .ssh/id_rsa.pub root@192.168.2.12 # 将该公钥导入其他主机
服务器较多时,逐个手动使用 ssh-copy-id 指令进行免密钥登录配置也是不小的工作量。怎样批量建立免密钥登录呢?
https://segmentfault.com/a/1190000009832597
上面就是简单配置了 SSH 免密之密钥登录法。可是这样有一个安全问题:假如 Ansible 控制端出问题了,甚至被入侵,则全部节点都具有风险。因而出于安全考虑,建议使用带密码之密钥进行登录。配置如下:
mkdir /etc/ansible/ssh_keysssh-keygen -t rsa -f /etc/ansible/ssh_keys/node2.key # 注意要给私钥设密码chmod 700 /etc/ansible/ssh_keys/node2.key# 将/etc/ansible/ssh_keys/node2.key.pub内容追加到远程主机的authorized_keys文件里面。
然后编辑清单文件:
[node2]192.168.2.12 ansible_ssh_private_key_file=/etc/ansible/ssh_keys/node2.key
这样一来,每次远程管理时,会提示输入私钥的密码才能连接服务器。
[root@PBSNGX03 ~]# easy_install pip...pkg_resources.DistributionNotFound: The 'distribute==0.6.10' distribution was not found and is required by the application
需要重新安装 distribute。首先下载软件包:
https://pypi.python.org/packages/source/d/distribute/distribute-0.7.3.zip
[root@PBSNGX03 tools]# unzip distribute-0.7.3.zip[root@PBSNGX03 tools]# cd distribute-0.7.3python2.6 setup.py install# 特别需要注意 Python 版本。
接下来使用 pip 安装模块时还有报错:
SyntaxError: invalid syntax
目前看来是 pip 版本过高,不适用于 Python2.6,需要安装 pip9.0.1 这个版本。
# 移除之前版本的 pip:[root@PBSNGX03 ~]# cd /usr/lib/python2.6/site-packages/[root@PBSNGX03 site-packages]# rm -rf pip*# 重新进行安装:[root@PBSNGX03 tools]# tar zxvf pip-9.0.1.tar.gz[root@PBSNGX03 tools]# cd pip-9.0.1[root@PBSNGX03 pip-9.0.1]# python2.6 setup.py install# 查看版本信息:[root@PBSNGX03 pip-9.0.1]# pip -Vpip 9.0.1 from /usr/lib/python2.6/site-packages/pip-9.0.1-py2.6.egg (python 2.6)
而后就能够正常使用了,安装时最好禁用 pip 版本检查,否则老是提醒你进行升级,但是由于 Python2.6 这个版本过低,实际上根本无法升级。
pip install --disable-pip-version-check ...
[root@cd-test02 ansible]# make installTraceback (most recent call last):File "packaging/release/versionhelper/version_helper.py", line 9, in <module>from packaging.version import Version, VERSION_PATTERNImportError: No module named packaging.versionMakefile:40: *** "version_helper failed". Stop.
最后稀里糊涂就解决了。
wget https://bootstrap.pypa.io/get-pip.pypython get-pip.py
还是不行,最后执行了下面这个指令解决了。我估计上面那个不用执行。
/usr/local/python3/bin/pip3 install packaging