在Ansible中,将可管理的服务器集合成为Inventory。因此,Inventory管理便是服务器管理。

hosts文件位置

我们知道,Ansible在执行操作时,首先需要确定对哪些服务器执行操作。默认情况下,Ansible读取/etc/ansible/hosts文件中的服务器配置,获取需要操作的服务器列表。Ansible定义与获取服务器列表的方式比这个要灵活的多。

在Ansible中,有三种方式指定hosts文件:

  1)默认读取/etc/ansible/hosts文件

  2)通过命令行参数的 -i 指定hosts文件

  3)通过ansible.cfg文件的inventory选项

ansible命令的--list-hosts选项用来显示匹配的服务器列表,我们可以通过该参数验证服务器的匹配情况

[heboan@c1 ~]$ ansible test --list-hostshosts (2):192.168.88.2192.168.88.3

我们在家目录下创建个hosts,然后用-i指定这个hosts

[heboan@c1 ~]$ cat hosts
[test]
192.168.20.1
192.168.20.2
[heboan@c1 ~]$
[heboan@c1 ~]$ ansible test -i hosts --list-hostshosts (2):192.168.20.1192.168.20.2
[heboan@c1 ~]$
[heboan@c1 ~]$ ansible test --list-hosts   #默认情况hosts (2):192.168.88.2192.168.88.3

我们也可以在ansible.cfg中通过inventory选项指定hosts文件路径

[heboan@c1 ~]$ cat /etc/ansible/ansible.cfg
[defaults]
remote_port = 2202
remote_user = heboan
inventory = /home/heboan/hosts
[heboan@c1 ~]$
[heboan@c1 ~]$ ansible test --list-hostshosts (2):192.168.20.1192.168.20.2

灵活定义hosts文件内容

Ansible支持更加灵活的方式定义hosts文件,例如将服务器进行分组,以便对不同的服务器类型进行不同的操作, 如下

mail.heboan.com    #不属于任何一个组

[webservers]
foo.heboan.com
bar.heboan.com[dbserver]
one.heboan.com
two.heboan.com
three.heboan.com

在服务器匹配时,all或星号是一个特殊的名称,用于匹配所有的服务器

[heboan@c1 ~]$ ansible '*' --list-hostshosts (6):mail.heboan.comfoo.heboan.combar.heboan.comone.heboan.comtwo.heboan.comthree.heboan.com

匹配所有服务器

使用组名,匹配该组所有服务器

[heboan@c1 ~]$ ansible dbserver --list-hostshosts (3):one.heboan.comtwo.heboan.comthree.heboan.com

匹配组服务器

Ansible也可以定义一个组,这个组下面定义的不是服务器列表,而是包含其他组的名称, 通过":children"的方式来声明

[common:children]
webserver
dbserver

如果我有一批服务器,并且这些服务器有相同的模式,我们可以向下面这样定义服务器

[webservers]
web[1:3].heboan.com
bar.heboan.com[dbservers]
db[a:d].heboan.com

灵活匹配hosts文件内容

规则 含义
192.168.88.2或c2.heboan.com 匹配模板IP地址或服务器名,如果有多个ip或服务器,使用":"分隔
webservers  匹配目标组为webservers,多个组使用":"分隔
 all或'*'  匹配所有服务器
 webservers:!dbservers  匹配在webservers中,不在dbservers组中的服务器
 webservers:&dbservers  匹配同时在webservers组以及在dbservers组中的服务器
 *.example.com或192.168.*  使用通配符匹配
 webservers[0],webservers[1:],webservers[-1]  使用索引或切片操作的方式匹配组中的服务器
 ~(web|db).*example.com  以~开头的匹配,表示使用正则表达式匹配

动态Inventory获取

从前面的介绍可以看到,Ansible提供给你了非常灵活的方式来编写hosts文件,可以节省不必要的时间浪费、提高工作效率。此外,Ansible还可以通过调用云计算服务的API,编写自定义脚本的方式获取服务器列表。如果公司有使用CMDB系统来管理服务器,那么,我们可以通过读取CMDB数据库中的记录得到服务器列表。

既然 我们可以通过调用云计算系统的API,或者读取CMDB系统的数据库获取服务器列表,那么,再将服务器地址写入hosts文件就显得比较冗余,且没有任何必要。

一个动态获取服务器列表的脚本必须支持如下两个命令行参数:

  1)--host=<hostname>: 用于列出某台服务器的详细信息

  2)--list:用于列出群组以及群组中的服务器

例如,我们才CMDB系统库中包含了一些服务器的信息, 如果将这些服务器信息从数据库中拷贝到hosts文件中,效果如下:

[root@c1 ~]# cat /etc/ansible/hosts
[test]
192.168.88.2 ansible_user=heboan ansible_port=22
192.168.88.3 ansible_user=heboan ansible_port=22[uat]
192.168.88.4 ansible_user=heboan ansible_port=22

按照Ansible的约定,我们需要编写一个动态脚本来获取服务器的列表,这个脚本必须支持--list选项和--host选项。其中,--list选项以json的格式返回以组名为key,服务器列表为value的数据。--host返回一个字典,该字典中包含了这个服务器的详细信息,如下:

[heboan@c1 ~]$ python hosts.py --list
{"test": ["192.168.88.2", "192.168.88.3"], "uat": ["192.168.88.4"]
}
[heboan@c1 ~]$
[heboan@c1 ~]$ python hosts.py --host='192.168.88.2'
{"ansible_port": "22", "asnible_user": "heboan"
}

接下来我们来看下如何编写这样一个动态获取服务器的脚本。假设。我们的CMDB数据库中,存在一张名为hosts的表:

CREATE TABLE  hosts(
id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
host varchar(15) DEFAULT NULL,
groupname varchar(15) DEFAULT NULL,
username varchar(15) DEFAULT NULL,
port int(11) DEFAULT NULL
);INSERT INTO hosts values(null, '192.168.88.2', 'test', 'heboan', 22);
INSERT INTO hosts values(null, '192.168.88.3', 'test', 'heboan', 22);
INSERT INTO hosts values(null, '192.168.88.3', 'uat', 'heboan', 22);

下面是动态获取服务器列表的程序

#!/usr/bin/python
#_*_ coding: UTF-8 _*_import argparse
import json
from collections import defaultdict
from contextlib import contextmanager
import pymysqldef to_json(in_dict):return json.dumps(in_dict, sort_keys=True, indent=4)@contextmanager
def get_conn(**kwargs):conn = pymysql.connect(**kwargs)try:yield connfinally:conn.close()def parse_args():parser = argparse.ArgumentParser(description='Heboan Inventory Module')group = parser.add_mutually_exclusive_group(required=True)group.add_argument('--list', action='store_true', help='list active servers')group.add_argument('--host', help='List details about the specific host')return parser.parse_args()def list_all_hosts(conn):hosts = defaultdict(list)with conn as cur:cur.execute('select * from hosts')rows = cur.fetchall()for row in rows:no, host, group, user, port = rowhosts[group].append(host)return hostsdef get_host_detail(conn, host):details = {}with conn as cur:cur.execute("select * from hosts where host='{}'".format(host))rows = cur.fetchall()if rows:no, host, group, user, port = rows[0]details.update(asnible_user=user, ansible_port=port)return detailsdef main():parser = parse_args()with get_conn(host='127.0.0.1', user='root', password='root', db='cmdb' ) as conn:if parser.list:hosts = list_all_hosts(conn)print(to_json(hosts))else:details = get_host_detail(conn, parser.host)print(to_json(details))if __name__ == '__main__':main()

hosts.py

为了让我们动态获取服务器列表程序能够应用在Ansible中,需要为hosts.py加上可执行权限。其他使用方式和普通的hosts文件一模一样。

定义服务器变量

...

转载于:https://www.cnblogs.com/sellsa/p/9946448.html

第2天:Ansible-Inventory管理相关推荐

  1. Ansible inventory文件详解

    我们知道inventory主要用来管理managed nodes(host),定义playbook要在哪些远程机器上执行.如果不熟悉inventory的角色的,可以参考ansible playbook ...

  2. Ansible系列-基础篇-Ansible Inventory的合理化配置

    欢迎关注个人公众号 DailyJobOps 原文地址:Ansible系列-基础篇-Ansible Inventory的合理化配置 这里写目录标题 Ansible Inventory内置参数 Inven ...

  3. ansible Inventory

    什么是 Inventory? ​ Ansible 可同时操作属于一个组的多台主机,组合主机之间的关系通过 Inventory 文件配置,默认的路径为 /etc/ansible/hosts 案例: 19 ...

  4. ansible模块管理与主机清单配置

    文章目录 一. ansible简介 二 . Ansible 部署 及 模块篇实操 2.1 ansible 软件部署 2.2 设置代理登录,免去密码交互 2.3 各模块详解 ------command模 ...

  5. Ansible Inventory指北进阶

    Ansible的inventory文件定义了它要操作的一些主机,它可以通过inventory对这些主机进行操控.默认的inventory文件是/etc/ansible/hosts .也可通过-i选项, ...

  6. Ansible Inventory内置参数

    参考文章:Ansible Inventory内置参数 - 简书 How to build your inventory - Ansible Documentation /etc/ansible/hos ...

  7. Ansiable常用模块讲解,以及Ansible的管理方式

    文章目录 一.Ansiable的管理方式 1. Ansible的管理方式 二.Ansible常用模块 1.command模块 2.shell模块 3.script模块 4.copy模块 5.fetch ...

  8. Ansible的Inventory管理

    Ansible将可管理的服务器集合成为Inventory,Inventory的管理便是服务器的管理. hosts文件的位置: /etc/ansible/hosts 在命令行通过-i参数指定 通过/et ...

  9. Ansible — Inventory 清单文件

    目录 文章目录 目录 Inventory 清单文件 Groups 与 Hosts 对象 嵌套 Groups Hostname 通配符 Inventory 清单文件的参数说明 Inventory 清单文 ...

  10. ansible inventory 主机清单配置

    文章目录 环境介绍 ansible ssh配置 操作 测试 /etc/hosts 配置 Inventory文件 主机与组 主机变量.组变量 把一个组变成另一个组的子成员 变量太多了,不好管理怎么办?来 ...

最新文章

  1. weblogic 修改控制台密码
  2. QtCreater的安装路径
  3. 解决idea控制台打印乱码问题
  4. 迭代器报“vector iterators incompatible”错的几种可能
  5. poj 1979 Red and Black(BFS)
  6. 使用GDI+实现圆形进度条控件的平滑效果
  7. Win10 + Python + MXNet + VS2015配置
  8. 对c语言字符数组描述错误的是,下述对C语言字符数组的描述中错误的是( )。
  9. jsp页面页面post传值_在Js页面通过POST传递参数跳转到新页面详解
  10. Linux(debian)操作基础(一)之压缩、解压缩
  11. 政府信息化与电子政务
  12. 仓库管理系统论文+源程序+执行程序,
  13. 物联网终端安全系列(之三) -- 物联网终端安全解决方案概述
  14. java斐波黄金分割_斐波那契(黄金分割)
  15. 女程序员写代码被骂,把领导萌死,成功治愈“选择困难症”
  16. 剧本创作时的标准格式,让你的剧本轻松得到制片公司青睐
  17. 读书:《有无之境:王阳明哲学的精神》
  18. 【学习笔记】《基于φ-OTDR的分布式扰动传感系统定位算法研究-北交-通信与信息系统-吴》重点笔记
  19. jcseg 配置详解
  20. CSS 设置页面缩放

热门文章

  1. SAP Netweaver ECATT介绍
  2. SAP S4HANA里关于生产订单的一些重要数据库表
  3. 一家互联网电商公司面向对象的编程笔试题目
  4. ant design vue input change_vue中关于$emit用法的笔记
  5. wps 模拟分析 规划求解_【网友来稿】利用Excel求解线性规划问题
  6. excel合并计算_【Excel】合并计算和模拟分析的应用
  7. 二叉树的前序遍历Python解法
  8. python 找到目录下文件名规则_Python学习第162课--PATH变量值修改以及模糊查找文件名...
  9. 多设备同步表数据_利用Excel数据透视表解决两份报表数据不同步问题
  10. mysql根据id删除数据sql语句_sql delete根据id删除数据库