ansible-inventory host list

Preface

Before learning the following playbook script, you need to understand some basic knowledge of some variable parameters, so as to better learn the following playbook

1. Host inventory configuration file

  • The default host list of ansible is the /etc/ansible/hosts file
  • The host list can be set manually or dynamically generated through Dynamic Inventory
  • The general host name uses FQDN (fully qualified domain name, such as: www.baidu.com; domain name: baidu.com)

Configuration file analysis in ansible host inventory

vi /etc/ansible/hosts
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups

# Ex 1: Ungrouped hosts, specify before any group headers.

## green.example.com
## blue.example.com
## 192.168.1.1
## 192.168.1.10

# Ex 2: A collection of hosts belonging to the 'webservers' group

## [webservers] #方括号设置组名
## alpha.example.org #定义被监控的主机,可以是主机名也可以是IP地址,主机名需要修改/etc/hosts文件
## beta.example.org
## 192.168.1.100
## 192.168.1.110
##www2.example.org:2222 #冒号后定义远程连接端口,默认是ssh的22端口

如果是名称类似的主机,可以使用列表的方式标识各个主机,这边定义了ssh远程节点的用户与密码,可以直接实现免密登录
[webserver]
www[01:50].example.org ansible_ssh_user=root ansible_ssh_pass=123456

[dbbservers]
db-[a:f].example.org 1/支持匹配a b c ... f

# If you have multiple hosts following a pattern you can specify
# them like this:

## www[001:006].example.com  #表示有6个节点
例如:  
www001.example.com
www002.example.com
www003.example.com
www004.example.com
www005.example.com
www006.example.com          
这样的形式会产生数据冗余:重复的内容过多,不利于加载与读取数据的时候过慢

# Ex 3: A collection of database servers in the 'dbservers' group

## [dbservers]
##
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57

# Here's another example of host ranges, this time there are no
# leading 0s:

##

2. Variables in Inventory

2.1 Host variables

[webserver]

#节点1中定义的http端口为80,最大连接数为808
www1.magedu.com http_port=80 maxRequestsChild=808

#节点2中定义的http端口为8080,最大连接数为909
www2.magedu.com http_port=8080 maxRequestsChild=909

2.2 Group variables

[servers:vars]
ntp_server=ntp.example.orgnfs_server=nfs.example.org

2.3 Group nesting

[apache]
http1.example.orghttp2.example.org

[nginx]
ngx1.example.org
ngx2.example.org

[webserver:children]   #调用前面的主机名,就相当于调用其中的节点
apache
nginx

2.4 inventory variable parameters

Inventory variable parameters (some variables that come with the system, the names of these parameters cannot be modified)
Insert picture description here

Guess you like

Origin blog.csdn.net/F2001523/article/details/112852452