[关闭]
@cdmonkey 2018-12-14T07:16:17.000000Z 字数 6770 阅读 1845

Ansible(1)安装及配置

Ansible


logo
a-logo.png-8.3kB                                                                                                                                                         

http://www.ansible.com.cn

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

  1. # Operating system version:
  2. [root@cd-test ~]# cat /etc/redhat-release
  3. CentOS release 6.7 (Final)

为避免后续安装报错,还需要安装一些依赖包:

  1. yum install gcc libffi-devel python-devel openssl-devel zlib-devel bzip2-devel ncurses-devel
  2. # 还需要安装:
  3. yum install sshpass

替换 python 版本:

  1. mv /usr/bin/python /usr/bin/python2.6.6
  2. ln -s /usr/local/python3/bin/python3.6 /usr/bin/python
  3. echo "/usr/local/lib" >>/etc/ld.so.conf
  4. echo "/usr/local/python3/lib" >> /etc/ld.so.conf
  5. ldconfig -v
  6. sed -i 's@#!/usr/bin/python@#!/usr/bin/python2.6.6@' /usr/bin/yum
  7. sed -i 's@#! /usr/bin/python@#! /usr/bin/python2.6.6@' /usr/libexec/urlgrabber-ext-down

Git

Install pip

  1. [root@cd-test ~]# easy_install pip
  2. # Update pip:
  3. [root@cd-test ~]# pip install --upgrade pip
  4. # Then install the Ansible controller host need to use the Python module:
  5. [root@cd-test ~]# pip install paramiko PyYAML Jinja2 httplib2 six
  6. # 还需要安装:
  7. pip install cffi
  8. pip install cryptography

如果是 python3.6 请执行下面的指令,而不是上面这些。

  1. /usr/local/python3/bin/pip3 install paramiko PyYAML Jinja2 httplib2 six
  2. /usr/local/python3/bin/pip3 install cffi
  3. /usr/local/python3/bin/pip3 install cryptography

这里可能会有问题,就是无法使用 pip3 指令进行安装:

  1. # 就是什么模块也安装不了。
  2. [root@PBSNGX03 python3]# /usr/local/python3/bin/pip3 install cffi
  3. Collecting cffi
  4. Could not find a version that satisfies the requirement cffi (from versions: )
  5. No matching distribution found for cffi

目前发现就是安装源配置有问题,需要重新配置:
https://opsx.alibaba.com/mirror

  1. [root@PBSNGX03 ~]# vim .pip/pip.conf
  2. [global]
  3. index-url = http://mirrors.aliyun.com/pypi/simple/
  4. [install]
  5. trusted-host = mirrors.aliyun.com
  6. # 上述配置是将安装源更换为阿里。
  7. # 注意,阿里官方配置帮助是使用 https,但是会有关于 SSL 之报错内容,因而这里可以使用 http,实际检测没啥问题。

Use Git for installation

这里需要注意安装位置,最好把克隆下来的目录放置于合适位置,而不是放置于 /root 下面。

  1. [root@cd-test ~]# git clone git://github.com/ansible/ansible.git --recursive
  2. [root@cd-test ~]# cd ansible/
  3. [root@cd-test ansible]# source ./hacking/env-setup
  4. # 输出信息如下:
  5. ...
  6. Ansible now needs setuptools in order to build. Install it using your package manager (usually python-setuptools) or via pip (pip install setuptools).
  7. Setting up Ansible to run out of checkout...
  8. PATH=/root/ansible/bin:/root/ansible/test/runner:/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
  9. PYTHONPATH=/root/ansible/lib:
  10. MANPATH=/root/ansible/docs/man:
  11. Remember, you may wish to specify your host file with -i
  12. Done!
  13. # 如果要减少安装过程中之告警及错误信息输出,可使用:
  14. [root@cd-test ansible]# source ./hacking/env-setup -q
  15. # 安装,这不还不明白是什么意思?
  16. [root@cd-test ansible]# make install
  1. [root@ELK-Kibana-01 ~]# vim .bash_profile
  2. # 配置 PATH 变量:
  3. PATH=$PATH:$HOME/bin:/opt/ansible/bin

似乎还可使用 setup.py 这个文件进行安装,但目前为止还没有用过。

查看版本信息:

  1. ansible 2.5.0 (devel 276f7122cd) last updated 2017/12/21 14:42:42 (GMT +800)
  2. config file = /root/ansible/examples/ansible.cfg
  3. configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  4. ansible python module location = /root/ansible/lib/ansible
  5. executable location = /root/ansible/bin/ansible
  6. python version = 3.6.0 ...

可是配置文件路径并不是我们需要的。配置文件可能处于不同位置,但只有一个可用。下面列表中,ansible 从上往下依次检查,检查到哪个可用就用哪个:

  1. ANSIBLE_CFG # 优先级最高的是这个环境变量,可以定义配置文件的位置;
  2. ./ansible.cfg # 其次是当前工作目录(执行 ansibe 指令时的目录)下的配置文件;
  3. ~/ansible.cfg # 再次是当前用户家目录中的配置文件;
  4. /etc/ansible/ansible.cfg # 最后是这个配置文件;
  1. [root@cd-test ~]# vim .bash_profile
  2. # 于最下面追加如下内容:
  3. export ANSIBLE_CFG="/etc/ansible/ansible.cfg"
  4. # 配置生效:
  5. source .bash_profile

Yum

使用 Git 指令进行安装会要求升级 OpenSSH,比较头疼。因而还可使用 yum 进行安装。
安装包下载:
http://releases.ansible.com/ansible/rpm/release/epel-6-x86_64/

  1. [root@PBSNGX03 tools]# yum localinstall ansible-2.5.2-1.el6.ans.noarch.rpm

查看版本信息:

  1. # Check version:
  2. [root@PBSNGX03 tools]# ansible --version
  3. ansible 2.5.2
  4. config file = /etc/ansible/ansible.cfg
  5. configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  6. ansible python module location = /usr/lib/python2.6/site-packages/ansible
  7. executable location = /usr/bin/ansible
  8. python version = 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]

很明显,Python 版本有问题,需要更换。至于怎样更换,目前不会~

编译安装

  1. [root@Ansible01 tools]# tar zxvf ansible-2.6.0.tar.gz -C /opt/
  2. [root@Ansible01 tools]# cd /opt/ansible-2.6.0/
  3. python setup.py install

可创建个软链接:

  1. [root@Ansible01 ~]# cd /opt/
  2. [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 进行安装,则没有配置文件及其目录,需要手动创建。

  1. [root@cd-test ~]# mkdir -p /etc/ansible/
  2. [root@cd-test ~]# cd ansible/examples/
  3. [root@cd-test examples]# cp ansible.cfg /etc/ansible/ # 是 ansible 全局配置文件
  4. [root@cd-test examples]# cp hosts /etc/ansible/ # 是 ansible 管理的主机列表,及部分参数。

免密登录

使用 ansible 批量部署服务的前提是建立免秘钥登录。

配置免密登录:

  1. ssh-keygen -t rsa # 生成密钥对
  2. cat .ssh/id_rsa.pub >>.ssh/authorized_keys # 将公钥导入本机
  3. chmod 600 .ssh/authorized_keys # 安全起见,设置权限为600
  4. ssh-copy-id -i .ssh/id_rsa.pub root@192.168.2.12 # 将该公钥导入其他主机

服务器较多时,逐个手动使用 ssh-copy-id 指令进行免密钥登录配置也是不小的工作量。怎样批量建立免密钥登录呢?
https://segmentfault.com/a/1190000009832597

上面就是简单配置了 SSH 免密之密钥登录法。可是这样有一个安全问题:假如 Ansible 控制端出问题了,甚至被入侵,则全部节点都具有风险。因而出于安全考虑,建议使用带密码之密钥进行登录。配置如下:

  1. mkdir /etc/ansible/ssh_keys
  2. ssh-keygen -t rsa -f /etc/ansible/ssh_keys/node2.key # 注意要给私钥设密码
  3. chmod 700 /etc/ansible/ssh_keys/node2.key
  4. # 将/etc/ansible/ssh_keys/node2.key.pub内容追加到远程主机的authorized_keys文件里面。

然后编辑清单文件:

  1. [node2]
  2. 192.168.2.12 ansible_ssh_private_key_file=/etc/ansible/ssh_keys/node2.key

这样一来,每次远程管理时,会提示输入私钥的密码才能连接服务器。


排错

  1. [root@PBSNGX03 ~]# easy_install pip
  2. ...
  3. 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

  1. [root@PBSNGX03 tools]# unzip distribute-0.7.3.zip
  2. [root@PBSNGX03 tools]# cd distribute-0.7.3
  3. python2.6 setup.py install
  4. # 特别需要注意 Python 版本。

接下来使用 pip 安装模块时还有报错:

  1. SyntaxError: invalid syntax

目前看来是 pip 版本过高,不适用于 Python2.6,需要安装 pip9.0.1 这个版本。

  1. # 移除之前版本的 pip:
  2. [root@PBSNGX03 ~]# cd /usr/lib/python2.6/site-packages/
  3. [root@PBSNGX03 site-packages]# rm -rf pip*
  4. # 重新进行安装:
  5. [root@PBSNGX03 tools]# tar zxvf pip-9.0.1.tar.gz
  6. [root@PBSNGX03 tools]# cd pip-9.0.1
  7. [root@PBSNGX03 pip-9.0.1]# python2.6 setup.py install
  8. # 查看版本信息:
  9. [root@PBSNGX03 pip-9.0.1]# pip -V
  10. pip 9.0.1 from /usr/lib/python2.6/site-packages/pip-9.0.1-py2.6.egg (python 2.6)

而后就能够正常使用了,安装时最好禁用 pip 版本检查,否则老是提醒你进行升级,但是由于 Python2.6 这个版本过低,实际上根本无法升级。

  1. pip install --disable-pip-version-check ...

排错

  1. [root@cd-test02 ansible]# make install
  2. Traceback (most recent call last):
  3. File "packaging/release/versionhelper/version_helper.py", line 9, in <module>
  4. from packaging.version import Version, VERSION_PATTERN
  5. ImportError: No module named packaging.version
  6. Makefile:40: *** "version_helper failed". Stop.

最后稀里糊涂就解决了。

  1. wget https://bootstrap.pypa.io/get-pip.py
  2. python get-pip.py

还是不行,最后执行了下面这个指令解决了。我估计上面那个不用执行。

  1. /usr/local/python3/bin/pip3 install packaging
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注