Inventory
Inventory簡單來說就是你的伺服器清單,當中包括了伺服器位址和連接設定。
A fully-featured inventory file can serve as the source of truth for your network. Using an inventory file, a single playbook can maintain hundreds of network devices with a single command. This page shows you how to build an inventory file, step by step.
Ansible 2.8 Documentation
沒錯,只要你的Inventory file設定好,所有在Inventory中的伺服器都任你指揮!
基本的Inventory可以這樣設定:
# you can specify the user and ssh port number of this server
# Inventory Parameters
# ansible_connection=ssh/winrm/local
# ansible_port=22
# ansible_user=root/administrator
# ansible_ssh_pass=password
web1 ansible_host=webserver.com ansible_connection=ssh ansible_user=webuser ansible_ssh_pass=webPass!
localhost ansible_connection=local
Parameters
Description
Specification
ansible_connection
連接其他伺服器或本機的方式
ssh,winrm,local
ansible_port
連接伺服器的連接埠號碼
1-65536(default:22)
ansible_user
連接伺服器的用戶
username
ansible_ssh_paa
使用ssh連接伺服器時用戶的密碼
password
ansible_password
使用winrm連接伺服器時用戶的密碼
password
上面的例子指定了本機及另一台伺服器的設定,然後你就能使用playbook開始自動化你的工作了!
如果你有好多台伺服器,你亦可以做一份複雜的inventory來實現不同的群組設定。以下是一個例子:
# Web Servers
web_node1 ansible_host=web01.xyz.com ansible_connection=winrm ansible_user=administrator ansible_password=Win$Pass
web_node2 ansible_host=web02.xyz.com ansible_connection=winrm ansible_user=administrator ansible_password=Win$Pass
web_node3 ansible_host=web03.xyz.com ansible_connection=winrm ansible_user=administrator ansible_password=Win$Pass
# DB Servers
sql_db1 ansible_host=sql01.xyz.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Lin$Pass
sql_db2 ansible_host=sql02.xyz.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Lin$Pass
# Groups
[db_nodes]
sql_db1
sql_db2
[web_nodes]
web_node1
web_node2
web_node3
[boston_nodes]
sql_db1
web_node1
[dallas_nodes]
sql_db2
web_node2
web_node3
[us_nodes:children]
boston_nodes
dallas_nodes

這個例子中,webserver都是以Windows作為OS運作,而且都在web_nodes群組;資料庫則使用Linux系統,歸類在db_nodes。而它們亦有以地方作為類別分組,sql_db1及web_node1在boston_nodes中;其餘則在dallas_nodes當中,而最後的us_nodes就包括兩個地方的nodes,即是所有的伺服器都在us_nodes當中。
Last updated
Was this helpful?