Ansible assets

1. Static assets

It is itself a text file, a file with a format similar to INI, which has fields ([]).
By default, Ansible assets are located at /etc/ansible/hosts. The pip installation may not have this file, you can manually create one yourself.

2. Asset files can be customized
Example:

# cat inventory.ini
1.1.1.1
2.2.2.2
3.3.3.[1.15]
test01.csdn.com
test03.csdn.com

[webservers]
192.168.1.2
192.168.1.3

[dbdb_servers]
182.168.2.2
192.168.2.3

[all_servers]
[all_servers:children]
webservers
dbdb_servers

In the Ansible asset file file, it can exist in the form of IP address or host name. If the asset is continuous, it can be expressed in the form of [stat:end], or the server can be defined into groups, and there can be an inheritance relationship between groups.

3. Verification

例举出所有资产
# ansible all -i inventory.ini --list-hosts
  hosts(29):
    1.1.1.1
    2.2.2.2
    3.3.3.1
    ......
例举出选定资产
# ansible web_servers -i inventory.ini --list-hosts
 hosts(2):
  192.168.1.2
  192.168.1.3

4. Asset Selection

选择一台或几台服务器
# ansible 1.1.1.1 -i inventory.ini --list-hosts
  hosts(1):
   1.1.1.1
   
 选择一组服务器
# ansible web_servers -i inventory.ini --list-hosts
  hosts(2):
    192.168.1.2
    192.168.1.3
    
使用*匹配
# ansible 3.3.3.1* -i inventory.ini --list-hosts
 hosts(7):
  3.3.3.13
  3.3.3.10
  3.3.3.11
  3.3.3.12
  ........

逻辑匹配
两个组内的所有主机
# ansible 'web_server:db_servers' -i inventory.ini --list-hosts
  hosts(4):
   192.168.1.2
   192.168.1.3
   192.168.2.2
   192.168.2.3

两个组共有的主机
# ansible 'web_servers:&db_servers' -i inventory.ini --list-hosts
 hosts(1):
  192.168.1.5

在web_servers中,但是不在db_servers中
# ansible 'web_servers:!db_servers' -i inventory.ini --list-hosts
  hosts(2):
   hosts(2):
    192.168.1.2
    192.168.1.3

Guess you like

Origin blog.csdn.net/qq_44451165/article/details/130360691