
以下任務:
- name: Download Jfrog Artifcats
ansible.windows.win_shell: |
$ENV:JFROG_CLI_OFFER_CONFIG="false"
jfrog rt download ...
when: ???
應該只為位於群組center
中的計算機運行appservers
---
all:
children:
root:
children:
center:
children:
appservers:
hosts:
vm1.domain.com:
qservers:
hosts:
vm2.domain.com:
dbservers:
hosts:
vm3.domain.com:
mobilefarms:
hosts:
children:
gateways:
hosts:
south:
children:
brooklyn:
hosts:
vm4.domain.com:
children:
clients:
hosts:
vm5.domain.com:
vm6.domain.com:
north:
children:
new_york:
hosts:
vm8.domain.com:
children:
clients:
hosts:
vm9.domain.com:
when
為了實現這個目標,我應該輸入什麼作為條件?另外,這個配置選項背後的原理是什麼?
答案1
為了在任務、播放或區塊的條件中使用群組成員身份,您可以使用以下格式:
when: inventory_hostname in groups["<group name>"]
具體到你最初的問題:
when: inventory_hostname in groups["appservers"]
要存取 下的所有機器north
,您只需將其變更為:
when: inventory_hostname in groups["north"]
關於您的後續澄清(在特定「位置」指定一個群組),由於群組名稱在ansible中必須是唯一的,因此無需區分哪個 appservers
您所指的群組appservers
只能位於一個位置。
如果您嘗試建立兩個appservers
群組,ansible 引擎實際上只會解析第一個群組;任何後續的同名組都將被忽略。因此,如果您計劃(將來)appservers
在建立一個群組north
,然後在 下方建立一個appservers
群組south
,您會發現僅包含第一組中的成員。
在ansible中,我們如何實現這一點(我對你的假設可能將來想要),可行的方法是將主機新增到多個群組,如下所示,並適當調整您的限製或條件:
all:
children:
north:
hosts:
a.domain.com:
b.domain.com:
south:
hosts:
y.domain.com:
z.domain.com:
appservers:
hosts:
a.domain.com:
y.domain.com:
dbservers:
hosts:
b.domain.com:
z.domain.com:
在此範例中,如果您想要全部應用程式伺服器,您只需定位appservers
.如果您只想appservers
在該north
地區,那麼您可以將播放限制設為north:&appservers
,或使用條件
when:
- inventory_hostname in groups["appservers"]
- inventory_hostname in groups["north"]
無論如何,我認為您可能需要回顧一下 ansible 中庫存的結構,為此我會推薦使用者指南;各種培訓網站上也有一些很棒的資源,可以提供更詳細的資訊。
有關使用多個組(組合、聯合和排除等)的更複雜定位的更多信息,我建議您查看其他使用者指南。
就我個人而言,最初我認為該設定既乏味又有限,但隨著我越來越熟悉它的使用,我實際上發現它比其他選擇更具動態性。