Detailed Inventory
Ansible system may be implemented according to a plurality of simultaneously Inventory file. The file is located /etc/ansible/hosts
. The actual use of multiple Inventory can write files when used with -i
temporary parameters specified on the line.
This file can not write your own, write more. The allocation of time not only can use local resources, we can use the resources in the cloud.
This file can be written into a variety of formats, commonly used are: the INI , YAML .
Inventory basis: multiple hosts packets
Mean the same thing in the inventory file can INI write can also be used YAML write, but you need to install the corresponding plug-ins.
INI format is this:
mail.example.com
[webservers]
foo.example.com
bar.example.com
[dbservers]
one.example.com
two.example.com
three.example.com
Inside the square brackets is the group name , did not know the name of justice reached see the effect, it is best to let the name reflects: This is what the operating system, which is used to do things, what the general run time.
This file YAML format is written like this:
all:
hosts:
mail.example.com:
children:
webservers:
hosts:
foo.example.com:
bar.example.com:
dbservers:
hosts:
one.example.com:
two.example.com:
three.example.com:
The multiple hosts configured into a plurality of groups
Simultaneously loading system to a plurality of groups, so this effect can be done: a host both a web server , and a database server .
When you can create a group with such an idea:
To doing
Application stack, micro-services. For example: Database Servers , Web Servers .
where is it
Local DNS server which data center in which region or, storage server storage. For example: East , West .
What stage
Development phase, the need to avoid the test line of code. For example: Prod , the Test .
Contains more than three thought YAML for Inventory, the file format can be written like this:
all:
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:
hosts:
foo.example.com:
one.example.com:
two.example.com:
test:
hosts:
bar.example.com:
three.example.com:
It can be seen one.example.com
in the dbservers
group, and also in east
the group, also in prod
the group.
You can also use a nested way to simplify this file:
all:
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:
If you use this method of shorthand, pay attention to the variables represent all hosts it contains.
Multiple hosts use different ports
If there is no host with the default ssh port, with the port after the host name on the line.
If you want to configure the host port number really is not the default port 22 , the port it is best to go with it.
like this:
badwolf.example.com:5309
If the IP hosts are static and do not change, and when the connection needs to be connected through the tunnel, it can hosts configure the look alias file.
INI file with this:
jumper ansible_port=5555 ansible_host=192.0.2.50
别名 端口号 主机名
YAML file with this:
...
hosts:
jumper:
ansible_port: 5555
ansible_host: 192.0.2.50
If the host name is written in a unified mode, it can be abbreviated:
INI format:
[webservers]
www[01:50].example.com
YAML format:
...
webservers:
hosts:
www[01:50].example.com:
For digital pattern matching preamble 0 may be added to or removed as needed, before and after the scope is closed interval , the two values can be taken to the boundary.
You can also be defined range of letters:
[databases]
db-[a:f].example.com
You can also select the connection type and the user of each host :
[targets]
localhost ansible_connection=local
other1.example.com ansible_connection=ssh ansible_user=mpdehaan
other2.example.com ansible_connection=ssh ansible_user=mdehaan
主机 连接类型 连接用户
As mentioned above, these are just a simplified method set in the inventory file and later on we will discuss how to store them in host_vars each file in the directory.
Refers to a variable with a host: the host variable
The host can also be used to refer to variables and variables are now specified in the back of the playbook will be used.
INI format:
[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909
YAML format:
atlanta:
host1:
http_port: 80
maxRequestsPerChild: 808
host2:
http_port: 303
maxRequestsPerChild: 909
With variable refer to multiple hosts: set of variables
Variables can also be applied to a whole group.
INI format:
[atlanta]
host1
host2
[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com
YAML format:
atlanta:
hosts:
host1:
host2:
vars:
ntp_server: ntp.atlanta.example.com
proxy: proxy.atlanta.example.com
Set of variables Inheritance: sub-set of variables
Subgroups may be arranged, and the use of a variable subset
INI format | YAML format | |
---|---|---|
Subgroups | :children |
children: |
Sub-set of variables | :vars |
vars: |
INI format:
[atlanta]
host1
host2
[raleigh]
host2
host3
[southeast:children]
atlanta
raleigh
[southeast:vars]
some_server=foo.southeast.example.com
halon_system_timeout=30
self_destruct_countdown=60
escape_pods=2
[usa:children]
southeast
northeast
southwest
northwest
YAML format:
all:
children:
usa:
children:
southeast:
children:
atlanta:
hosts:
host1:
host2:
raleigh:
hosts:
host2:
host3:
vars:
some_server: foo.southeast.example.com
halon_system_timeout: 30
self_destruct_countdown: 60
escape_pods: 2
northeast:
northwest:
southwest:
Several default group
There are two default groups: All and ungrouped .
all contain all hosts
ungrouped comprising belongs to all hosts group
Each host belongs to at least two groups
Despite all and ungrouped always existed, but they may be implicit, not appear in group_names group list of classes.