一 ansible 简介
Ansible 是新出现的自动化运维工具,基于Python研发。糅合了众多老牌运维工具的优点实现了批量操作系统配置、批量程序的部署、批量运行命令等功能。ansible无需安装客户端。ansible应用程序存在于epel(第三方社区)源,依赖于很多python组件。安装ansible时,对于其所依赖的python,使用系统自带的python2即可。

ansible目录结构:

通过如下命令我们可以获取ansible所有文件存放目录:

rpm -ql ansible
该命令输出内容较多,大致分为如下几类:

配置文件目录:/etc/ansible/(ansible.cfg、hosts、roles)
执行文件目录:/usr/bin
lib库依赖目录:/usr/lib/python2.7/site-packages/ansible
ansible插件目录:/usr/share/ansible/plugins
help文档目录:/usr/share/doc/ansible-2.3.1.0
man文档目录:/usr/share/man/man1
 

二 ansible 安装
1 查看可用ansible包版本

[[email protected] ~]# yum list|grep ansible
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
ansible.noarch                              2.4.2.0-2.el7              extras   
ansible-doc.noarch                          2.4.2.0-2.el7              extras   
centos-release-ansible26.noarch             1-3.el7.centos             extras
2 系统可用ansible版本太低,安装epel源,从epel源安装高版本 ansible

yum -y install epel-release
安装完后 /etc/yum.repos.d/ 下会多出来两个yum库 epel.repo  epel-testing.repo

安装完后,再次查看可用ansible包版本

[[email protected] ~]# yum list|grep ansible
ansible.noarch                          2.8.2-1.el7                    epel     
ansible-doc.noarch                      2.8.2-1.el7                    epel     
ansible-inventory-grapher.noarch        2.4.4-1.el7                    epel     
ansible-lint.noarch                     3.5.1-1.el7                    epel     
ansible-openstack-modules.noarch        0-20140902git79d751a.el7       epel     
ansible-review.noarch                   0.13.4-1.el7                   epel     
centos-release-ansible26.noarch         1-3.el7.centos                 extras   
kubernetes-ansible.noarch               0.6.0-0.1.gitd65ebd5.el7       epel     
python2-ansible-runner.noarch           1.0.1-1.el7                    epel     
python2-ansible-tower-cli.noarch        3.3.0-2.el7                    epel     
[[email protected] ~]# 
也可使用yum list ansible 命令查看
[[email protected] ~]# yum list ansible
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.intergrid.com.au
 * epel: mirrors.yun-idc.com
 * extras: mirrors.nju.edu.cn
 * updates: ftp.sjtu.edu.cn
Available Packages
ansible.noarch                                  2.8.2-1.el7                                   epel
3 安装 ansible 2.8.2,并查看版本

yum -y install ansible
查看ansible版本

[[email protected] ~]# ansible --version
ansible 2.8.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
4 配置 /etc/ansible/hosts 文件,添加被管控主机ip

/etc/ansible/ 下有两个文件ansible.cfg、hosts,一个文件夹roles

vim /etc/ansible/hosts

[[email protected] ~]# vim /etc/ansible/hosts
添加 被管控主机ip

eg:

[sjk]
192.168.8.[1:3]
 
[dubbo]
192.168.1.[11:16]
 
[yingyong]
192.168.1.[21:30]
192.168.1.35
192.168.1.36
 
5 配置ansbile主机和被管控主机机器互信(ssh免密登录)

为了使用ansible进行自动化运维,我们需要实现192.168.158.133向其他其他主机的单向ssh免密登录。

为了实现此功能。

我们需要在ansible服务器192.168.158.133上,生成**对(公钥id_rsa.pub,私钥id_rsa),然后将公钥追加到ansible管控端和被管控端的授权文件authorized_keys中

这里有个坑:

authorized_keys 这个文件的权限必须是600(,就算是644 也不行),否则无法由133免密登录到被管控端服务器上。

5.1 在ansible服务器上生成一对**

ssh-****** -t rsa

[[email protected] ~]# cd .ssh    //系统默认不带 .ssh文件
bash: cd: .ssh: No such file or directory
// 生成**对
[[email protected] ~]# ssh-****** -t rsa        // 如果之前生成过**对,那可以直接用,不需要再生成
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.         // 自动生成 /root/.ssh 文件夹
Enter passphrase (empty for no passphrase):   // 直接回车,也可以设置密码
Enter same passphrase again:                  // 直接回车
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:uYpPQZBJkXb5uRw5Ugxs4GFG/qGuVALRZyecHqmwxJc [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|o. [email protected]+         |
|.+.E&o* o        |
|oo.*o*oo o       |
|... .+..*.       |
|  . o ooS+       |
|   +   .o.       |
|  . . . .        |
| . . o .         |
|  . ..o          |
+----[SHA256]-----+
[[email protected] ~]# 
生成的**对如下:

[[email protected] ~]# ls /root/.ssh/
id_rsa  id_rsa.pub
其中id_rsa 是私钥,id_rsa.pub是公钥

5.2 将jenkins服务器的公钥加到远程被管控服务器的 authorized_keys中。

mkdir -p /root/.ssh

cd /root/.ssh && touch authorized_keys

将jenkins服务器的公钥复制到 authorized_keys中

 

5.3 有可能出现的问题

使用ansible命令有可能出现如下告警:

解决办法:

关闭 DEPRECATION WARNING 警告:
vi /etc/ansible/ansible.cfg

# to disable these warnings, set the following value to False:
#deprecation_warnings = True
deprecation_warnings = False

 

而出现 [WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details 警告,是因为 /etc/ansible/hosts 文件中配置的所有组名 存在 以数字开头的,比如[129], (若改成 [t129]就不会报错),但不影响使用。

6 测试

查看所有主机列表

尝试访问"pxc"组下所有ip

如果有主机挂掉了,或网络不通,则该主机会ping失败

尝试访问所有ip

ansible "*" -m ping
 

 
ansible all -m ping
查看192.168.158.144 /usr/local/restart.sh 文件状态

 

三 ansible插件ansible-cmdb 实现cmdb功能

1. 安装 ansible-cmdb 插件

wget https://github.com/fboender/ansible-cmdb/releases/download/1.30/ansible-cmdb-1.30.tar.gz
tar -xvf ansible-cmdb-1.30.tar.gz -C /usr/share/ansible/plugins
cd /usr/share/ansible/plugins/ansible-cmdb-1.30/ && make install

2 添加环境变量

vim /etc/profile

source /etc/profile

3. 生成所有主机的facts信息

ansible all -m setup --tree out/
会在当前目录下生成 out 目录,out目录下都是以主机域名或ip命名的文件。

比如,在/usr/local/ 下执行 ansible all -m setup --tree out/ ,则会在 /usr/local/下生成out目录

或者,

直接指明在哪生成 out目录

ansible all -m setup --tree /usr/local/out
4 通过第3步生成的facts信息生成web页面

cd ~
ansible-cmdb /usr/local/out/ > overview.html
将 ~/overview.html 文件导出可用浏览器直接访问:


ansible简介 及 CentOS7 安装ansible (rpm)及 安装ansible插件ansible-cmdb 实现cmdb功能

 

以资产列表形式统计出ansible主机信息

ansible-cmdb -t txt_table --columns name,os,ip,mem,cpus /usr/local/out/
统计结果会在命令行窗口直接显示:

[[email protected] ~]# ansible-cmdb -t txt_table --columns name,os,ip,mem,cpus /usr/local/out/
Name             OS          IP               Mem  CPUs  
---------------  ----------  ---------------  ---  ----  
192.168.158.133  CentOS 7.5  192.168.158.133  3g   2     
192.168.158.141  CentOS 7.5  192.168.158.141  1g   1     
192.168.158.129  CentOS 7.5  192.168.158.129  2g   2     
192.168.158.142  CentOS 7.5  192.168.158.142  1g   1     
192.168.158.145  CentOS 7.5  192.168.158.145  2g   1     
192.168.158.144  CentOS 7.5  192.168.158.144  2g   1     
192.168.158.146  CentOS 7.5  192.168.158.146  2g   1
 
 

相关文章: