ansible-inventoryホストリスト

序文

次のプレイブックスクリプトを学習する前に、次のプレイブックをよりよく学習するために、いくつかの可変パラメータの基本的な知識を理解する必要があります。

1.ホストインベントリ構成ファイル

  • ansibleのデフォルトのホストリストは/ etc / ansible / hostsファイルです
  • ホストリストは、手動で設定することも、動的インベントリを介して動的に生成することもできます
  • 一般的なホスト名はFQDNを使用します(完全修飾ドメイン名、例:www.baidu.com;ドメイン名:baidu.com)

ansibleホストインベントリの構成ファイル分析

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.インベントリ内の変数

2.1ホスト変数

[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グループ変数

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

2.3グループの入れ子

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

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

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

2.4インベントリ変数パラメータ

インベントリ変数パラメータ(システムに付属する一部の変数、これらのパラメータの名前は変更できません)
ここに写真の説明を挿入

おすすめ

転載: blog.csdn.net/F2001523/article/details/112852452