Ansible是一个开源配置管理工具,可以使用它来自动化任务,部署应用程序实现IT基础架构。比如,服务器的初始化配置、安全基线配置、更新和打补丁系统,安装软件包等。
作为一个架构简单但是功能强大的自动化IT工具,我们可以使用它来帮我们完成自动化测试测试环境搭建工作。
关于Ansible的整体介绍与使用,已经在:《自动化运维工具-Ansible》做过介绍,今天给大家介绍一下Ansible的Inventory。
Inventory 介绍
Ansible 从 Inventory 读取列表或组,可同时并发操作这些受控节点或主机。一旦 inventory 被定义,就可以使用正则匹配主机或者组来指定要运行的主机列表 patterns 。
Inventory 主机清单存放在 /etc/ansible/hosts。
可以在命令行使用 -i <path> 指定特定的 inventory 清单;当然,也可以一次指定多个 inventory 清单;还可以使用 pull inventory 的动态获取或者从云主机获取。
Inventory 文件可以有多种格式,取决于你使用什么插件,最常用的格式是 YAML 和 INI。
INI 文件示例:
[dbservers]
one.example.com two.example.com three.example.com |
注:括号中的标题是组名,用于对主机进行分类,用于确定什么时间、什么目的、相对哪些主机做什么事情。
对应的YAML文件示例:
all:
hosts: mail.example.com: 10.0.0.18: children: webservers: hosts: foo.example.com: bar.example.com: dbservers: hosts: one.example.com: two.example.com: three.example.com: |
可以把一台主机放在多个组中,还可以使用嵌套组来简化此清单中的分组。
示例:
hosts:
mail.example.com: children: webservers: hosts: foo.example.com: bar.example.com: dbservers: hosts: one.example.com: two.example.com: three.example.com: east: hosts: foo.example.com: one.example.com: two.example.com: west: hosts: bar.example.com: three.example.com: prod: children: east: test: children: west: |
如果有许多具有相似模式的主机,则可以将它们添加为一个范围,而不必分别列出每个主机名。
INI 示例:
[databases]
db-[a:f].example.com |
YAML 示例:
webservers:
hosts: www[01:50].example.com: |
Inventory 变量
可以直接在 Inventory 清单中定义的 host 或 group 变量。可以直接添加 host 或 group 到 Inventory 文件中。如果数据量太大时,可以将变量和 host group 分离成独立的文件。
给单台主机设置变量示例:
[atlanta]
host1 http_port=80 maxRequestsPerChild=808 host2 http_port=303 maxRequestsPerChild=909 |
在 Inventory 中定义别名:
jumper ansible_port=5555 ansible_host=192.0.2.50 |
注:执行 Ansible 对 “”jumper”” 主机执行命令时,会连接 192.0.2.50 的 5555 端口。这种方式仅适用于通过静态 IP 的主机,或者通过隧道连接的主机。
如果组中的所有主机共享一个变量值,则可以一次将该变量应用于整个组。
示例:
[atlanta:vars]
ntp_server=ntp.atlanta.example.com proxy=proxy.atlanta.example.com |
组变量是一次将变量同时应用于多个主机的便捷方法。但是,在执行之前,Ansible始终将变量(包括 Inventory 清单变量)展平到主机级别。如果该主机是多个组的成员,则 Ansible 将从所有这些组中读取变量值。如果同一主机在不同的组中被赋予不同的变量值,则 Ansible 会根据内部规则来选择要使用的值。
多个 Inventory
Ansible 可以使用提供多个 Inventory 选项的命令行或者配置 ANSIBLE_INVENTORY 的方式,同时使用多个 Inventory 源 ( 目录, 动态 Inveory 脚本 或者 Inventory 插件提供的文件 )。该功能针对相互独立的环境非常有帮助,比如你想同时对测试环境和生产环境执行某操作。
同时使用两个源的命令执行方式如下:
ansible-playbook get_logs.yml -i staging -i production |
还可以合并组合目录下的多个 Inventory 清单和不同类型的 Inventory 来创建新清单。这对于组合静态和动态主机并将它们作为一个 Inventory 清单进行管理很有用。
以下 Inventory 清单结合了清单插件源,动态清单脚本,和带有静态主机的文件:
inventory/
openstack.yml |
可以像下面这样指定一个 Inventory 清单目录:
ansible-playbook example.yml -i inventory |
Inventory 参数说明
通过设置下面的参数,可以控制 ansible 与远程主机的交互方式,下面是常用参数:
ansible_connection 与主机的连接类型.比如:local, ssh 或者 paramiko. Ansible 1.2 以前默认使用 paramiko.1.2 以后默认使用 ‘smart’,’smart’ 方式会根据是否支持 ControlPersist, 来判断’ssh’ 方式是否可行. |
ansible_ssh_host | |
ansible_ssh_port | |
ansible_ssh_user | |
ansible_ssh_pass |
ansible_ssh_private_key_file | |
ansible_ssh_common_args | |
ansible_sftp_extra_args | |
ansible_scp_extra_args | |
ansible_ssh_extra_args | |
ansible_ssh_pipelining | |
ansible_ssh_executable (added in version 2.2) |
ansible_shell_type | |
ansible_python_interpreter | |
ansible_*_interpreter | |
ansible_shell_executable |
知乎:@TestOps云层
● 扫码关注我们
《TestOps测试运维架构》公开课录屏
接口&UI自动化测试教程
性能测试教程
测试环境及工具教程
提升生活幸福感,你需要这10件小物
课程试听&索引
我知道你在看哟
声明:文中观点不代表本站立场。本文传送门:https://eyangzhen.com/187030.html