---
title: "Ansible主机与组的定义Inventory"
author: "2002wmj"
date: "2016-03-31T19:27:51Z"
tags: []
categories: ["编程语言"]
slug: "ansible-inventory"
draft: false
lastmod: "2016-03-31T19:27:51Z"
id: 2287865
url: "https://2002wmj.iteye.com/blog/2287865"
---

## Inventory

Ansible的Inventory文件，可以理解为saltstack中的salt-key中的所有minion的列表以及用户自定义的nodegroup的概念，默认情况下这个文件是/etc/ansible/hosts，到目前为止，以上仅仅是Inventory文件的小小一部分作用，其实他的作用远远不止这些:)

## Hosts and Groups

对于/etc/ansible/hosts最简单的定义格式像下面：

mail . lightclooud . com

\[ webservers ]

web1 . lightcloud . com

web2 . lightcloud . com

\[ cloudservers ]

cloud1 . lightcloud . com

cloud2 . lightcloud . com

- 想当然，标准的FQDN自然表示的是主机的主机名或者域名
- 中括号中的名字代表组名，你可以根据你自己的需求将庞大的主机分成具有标识的组。

如果某些主机的SSH运行在自定义的端口上，ansible使用Paramiko进行ssh连接时，不会使用你SSH配置文件中列出的端口，但是如果修改ansible使用openssh进行ssh连接时将会使用：

badwolf . lightcloud . com : 5309

假如你想要为某些静态IP设置一些别名，类似于SaltStack中minion配置文件中id的参数配置。你可以这样做：

jumper ansible\_ssh\_port = 5555 ansible\_ssh\_host = 192.168.1.50

假如你有很多主机遵循某一种模式，你还可以这样来表示他们：

\[ webservers ]

web \[ 1 : 50 ] . lightcloud . com

- 表示从web1到web50，共计50台主机

当然你还可以通过字母来定义可变的范围，例如：

\[ database ]

db - \[ a : f ] . lightcloud . com

对于每台主机的连接类型，连接用户等这些你都可以完全自定义，例如：

\[ targets ]

localhost ansible\_connection = local

cloud1 . lightcloud . com ansible\_connection = ssh ansible\_ssh\_user = light

cloud2 . lightcloud . com ansible\_connection = ssh ansible\_ssh\_user = cloud

而对于上面的定义方式，只能算是我们针对每台主机的一个快速定义，随后将会讲述如何在host\_vars目录下，以单个文件存储它们。

## Host Variables

如上面提到，我们目前可以轻易的给某台或多台主机赋予变量，提供给后面的playbooks使用：

\[ lightcloud ]

web1 http\_port = 202 maxRequestsPerChild = 404

web2 http\_port = 303 maxRequestsPerChild = 606

## Groups Variables

变量也可以通过组名，应用到组内的所有成员：

\[ lightcloud ]

host1

host2

\[ lightcloud : vars ]

ntp\_server = ntp . lightcloud . com

proxy = proxy . lightcloud . com

## Groups of Groups, and Group Variables

主机组可以包含主机组，主机的变量可以通过继承关系，继承到最高等级的组的变量。定义主机组之间的继承关系我们使用”:children”来表示：

\[ atlanta ]

host1

host2

\[ raleigh ]

host2

host3

\[ southeas : children ]

atlanta

raleigh

\[ southeas : vars ]

some\_server = foo . southeast . example . com

halon\_system\_timeout = 30

self\_destruct\_countdown = 60

escape\_pods = 2

\[ usa : children ]

southeast

northeast

southwest

northwest

## Splitting Out Host and Group Specific Data

Ansible并不建议将主机的变量都存储在Inventory文件中，这些变量也可以存储在单个文件中，而这些变量文件必须遵循YAML语法，假设Inventory文件没有被修改，路径如下：

/ etc / ansible / hosts

如果主机的名字“foosball”,两个组的名字分别是“raleigh”，“raleigh”，以下路径的yaml文件中的变量将会应用到对应的主机上：

/ etc / ansible / group\_vars / raleigh

/ etc / ansible / group\_vars / webservers

/ etc / ansible / host\_vars / foosball

Tip：在Ansible 1.2或更高版本中，group *vars和host* vars目录可以存在于playbook目录或者Inventory目录，如果两个目录都存在同一主机的相同定义，那么playbook目录将会第二次被加载，也就是说playbook中定义的将会覆盖Inventory中对应的定义

## List of Behavioral Inventory Parameters

Ansible内置了一些关于连接主机的变量，设置以下变量控制ansible与远程主机：

ansible\_ssh\_host

   ansible 通过 ssh 连接的 IP 或者 FQDN

ansible\_ssh\_port

   SSH 连接端口

ansible\_ssh _ user

   默认 SSH 连接用户

ansible\_ssh\_pass

   SSH 连接的密码（这是不安全的， ansible 极力推荐使用 -- ask - pass 选项或使用SSH keys ）

ansible\_sudo\_pass

   sudo 用户的密码

ansible\_connection

   SSH 连接的类型： local , ssh , paramiko ，在 ansible 1.2 之前默认是 paramiko ，后来智能选择，优先使用基于 ControlPersist 的 ssh （支持的前提）

ansible\_ssh\_private\_key\_file

   SSH 连接的公钥文件

ansible\_shell _ type

   指定主机所使用的 shell 解释器，默认是 sh ，你可以设置成 csh , fish 等 shell 解释器

ansible\_python _ interpreter

   用来指定 python 解释器的路径

ansible \\ _ \\ * \\ \_interpreter

   用来指定主机上其他语法解释器的路径，例如 ruby ， perl 等

例如：

some\_host         ansible\_ssh\_port = 2222      ansible\_ssh\_user = manager

aws\_host           ansible\_ssh\_private\_key\_file = / home / example / . ssh / aws . pem

freebsd\_host       ansible\_python\_interpreter = / usr / local / bin / python

ruby\_module\_host   ansible\_ruby\_interpreter = / usr / bin / ruby . 1.9.3