一、背景

因某业务环境部署agent采集,且OS多为Linux,部分windows;考虑采用ansible进行批量部署;

二、概要

ansible作为一款自动化运维工具,它是基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,Ansible默认通过 SSH 协议管理客户机,可实现批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供了一种框架。它的主要组件包括:

【组件说明】:

(1)、连接插件connection plugins:负责和被监控端实现通信;ansible基于连接插件连接到各个主机上,默认ansible是使用ssh连接到各个主机的,但是它还支持其他的连接方法,如图示中的local 、ssh、zeromq ;

(2)、受管主机群host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;可以按应用类型等方式对Host Inventory(主机群)分类,管理节点通过各类模块实现相应的操作;其中,单个模块,单条命令的批量执行,我们可以称之为ad-hoc;

(3)、各种模块核心模块、command模块、自定义模块;CoreModules:核心模块是通过调用核心模块来完成管理任务。CustomModules:自定义模块,完成核心模块无法完成的功能,支持多种语言。

(4)、借助于插件完成记录日志邮件等功能;

(5)、playbook:剧本(非必需)执行多个任务时,可以让节点一次性运行多个任务。通过playbooks 实现多个task的集合实现一类功能,如web服务的安装部署、数据库服务器的批量备份等。

【产品优势】:

(1)、no agents:轻量级,不需要在被管控主机上安装任何客户端;更新时,只需在操作机上进行一次更新即可;

(2)、no server:无服务器端,只需要安装ansible软件,配置完之后,使用时直接运行命令即可;

(3)、modules in any languages:基于模块工作,可使用任意语言开发模块;可使用python编写,维护更简单;模块是幂等性的,定义的任务已存在则不会做任何事情,意味着在同一台服务器上多次执行同一个playbook和执行一次,效果一样

(4)、yaml,not code:使用yaml语言定制剧本playbook;批量任务执行可以写成脚本,而且不用分发到远程就可以执行;

(5)、ssh by default:基于SSH工作;

(6)、strong multi-tier solution:可实现多级指挥。

ansible执行任务的2种模式:

1)ad-hoc模式:单模块,批量执行单条命令;

2)playbook模式:批量执行多个任务,多个任务完成一个大的功能,相当于多个ad-hoc的配置文件。

官方文档:http://www.ansible.com.cn/

【产品特性】:

1)跨平台支持
2)人类可读自动化:ansible提供linux、windows、unix和网络设备的无代理支持,适用于物理、虚拟、云和容器环境。
3)完美描述应用:playbook
4) 轻松管理版本控制:playbook是纯文本,可视作源代码。
5)支持动态清单
6)编排可与其他系统轻松集成:puppet、jenkins。
7)基础架构即代码。
8)减少人为错误。任务、play和playbook设计为具有幂等性,所以在运行playbook时,如果目标主机处于正确状态,则不会进行任何更改。

三、安装准备

3.1 架构了解和工作原理:






3.2 安装注意及资源要求

安装Ansible之后,不需要启动或运行一个后台进程,或是添加一个数据库.只要在一台电脑(可以是一台笔记本)上安装好,就可以通过这台电脑管理一组远程的机器。在远程被管理的机器上,不需安装运行任何软件,因此升级Ansible版本不会有太多问题。Ansible一般每两个月出一个发行版本。

1)对管理主机测的要求:

目前,只要机器上安装了 Python 2.6 或 Python 2.7 (windows系统不可以做控制主机),都可以运行Ansible,主机的系统可以是 Red Hat, Debian, CentOS, OS X, BSD的各种版本,等等。

自2.0版本开始,ansible使用了更多句柄来管理它的子进程,对于MacOS X系统,你需要增加ulimit值才能使用15个以上子进程,方法 **sudo launchctl limit maxfiles 1024 2048,**否则你可能会看见”Too many open file”的错误提示.

2)对托/受管节点的要求:

通常我们使用 ssh 与托管节点通信,默认使用 sftp;如果 sftp 不可用,可在 ansible.cfg 配置文件中配置成 scp 的方式.,在托管节点上也需要安装 Python 2.4 或以上的版本,如果版本低于 Python 2.5 ,还需要额外安装一个模块:python-simplejson。

没安装python-simplejson,也可以使用Ansible的raw模块script模块,因此从技术上讲,你可以通过Ansible的raw模块安装python-simplejson,之后就可以使用Ansible的所有功能了.如果托管节点上开启了SElinux,你需要安装libselinux-python,这样才可使用Ansible中与copy/file/template相关的函数。也可以通过Ansible的yum模块在需要的托管节点上安装libselinux-python。

Python 3 与 Python 2 是稍有不同,大多数Python程序还不能在 Python 3 中正确运行,一些Linux发行版(Gentoo, Arch)默认没有安装 Python 2.X 解释器,这时,你需要安装一个 Python 2.X 解释器,并在 inventory (详见 Inventory文件) 中设置 ‘ansible_python_interpreter’ 变量指向你的 2.X Python;Red Hat Enterprise Linux, CentOS, Fedora, and Ubuntu 等发行版都默认安装了 2.X 的解释器,包括几乎所有的Unix系统也是如此。你可以使用 raw’模块在托管节点上远程安装Python 2.X:

eg:ansible myhost --sudo -m raw -a “yum install -y python2 python-simplejson” ;这条命令可以通过远程方式在托管节点上安装 Python 2.X 和 simplejson 模块.

3)Ansible安装版本选择

Ansible一般每两个月出一个发行版本.小bugs一般在下一个发行版本中修复,并在稳定分支中做backports.大bugs会在必要时出一个维护版本,且也并不是很频繁。

如果当前使用的操作系统是 Red Hat Enterprise Linux ™, CentOS, Fedora, Debian, Ubuntu,建议使用系统的软件包管理器,即rpm 来安装最新版,或使用pip安装。

sudo easy_install pip  //安装pip
pip show pip  //验证版本
sudo pip install ansible

若希望跟进开发版本,想使用和测试最新的功能特性,可从源码运行Ansible,从源码运行程序不需要进行软件安装,直接运行即可。

git clone git://github.com/ansible/ansible.git --recursive
cd ./ansible
source ./hacking/env-setup  //-q不输出错误日志;一旦运行env-setup脚本,就意味着Ansible从源码中运行起来了.默认的inventory文件是 /etc/ansible/hosts.inventory
#验证测试
ansible all -m ping --ask-pass

最后再强调一次:Ansible的运行不要求root权限,也不依赖于其他软件,不要求运行后台进程,也不需要设置数据库。

四、部署过程:

4.1、Ansible的安装与配置

1)RedHat系列中Yum安装ansible环境:

yum  install  python26 python26-PyYAML  python26-paramiko  python26-jinja2   python-simplejson python-setuptools python-cryptography -y
rpm -qa |grep python-crypto
#yum -y remove python-cryptography
yum install ansible -y//自己构建RPM软件包
git clone git://github.com/ansible/ansible.git
cd ./ansible  //在Ansible项目的checkout的根目录下,或是在一个tarball中,使用 make rpm 命令创建RPM软件包
make rpm
sudo rpm -Uvh ~/rpmbuild/ansible-*.noarch.rpm

2)下载ansible,选择相应的版本进行下载

软件地址:http://releases.ansible.com/ansible/


最新版:https://releases.ansible.com/ansible/ansible-2.9.21.tar.gz //发布于2021年5月4日
SHA:https://releases.ansible.com/ansible/ansible-2.9.21.tar.gz.sha
最新稳定版:

#ansible-2.9.13
wget --no-check-certificate https://releases.ansible.com/ansible/ansible-latest.tar.gz
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repoyum list | grep epel-release
yum install -y epel-release
yum repolist enabled
yum repolist all

#对于Red Hat Enterprise Linux7的更新包只对注册的用户生效,所以需要自己手动改成Centos的更新包

rpm -qa | grep yum | xargs rpm -e --nodeps #不检查依赖关系,直接卸载,重新安装centos的yum软件包

[base]
name= yum repo
baseurl=http://mirrors.aliyun.com/centos/7/os/$basearch/
enabled=1
gpgcheck=0

如果yum安装,适用于 EPEL 6, 7,执行:

$ sudo yum -y install ansible //Fedora 用户可直接安装Ansible, 但RHEL或CentOS用户,需要配置 EPEL

$ apt-get install ansible //Ubuntu 、Debian,以及兼容发行版,完整命令见下文

注意:Fedora、RHEL、CentOS要配置epel源仓库。Ubuntu 源码安装最新版:

$ sudo apt-get install software-properties-common  //早期Ubuntu发行版中, “software-properties-common” 名为 “python-software-properties”
$ sudo apt-add-repository ppa:ansible/ansible
$ sudo apt-get update
$ sudo apt-get install ansible
//自己构建Debian/Ubuntu 软件包
cd ./checkout
make deb

配置Epel源:

#cat >/etc/yum.repos.d/epel.repo<< EOF
[epel]
name=epel
mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-$releasever&arch=$basearch
enabled=1
gpgcheck=0
EOFyum clean all && yum updateansible --version   //安装完成后,验证ansible -m setup localhost |grep ansible_python_version   //使用setup模块验证python,-m指定模块

配置Pip源:

3)解压下载的文件并安装:

tar xvf ./ansible-2.9.21.tar.gz
cd ansible-2.9.21
python setup.py build
python setup.py install
#安装cryptography前先安装setuptools,且Setuptools版本需要大于等于18.5
wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-18.5.tar.gz
tar -zxvf setuptools-18.5.tar.gz
cd setuptools-18.5
python setup.py build
python setup.py install  //输出如下类似
……
creating dist
creating 'dist/setuptools-18.5-py2.7.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing setuptools-18.5-py2.7.egg
Copying setuptools-18.5-py2.7.egg to /usr/local/python2.7/lib/python2.7/site-packages
Removing setuptools 0.8b2 from easy-install.pth file
Adding setuptools 18.5 to easy-install.pth file
Installing easy_install script to /usr/local/python2.7/bin
Installing easy_install-2.7 script to /usr/local/python2.7/binInstalled /usr/local/python2.7/lib/python2.7/site-packages/setuptools-18.5-py2.7.egg
Processing dependencies for setuptools==18.5
Finished processing dependencies for setuptools==18.5
#安装cffi
wget https://files.pythonhosted.org/packages/2b/a8/050ab4f0c3d4c1b8aaa805f70e26e84d0e27004907c5b8ecc1d31815f92a/cffi-1.15.1.tar.gz
tar -xzf cffi-1.15.1.tar.gz
cd cffi-1.15.1  //实际验证版本过高与gcc 4.4.7不兼容
wget https://files.pythonhosted.org/packages/0a/f3/686af8873b70028fccf67b15c78fd4e4667a3da995007afc71e786d61b0a/cffi-1.8.3.tar.gz
tar -xzf cffi-1.8.3.tar.gz
cd cffi-1.8.3
yum install libffi-devel //3.0.5-3.2.el6
wget ftp://ftp.pbone.net/mirror/ftp5.gwdg.de/pub/opensuse/repositories/home:/rexut:/RHEL6-missing-deps/RedHat_RHEL-6/x86_64/libffi-devel-3.0.5-3.2.el6.x86_64.rpm
rpm -ivh libffi-devel-3.0.5-3.2.el6.x86_64.rpm
#安装cffi
python setup.py build  //输出类似如下
running build
running build_py
running build_ext
building '_cffi_backend' extension
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -DUSE__THREAD -I/usr/lib64/libffi-3.0.5/include -I/usr/local/python2.7/include/python2.7 -c c/_cffi_backend.c -o build/temp.linux-x86_64-2.7/c/_cffi_backend.o
gcc -pthread -shared build/temp.linux-x86_64-2.7/c/_cffi_backend.o -lffi -o build/lib.linux-x86_64-2.7/_cffi_backend.sopython setup.py install  //输出类似如下
Adding cffi 1.8.3 to easy-install.pth file
Installed /usr/local/python2.7/lib/python2.7/site-packages/cffi-1.8.3-py2.7-linux-x86_64.egg
Processing dependencies for cffi==1.8.3
Searching for pycparser
Reading https://pypi.python.org/simple/pycparser/
Download error on https://pypi.python.org/simple/pycparser/: unknown url type: https -- Some packages may not be found!
Couldn't find index page for 'pycparser' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading https://pypi.python.org/simple/
Download error on https://pypi.python.org/simple/: unknown url type: https -- Some packages may not be found!
No local packages or download links found for pycparser
error: Could not find suitable distribution for Requirement.parse('pycparser')wget https://files.pythonhosted.org/packages/5e/0b/95d387f5f4433cb0f53ff7ad859bd2c6051051cebbb564f139a999ab46de/pycparser-2.21.tar.gz
tar -xzf pycparser-2.21.tar.gz
cd pycparser-2.21
python setup.py install  //输出类似如下
============================
running install_egg_info
running egg_info
creating pycparser.egg-info
writing pycparser.egg-info/PKG-INFO
writing top-level names to pycparser.egg-info/top_level.txt
writing dependency_links to pycparser.egg-info/dependency_links.txt
writing manifest file 'pycparser.egg-info/SOURCES.txt'
reading manifest file 'pycparser.egg-info/SOURCES.txt'
writing manifest file 'pycparser.egg-info/SOURCES.txt'
Copying pycparser.egg-info to /usr/local/python2.7/lib/python2.7/site-packages/pycparser-2.21-py2.7.egg-info
running install_scripts
Build the lexing/parsing tables#再重新安装cffi,执行:
python setup.py install  //输出类似如下cffi 1.8.3 is already the active version in easy-install.pthInstalled /usr/local/python2.7/lib/python2.7/site-packages/cffi-1.8.3-py2.7-linux-x86_64.egg
Processing dependencies for cffi==1.8.3
Searching for pycparser==2.21
Best match: pycparser 2.21
Adding pycparser 2.21 to easy-install.pth fileUsing /usr/local/python2.7/lib/python2.7/site-packages
Finished processing dependencies for cffi==1.8.3##检查
pip list -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
Package    Version
---------- --------
cffi       1.8.3
distribute 0.7.3
hashlib    20081119
pip        18.0
pycparser  2.21
setuptools 18.5    wget --no-check-certificate https://files.pythonhosted.org/packages/f7/0b/dc724c3e1025b7b61aed8071b551286415dade6861170695293899263d68/cryptography-2.9.1.tar.gz
#sha256=ce0bd68b4b946bd4bcebc3d4d1325bf0e938e445ae18cedddd60e33dd85a368e
//离线安装尝试多次未果,失败,打通网络在线安装
pip install cryptography  -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com  //输出如下
Downloading http://mirrors.aliyun.com/pypi/packages/07/92/ad1e9f3c20cecf40892cbbf7a40af88a1eb6871d9a70c5caf378af71f051/cryptography-3.3.2-cp27-cp27m-manylinux1_x86_64.whl (2.7MB)100% |████████████████████████████████| 2.7MB 440kB/s
Collecting enum34; python_version < "3" (from cryptography)Downloading http://mirrors.aliyun.com/pypi/packages/6f/2c/a9386903ece2ea85e9807e0e062174dc26fdce8b05f216d00491be29fad5/enum34-1.1.10-py2-none-any.whl
Collecting cffi>=1.12 (from cryptography)Downloading http://mirrors.aliyun.com/pypi/packages/1d/76/bcebbbab689f5f6fc8a91e361038a3001ee2e48c5f9dbad0a3b64a64cc9e/cffi-1.15.1-cp27-cp27m-manylinux1_x86_64.whl (390kB)100% |████████████████████████████████| 399kB 418kB/s
Collecting six>=1.4.1 (from cryptography)Downloading http://mirrors.aliyun.com/pypi/packages/d9/5a/e7c31adbe875f2abbb91bd84cf2dc52d792b5a01506781dbcf25c91daf11/six-1.16.0-py2.py3-none-any.whl
Collecting ipaddress; python_version < "3" (from cryptography)Downloading http://mirrors.aliyun.com/pypi/packages/c2/f8/49697181b1651d8347d24c095ce46c7346c37335ddc7d255833e7cde674d/ipaddress-1.0.23-py2.py3-none-any.whl
Requirement already satisfied: pycparser in /usr/local/python2.7/lib/python2.7/site-packages (from cffi>=1.12->cryptography) (2.21)
ansible 2.9.13 requires jinja2, which is not installed.
ansible 2.9.13 requires PyYAML, which is not installed.
Installing collected packages: enum34, cffi, six, ipaddress, cryptographyFound existing installation: cffi 1.8.3Uninstalling cffi-1.8.3:Successfully uninstalled cffi-1.8.3
Successfully installed cffi-1.15.1 cryptography-3.3.2 enum34-1.1.10 ipaddress-1.0.23 six-1.16.0#安装jinja2
pip install jinja2  -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
……
Downloading http://mirrors.aliyun.com/pypi/packages/7e/c2/1eece8c95ddbc9b1aeb64f5783a9e07a286de42191b7204d67b7496ddf35/Jinja2-2.11.3-py2.py3-none-any.whl (125kB)100% |████████████████████████████████| 133kB 407kB/s
Collecting MarkupSafe>=0.23 (from jinja2)Downloading http://mirrors.aliyun.com/pypi/packages/d8/1f/e97c4c6b182e59562f99c207f0f621d15a42fc82a6532a98e0b2d38b7c4e/MarkupSafe-1.1.1-cp27-cp27m-manylinux1_x86_64.whl
ansible 2.9.13 requires PyYAML, which is not installed.
Installing collected packages: MarkupSafe, jinja2
Successfully installed MarkupSafe-1.1.1 jinja2-2.11.3#安装PyYAML
pip install PyYAML  -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
mkdir /etc/ansible
……
Downloading http://mirrors.aliyun.com/pypi/packages/a0/a4/d63f2d7597e1a4b55aa3b4d6c5b029991d3b824b5bd331af8d4ab1ed687d/PyYAML-5.4.1.tar.gz (175kB)100% |████████████████████████████████| 184kB 439kB/s Installing build dependencies ... done
Installing collected packages: PyYAMLRunning setup.py install for PyYAML ... done
Successfully installed PyYAML-5.4.1#安装ansible
python setup.py build
python setup.py install  //输出类似如下
……
copying lib/ansible.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying lib/ansible.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying lib/ansible.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying lib/ansible.egg-info/not-zip-safe -> build/bdist.linux-x86_64/egg/EGG-INFO
copying lib/ansible.egg-info/requires.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying lib/ansible.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
creating 'dist/ansible-2.9.13-py2.7.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing ansible-2.9.13-py2.7.egg
removing '/usr/local/python2.7/lib/python2.7/site-packages/ansible-2.9.13-py2.7.egg' (and everything under it)
creating /usr/local/python2.7/lib/python2.7/site-packages/ansible-2.9.13-py2.7.egg
Extracting ansible-2.9.13-py2.7.egg to /usr/local/python2.7/lib/python2.7/site-packages
ansible 2.9.13 is already the active version in easy-install.pth
Installing ansible-playbook script to /usr/local/python2.7/bin
Installing ansible-inventory script to /usr/local/python2.7/bin
Installing ansible-config script to /usr/local/python2.7/bin
Installing ansible-pull script to /usr/local/python2.7/bin
Installing ansible-console script to /usr/local/python2.7/bin
Installing ansible script to /usr/local/python2.7/bin
Installing ansible-connection script to /usr/local/python2.7/bin
Installing ansible-test script to /usr/local/python2.7/bin
Installing ansible-doc script to /usr/local/python2.7/bin
Installing ansible-galaxy script to /usr/local/python2.7/bin
Installing ansible-vault script to /usr/local/python2.7/binInstalled /usr/local/python2.7/lib/python2.7/site-packages/ansible-2.9.13-py2.7.egg
Processing dependencies for ansible==2.9.13
Searching for cryptography==3.3.2
Best match: cryptography 3.3.2
Adding cryptography 3.3.2 to easy-install.pth fileUsing /usr/local/python2.7/lib/python2.7/site-packages
Searching for PyYAML==5.4.1
Best match: PyYAML 5.4.1
Adding PyYAML 5.4.1 to easy-install.pth fileUsing /usr/local/python2.7/lib/python2.7/site-packages
Searching for Jinja2==2.11.3
Best match: Jinja2 2.11.3
Adding Jinja2 2.11.3 to easy-install.pth fileUsing /usr/local/python2.7/lib/python2.7/site-packages
Searching for cffi==1.15.1
Best match: cffi 1.15.1
Adding cffi 1.15.1 to easy-install.pth fileUsing /usr/local/python2.7/lib/python2.7/site-packages
Searching for ipaddress==1.0.23
Best match: ipaddress 1.0.23
Adding ipaddress 1.0.23 to easy-install.pth fileUsing /usr/local/python2.7/lib/python2.7/site-packages
Searching for enum34==1.1.10
Best match: enum34 1.1.10
Adding enum34 1.1.10 to easy-install.pth fileUsing /usr/local/python2.7/lib/python2.7/site-packages
Searching for six==1.16.0
Best match: six 1.16.0
Adding six 1.16.0 to easy-install.pth fileUsing /usr/local/python2.7/lib/python2.7/site-packages
Searching for MarkupSafe==1.1.1
Best match: MarkupSafe 1.1.1
Adding MarkupSafe 1.1.1 to easy-install.pth fileUsing /usr/local/python2.7/lib/python2.7/site-packages
Searching for pycparser==2.21
Best match: pycparser 2.21
Adding pycparser 2.21 to easy-install.pth fileUsing /usr/local/python2.7/lib/python2.7/site-packages
Finished processing dependencies for ansible==2.9.13#验证
ansible --version  //输出类似如下/usr/local/python2.7/lib/python2.7/site-packages/ansible-2.9.13-py2.7.egg/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in the next release.from cryptography.exceptions import InvalidSignature
ansible 2.9.13config file = Noneconfigured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']ansible python module location = /usr/local/python2.7/lib/python2.7/site-packages/ansible-2.9.13-py2.7.egg/ansibleexecutable location = /usr/bin/ansiblepython version = 2.7.3 (default, Jul 10 2022, 12:35:31) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)]pip list       //软件查看验证
Package      Version
------------ --------
ansible      2.9.13
cffi         1.15.1
cryptography 3.3.2
distribute   0.7.3
enum34       1.1.10
hashlib      20081119
ipaddress    1.0.23
Jinja2       2.11.3
MarkupSafe   1.1.1
pip          18.0
pycparser    2.21
PyYAML       5.4.1
setuptools   18.5
six          1.16.0  cp -r ./ansible-2.9.21/examples/* /etc/ansible


【ansible版本总结】:

组件 组件版本 依赖性
ansible 2.9.13 主要包
cffi 1.15.1 cryptography依赖,现场离线安装cffi-1.8.3,成功了但安装cryptography失败
cryptography 3.3.2 ansible依赖;现场离线安装cryptography-2.9.1失败,原因未知
distribute 0.7.3 pip依赖
enum34 1.1.10 未知
hashlib 20081119 Pip依赖
ipaddress 1.0.23 未知
Jinja2 2.11.3 ansible依赖
MarkupSafe 1.1.1 未知
pip 18.0 ansible依赖
pycparser 2.21 cffi依赖
PyYAML 5.4.1 ansible依赖
setuptools 18.5 cffi依赖
six 1.16.0 未知

4)控制端进行ssh免密钥登录设置(只在控制端使用)

ssh-keygen -t rsa -P ''     #####生成公钥/私钥
cat /root/.ssh/id_rsa_web.pub >> /root/.ssh/authorized_keys
scp /root/.ssh/id_rsa_web.pub root@192.168.180.5:/root/.ssh/   //在ansible服务器上执行,下发到5主机上cat /root/.ssh/id_rsa_web.pub >> /root/.ssh/authorized_keys //托管主机上执行
chmod 600 /root/.ssh/authorized_keys        //这里一定是600,rw权限

5)Ansible默认安装好后有一个配置文件**/etc/ansible/ansible.cfg**,该配置文件中定义了ansible的控制主机的默认配置部分;(为防止数据混乱,建议建立新的配置文件和清单文件用来储存相关数据)

配置文件中包含有:默认是否需要输入密码、是否开启sudo认证、action_plugins插件的位置、hosts主机组的位置、是否开启log功能、默认端口、key文件位置等等,默认的都是关闭,ansible上开启remote_port和private_key_file两个配置,修改:

remote_port = 22 ####用于连接远程ssh的端口
private_key_file = /root/.ssh/id_rsa_web ####用于免密码登陆的

6)ansible上默认清单文件为:/etc/ansible/hosts,该清单文件定义了hosts主机组(被控制主机,ansible将要管理的一批主机)

#vim /etc/ansible/hosts //一般我们使用的静态清单(常复制手动创建一份而不用默认的),即清单配置文件中每行一个,填写主机名或ip;类似如下:

[web] ##主机组名称
192.168.180.4
192.168.180.5
192.168.180.3
[db] ##主机组名称,注意:一台主机可以存在于多个主机组中
192.168.180.2
192.168.180.3

另外,ansible主机清单可以包含多个主机组构成的组,如:

[web]
rhel7_node2.cmcc.com
rhel8_node1.cmcc.com
192.168.43.30

[dbs]
server1.cmcc.com
server2.cmcc.com
192.168.43.30

[servers:children]
web
db

我们还可以通过指定主机名称或ip范围或者数字和字母范围来动态纳管更多主机:

语法:[START:END]

eg:192.168.43.[0:254] //匹配192.168.43.0/24 ip的主机

【动态清单】:可以从开源社区的脚本中获取

【自定义清单】:
#mkdir deploy-inventory ##建立清单目录
#vim inventory ##建立清单文件
#ansible all -i inventory --list-hosts ##列出inventory 清单所有受管主机

7)验证测试:

ansible web -m command -a 'uptime'   //在web托管主机组上执行uptime命令
ansible  all -m command -a 'uptime'     //所有主机上运行
ansible web --list-hosts                        //列出web组的所有主机
ansible db --list-hosts                             //列出db组的所有主机
ansible  all   --list-hosts                         //查看清单中管理的所有主机
ansible ungrouped --list-hosts                  //查看清单中不在组里面的主机

8)其他模块
#ansible-doc –l //查看更多模块,-s 模块名称,查看指定模块

  • 文件模块:

-copy 将本地文件复制到其他受管主机
-file 设置文件的权限和其他属性
-lineinfile 确保特定行是否在文件中
-synchronize 使用rsync同步内容

  • 系统模块:

-firewalld 使用firewalld管理任务端口和服务
-reboot 重启
-service 管理服务
-user 添加、删除和管理用户账户

  • Net Tools模块:

-get_url 通过http 、https、或者ftp下载文件
-nmcli 管理网络
-uri 与web服务交互

  • 其他常用模块,比如:service、cron、yum、synchronize就不一一例举,可以结合自身的系统环境进行测试。

service:系统服务管理

cron:计划任务管理

yum:yum软件包安装管理

synchronize:使用rsync同步文件

user:系统用户管理

group:系统用户组管理

setup:用来查看远程主机的一些基本信息

ping:用来测试远程主机的运行状态;ansible-doc ping 可查看ping模块的帮助文档

file模块: 设置文件的属性,相关选项如下:

        force:需要在两种情况下强制创建软链接,一种是源文件不存在,但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|nogroup:定义文件/目录的属组mode:定义文件/目录的权限owner:定义文件/目录的属主path:必选项,定义文件/目录的路径recurse:递归设置文件的属性,只对目录有效src:被链接的源文件路径,只应用于state=link的情况dest:被链接到的路径,只应用于state=link的情况state:directory:如果目录不存在,就创建目录file:即使文件不存在,也不会被创建link:创建软链接hard:创建硬链接touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间absent:删除目录、文件或者取消链接文件   

eg1:ansible web -m file -a “src=/etc/resolv.conf dest=/tmp/resolv.conf state=link” //远程主机上创建文件符号链接

eg2:ansible web -m command -a “ls -al /tmp/resolv.conf” //远程文件查看信息

eg3:ansible storm_cluster -m file -a “path=/tmp/resolv.conf state=absent” //远程文件符号链接删除

  • copy模块: 复制文件到远程主机,相关选项如下:
backup:在覆盖之前,将源文件备份,备份文件包含时间信息。有两个选项:yes|nocontent:用于替代“src”,可以直接设定指定文件的值dest:必选项。要将源文件复制到的远程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目录directory_mode:递归设定目录的权限,默认为系统默认权限force:如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yesothers:所有的file模块里的选项都可以在这里使用src:被复制到远程主机的本地文件,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用“/”来结尾,则只复制目录里的内容,如果没有使用“/”来结尾,则包含目录在内的整个内容全部复制,类似于rsync。

eg1:#ansible web -m copy -a “src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=root group=root mode=0644” //将本地文件“/etc/ansible/ansible.cfg”复制到远程服务器,src=指明本地文件路径;dest=远程主机上用来引用的路径,通常是绝对路径;mode=指定权限onwer=指定属主,group=指定数组

eg2:#ansible web -m command -a “ls -al /tmp/ansible.cfg” // 远程文件信息查看

  • command模块:在远程主机上执行命令;该模块通过-a跟上要执行的命令可以直接执行,不过命令里如果有带有如下字符部分则会执行不成功: “ so variables like $HOME and operations like “<”, “>”, “|”, and “&” will not work (use the shell module if you need these features).”;相关选项如下:
creates:一个文件名,当该文件存在,则该命令不执行free_form:要执行的linux指令chdir:在执行指令之前,先切换到该目录removes:一个文件名,当该文件不存在,则该选项不执行executable:切换shell来执行指令,该执行路径必须是一个绝对路径

#ansible hostname -m ping //测试主机连通性

shell模块: 远程执行shell,需要在ansible服务器先在本地创建一个SHELL脚本,然后发送到托管主机上;用法其本和command一样,不过的是其是通过/bin/sh进行执行,所以shell 模块可以执行任何命令,就像在本机执行一样。

eg1:#ansible web -m copy -a “src=/tmp/rocketliu_test.sh dest=/tmp/rocketliu_test.sh owner=root group=root mode=0755” //将创建的脚本文件分发到远程

#ansible storm_cluster -m shell -a “/tmp/rocketliu_test.sh” // 远程执行

RAW模块:用法和shell 模块一样 ,其也可以执行任意命令,就像在本机执行一样,“Executes a low-down and dirty SSH command, not going through the module subsystem. There is no change handler support for this module. This module does not require python on the remote system”

script模块:其是将管理端的shell 在被管理主机上执行,其原理是先将shell 复制到远程主机,再在远程主机上执行,原理类似于raw模块,“This module does not require python on the remote system, much like the raw module.” ;raw模块和comand、shell 模块不同的是其没有chdir、creates、removes参数,chdir参数的作用就是先切到chdir指定的目录后,再执行后面的命令。


针对上述,做免密登陆,可如下操作:

ssh-keygen ##生成密钥
ssh-copy-id rhel7_node2.westos.com ##将公钥传到rhel7主机中
ssh-agent bash ##设置免交互
ssh-add ##添加ssh交互秘钥

#ansible web -m command -a ‘hostname’ -o ##command 模块允许执行远程命令,但这些命令不是shell 处理,所以无法访问shell环境变量,所以不能执行重定向,传送等操作。

9)ansible命令:

ansible是指令核心部分,其主要用于执行ad-hoc命令,即单条命令。默认后面需要跟主机和选项部分,默认不指定模块时,使用的是command模块,但默认使用的模块是可以在ansible.cfg 中进行修改。

语法:ansible [options]

eg:ansible 主机或组 -m 模块名 -a ‘模块参数’ ansible参数 //其中,主机和组,是在/etc/ansible/hosts 里进行指定的;
模块名,可以通过ansible-doc -l 查看目前安装的模块,默认不指定时,使用的是command模块,具体可以查看/etc/ansible/ansible.cfg 的“#module_name = command ” 部分,默认模块可以在该配置文件中进行修改;
模块参数,可以通过 “ansible-doc 模块名” 查看具体的用法及后面的参数;
ansible参数,可以通过ansible命令的帮忙信息里查看到,这里有很多参数可以供选择,如是否需要输入密码、是否sudo等。


10)其他相关命令:

ansible-galaxy 指令用于方便的从https://galaxy.ansible.com/ 站点下载第三方扩展模块;类似于centos下的yum、python下的pip或easy_install。

ansible-lint是对playbook的语法进行检查的一个工具。用法是ansible-lint playbook.yml

ansible-playbook该指令是使用最多的指令,其通过读取playbook 文件后,执行相应的动作;

ansible-pull:该指令用于ansible的pull 模式

ansible-vault:主要应用于配置文件中含有敏感信息,vault可以帮你加密/解密这个配置文件;主要对于playbooks里比如涉及到配置密码或其他变量时,可以通过该指令加密,这样我们通过cat看到的会是一个密码串类的文件,编辑的时候需要输入事先设定的密码才能打开。这种playbook文件在执行时,需要加上 --ask-vault-pass参数,同样需要输入密码后才能正常执行。

11)命令示例:

当命令执行时间比较长时,也可以放到后台执行,这里会用到-B、-P参数,如下:
ansible all -B 3600 -a "/usr/bin/long_running_operation --do-stuff" #\\后台执行命令 3600s,-B 表示后执行的时间
ansible all -m async_status -a "jid=123456789"  #\\检查任务的状态
ansible all -B 1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff" #\\后台执行命令最大时间是 1800s 即 30 分钟,-P 每 60s 检查下状态,默认 15seg1:ansible 10.212.52.252 -B 3600 -P 0 -a 'watch ls'  ##252主机上执行watch监控ls的命令,保持1h,状态实时检查
background launch...
10.212.52.252 | success >> {"ansible_job_id": "411650646689.13501",
"results_file": "/root/.ansible_async/411650646689.13501",
"started": 1
}
eg2:ansible 10.212.52.252 -m async_status -a 'jid=411650646689.13501'   ##
10.212.52.252 | success >> {"ansible_job_id": "411650646689.13501",
"changed": false,
"finished": 0,
"results_file": "/root/.ansible_async/411650646689.13501",
"started": 1
}
##不指定-P或-P参数为非0时,该任务就会按-P直接的参数一直刷新下去,直到超出-B参数指定的时间或命令执行完成:
eg3:ansible 10.212.52.252 -m command -a 'ps auxf|grep snmp'
eg4: ansible 10.212.52.252 -m raw -a 'ps auxf|grep snmp'
eg5:ansible 10.212.52.252 -m shell -a 'ps auxf|grep snmp'  ##可看出,加了管道,command模块执行时出错,而使用raw模块和shell 模块都正常。

详细示例可参看:https://blog.csdn.net/asd1992498/article/details/53884276?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_utm_term~default-5.no_search_link&spm=1001.2101.3001.4242

4.2 源码安装

$ git clone git://github.com/ansible/ansible.git --recursive //GitHub仓库下载项目
$ cd ./ansible
$ source ./hacking/env-setup //加-p参数可抑制错误和告警输出
$ sudo easy_install pip    //需先安装对应于你的Python版本的pip
$ sudo pip install paramiko PyYAML Jinja2 httplib2 six //安装相关的python模块,注意,当更新ansible版本时,不只要更新git的源码树,也要更新git中指向Ansible自身模块的 “submodules” (不是同一种模块)
$ git pull --rebase
$ git submodule update --init --recursive
一旦运行env-setup脚本,就意味着Ansible从源码中运行起来了.默认的inventory文件是 /etc/ansible/hosts.inventory文件也可以另行指定
$ echo “127.0.0.1” > ~/ansible_hosts
$ export ANSIBLE_HOSTS=~/ansible_hosts
$ ansible all -m ping --ask-pass //验证

我们可以自己创建RPM软件包.在Ansible项目的checkout的根目录下,或是在一个tarball中,使用 make rpm 命令创建RPM软件包. 然后可分发这个软件包或是使用它来安装Ansible.在创建之前,先确定你已安装了 rpm-build, make, and python2-devel :

$ git clone git://github.com/ansible/ansible.git
$ cd ./ansible
$ make rpm
$ sudo rpm -Uvh ~/rpmbuild/ansible-*.noarch.rpm

$ make deb //从源码checkout中创建 Debian/Ubuntu 软件包

4.3 Pip安装

通过 Pip 安装最新发布版本,Ansible可通过 “pip” 安装(安装和管理Python包的工具);
$ sudo easy_install pip
$ sudo pip install ansible

4.4 配置文件

默认:/etc/ansible/ansible.cfg,有时使用 ~/.ansible.cfg代替前者;一般常用ansible命令所在目录的配置文件,./ansible.cfg(如果有)。

#ansible all -i inventory --list-hosts -v //显示当前使用的配置文件:

配置文件中:
[defaults] ##部分设置ansible操作的默认值
[privilege_escalation] ##配置ansible如何在受管主机上在执行特权升级

4.5、web管理界面工具Ansible Tower

# 安装curl postgresql
yum -y install vim curl postgresql
mkdir /tmp/tower && cd /tmp/tower
# 下载安装包
curl -k -O https://releases.ansible.com/ansible-tower/setup/ansible-tower-setup-latest.tar.gz
# 解压
tar xvf ansible-tower-setup-latest.tar.gz
# 进入到安装目录
cd ansible-tower-setup*/
# 修改配置文件
sed -i "s#password=''#password='324215'#g" inventory
sed -i "s#host=''#host='127.0.0.1'#g" inventory
sed -i "s#port=''#port='5432'#g" inventory# 安装并启动supervisord服务,否则启动报错systemctl enable supervisord
systemctl start supervisord# 创建日志目录,然后安装,否则会报错mkdir -p /var/log/tower
./setup.sh

五、软件使用


Ansible使用者可以采用多种方式和Ansible交互,图中展示了四种方式:

CMDB:CMDB存储和管理者企业IT架构中的各项配置信息,是构建ITIL项目的核心工具,运维人员可以组合CMDB和Ansible,通过CMDB直接下发指令调用Ansible工具集完成操作者所希望达到的目标;

PUBLIC/PRIVATE方式:Ansible除了丰富的内置模块外,同时还提供丰富的API语言接口,如PHP、Python、PERL等多种流行语言,基于PUBLIC/PRIVATE,Ansible以API调用的方式运行;

Ad-Hoc命令集:Users直接通过Ad-Hoc命令集调用Ansible工具集来完成任务;

Playbooks:Users预先编写好Ansible Playbooks,通过执行Playbooks中预先编排好的任务集,按序执行任务;

技术博客:https://www.ansible.com/blog
参考文档:http://www.ansible.com.cn/
Ansible官方文档:http://www.ansible.com.cn/index.html,https://docs.ansible.com/ansible/latest/index.html
相关视频:https://www.bilibili.com/video/BV1Cz411q7XN/?p=2&spm_id_from=pageDriver

5.1、远程管理执行命令

5.2、Playbook剧本编写

playbooks 是简单的配置管理系统与多机器部署系统的基础,playbooks 中可以编排有序的执行过程,甚至于做到在多组机器间,来回有序的执行特别指定的步骤.并且可以同步或异步的发起任务。Playbooks 采用YAML(YAML语法)格式编写,YAML文件的扩展名通常为.yaml或.yml。每一个 YAML 文件都是从一个列表开始.。列表中的每一项都是一个键值对, 通常它们被称为一个 “哈希” 或 “字典”。另外, 所有的 YAML 文件(无论和 Ansible 有没有关系)开始行都应该是 —. 这是 YAML 格式的一部分, 表明一个文件的开始。列表中的所有成员都开始于相同的缩进级别, 并且使用一个 “- 成员name:” 作为开头(一个横杠和一个空格);一个字典是由一个简单的 键: 值 的形式组成(这个冒号后面必须是一个空格)。总之,YAML其结构通过缩进来展示,通过“-”来代表项;“:”用来分隔键和值;所有的“-”和“:”后面均有空格,而且注意缩进和对齐,整个文件以“—”开头并以“…”结尾。

playbook 由一个或多个 ‘plays’ 组成.它的内容是一个以 ‘plays’ 为元素的列表。在 play 之中,一组机器被映射为定义好的角色.在 ansible 中,play 的内容,被称为 tasks,即任务.在基本层次的应用中,一个任务是一个对 ansible 模块的调用,以下是一个ansible的一个剧本示例:

---
- hosts: webserversvars:http_port: 80max_clients: 200remote_user: roottasks:- name: ensure apache is at the latest versionyum: pkg=httpd state=latest- name: write the apache config filetemplate: src=/srv/httpd.j2 dest=/etc/httpd.confnotify:- restart apache- name: ensure apache is runningservice: name=httpd state=startedhandlers:- name: restart apacheservice: name=httpd state=restarted
...

Devops常用工具软件之ansible部署使用相关推荐

  1. Devops常用工具软件总结笔记(持续更新中……)

    一.概览 Devops一词来自于Development 和 Operations 的组合,从字面意思了解,就是让软件开发人员和运维人员更好的沟通合作,通过自动化流程让软件开发.测试.发布更快捷. De ...

  2. 计算机工具软件应用考试,《计算机常用工具软件》期中考试题

    <计算机常用工具软件>期中考试题 一.单选题 1.使用PartitionMagic创建新分区时,默认的文件系统类型是() A.FAT16 B.FAT32 C.NTFS D.Linux Ex ...

  3. 日常使用计算机工具软件,计算机常用工具软件的使用.doc

    计算机常用工具软件的使用.doc 计算机常用工具软件的使用 摘要:本文初略的介绍了几种计算机常用工具软件的使用,并由此宣扬熟练,灵活的把计算机当作日常生活中处理问题,解决问题的一种重要工具的理念. 关 ...

  4. 大学计算机基础英语翻译,大学计算机基础 教学课件 978 7 302 21247 8 第7章 常用工具软件.pdf...

    第7章 常用工具软件 7.1 文件压缩工具--WinRAR 7.2 图片浏览工具--ACDSee 7.3 常用杀毒工具--RISING 7.4 硬盘备份工具--Ghost 7.5 翻译工具--金山词霸 ...

  5. 办公计算机应用常用软件,办公自动化及常用工具软件

    办公自动化及常用工具软件 在计算机技术迅速发展的信息社会,计算机应用几乎已经进入了人们学习.工作.生活的方方面面中,用计算机解决工作中的问题已经成为了人们必须掌握的一项基本技能.下面是小编整理的办公自 ...

  6. 计算机常用软件工具试题,计算机常用工具软件试题-20210402163232.docx-原创力文档...

    精品文档 精品文档 薆 蚃肇 芀PAGE PAGE4 精品文档 PAGE 计算机常用工具软件试题 (考试时间60分钟 满分100分) (第一卷)一.选择题(本题共25小题,每小题2分,共50分.请将答 ...

  7. 计算机实用软件大全pdf,常用工具软件大全.pdf

    常用工具软件大全 内 容 简 介 本书将当前最流行的面向个人电脑用户的工具软件进行了详细的分类,包括文件压缩.备份工具.软件卸 载.文件管理.系统维护等十三个类别,基本上涵盖了电脑使用的各个方面.现在 ...

  8. 计算机常用工具软件第5版,计算机常用工具软件(第3版)

    计算机常用工具软件(第3版) 出版时间:2011年11月 定 价:25.00 I S B N :9787121148163 所属分类: 计算机•网络 &nbsp计算机•网络  >  软件 ...

  9. 教师教学常用计算机软件,浅谈《计算机常用工具软件》的教学心得

    摘 要:随着计算机技术的普及,社会对掌握计算机技术的人才不断提出新的要求,人们所需要掌握的计算机技术也越来越多.<计算机常用工具软件>具有实践性.应用性.灵活性等特点,旨在培养职高学生能够 ...

最新文章

  1. JavaScript之Promise实现
  2. 在你休息时,你的大脑运动皮层中重放习得的神经放电序列
  3. 中国最大的python社区-python最大数
  4. python语言自学-【经验分享】自学Python的学习顺序!附学习资料
  5. php文件代码采用UTF-8编码的BOM问题---转自CSDN---十方地藏---http://blog.csdn.net/webdesman...
  6. 虚拟form 下载文件
  7. ElasticSearch-安装以及常见错误(自己测试过yum install -y log4j* )
  8. java 为什么序列化_java类为什么要序列化
  9. 池化对象 RecyclableMemoryStream 在 .netcore 中的使用
  10. 前端开发-Weex初试
  11. 《Groovy语言规范》-语法(一)
  12. HpSocket HttpEasyClient 二次封装
  13. Excel堆积柱形图制作过程
  14. 《2022中国PaaS市场研究及选型评估报告》正式发布
  15. html5表格数据加载,bootstrap table load加载数据到表格的方法
  16. 全球与中国混频器市场现状及未来发展趋势
  17. 2-1个人小程序注册
  18. C语言程序设计精髓 第14周——结构设计的艺术 练兵区——编程题
  19. 方差分析ANOVA:理论、推导与R语言实现
  20. 还在纠结交易系统的细节吗?看这篇文章就够了!

热门文章

  1. Google面试题 图论的问题
  2. GNU Radio3.8创建OOT的详细过程(基础/C++)
  3. Cyclone V SoC FPGA学习之路第一章:综述
  4. dede服务器建站_如何用DEDECMS建站的详细新手指南
  5. 你是外包,麻烦不要偷吃零食,注意素质..
  6. java给图片加水印_java中怎么样将水印加在图片的上面或者下面
  7. 【机器学习的数学基础】(五)解析几何(Analytic Geometry)(下)
  8. 【恒指早盘分析】8.29恒指早盘思路
  9. gh-ost的原子rename
  10. 拓嘉辰丰:拼多多详情图制作方法