![](/img/trans.png)
[英]Running Automation on Google Cloud Virtual Instances using Ansible
[英]Getting Cluster Running State Using Ansible
我有一本在 GKE 集群上執行一些任務的劇本。 它可以工作,但時不時地,集群會將 go 轉換為“RECONCILING” state(我們還不知道為什么)。
我想添加一個任務來等待集群 state 在繼續其他任務之前處於“運行”狀態,以避免任務失敗
這有效:
- name: Wait for cluster in RUNNING state
shell:
cmd: |
gcloud container clusters describe {{ cluster_name }} --zone {{ gke_location }} --format json | jq .status --raw-output
retries: 60
delay: 60
register: cluster_state
until: cluster_state.stdout == "RUNNING"
但這相當粗糙,如果可能的話,我想使用google.cloud集合,而不是破解僅在后台使用gcloud
二進制文件的東西
我已經做到了:
- name: Get clusters
gcp_container_cluster_info:
location: "{{ gke_location }}"
project: "{{ project }}"
auth_kind: serviceaccount
service_account_file: "ansible-user.json"
register: cluster_info
這為我提供了我項目中所有集群的詳細資源列表。
但是,根據cluster_info塊上的文檔,我似乎無法根據名稱過濾單個集群,並且由於我無法過濾,因此我不能假設resources[0]
中的集群將是我正在尋找一個——即使它是,也不能保證它會在未來出現。
如何在不使用shell
或command
模塊(如果可能的話)的情況下檢索集群 state(RUNNING、RECONCILING 等)?
更新
我已經能夠從列表中找到我的集群 select,但我似乎無法正確獲取until
子句
這是我已經能夠拼湊起來的 yaml:
- name: Get clusters info
gcp_container_cluster_info:
location: "{{ gke_location }}"
project: "{{ project }}"
auth_kind: serviceaccount
service_account_file: "ansible-user.json"
register: cluster_info
- debug:
msg: "{{ cluster_info.resources | selectattr('name','==',cluster_name) | map(attribute='status') }}"
- name: Wait for cluster to be in RUNNING
gcp_container_cluster_info:
location: "{{ gke_location }}"
project: "{{ project }}"
auth_kind: serviceaccount
service_account_file: "ansible-user.json"
register: cluster_info
retries: 60
delay: 30
until: (cluster_info.resources | selectattr('name','==', cluster_name) | map(attribute='status')) == "RUNNING"
這是 output
TASK [Debug] *******************************************************************
TASK [debug : Get clusters info] ***********************************************
ok: [localhost]
TASK [debug : debug] ***********************************************************
ok: [localhost] => {
"msg": [
"RUNNING"
]
}
TASK [debug : Wait for cluster to be in RUNNING] *******************************
FAILED - RETRYING: [localhost]: Wait for cluster to be in RUNNING (60 retries left).
...
我知道我有兩個gcp_container_cluster_info
塊,這只是為了調試。 如果我能讓這個工作,我只需要一個。
您可以從 output 看到 state 正在使用過濾器正確檢索:
cluster_info.resources | selectattr('name','==',cluster_name) | map(attribute='status')
(如果有更好的方法,請隨時告訴我^_^)
但是,當嘗試在until
子句中使用它時,它似乎總是評估為假,因此永遠不會繼續,即使之前的調試證明過濾器有效。 任何想法為什么?
你幾乎是正確的。 唯一的問題是selectattr
和map
查詢返回一個包含 1 個項目的列表,即
"msg": [
"RUNNING"
]
這就是字符串比較== "RUNNING"
不起作用的原因。 我們可以 (i) 使用索引 0 ( list[0]
) 來獲取列表中的項目或(ii) 使用item in list
。
以下是item in list
的示例:
until: ("RUNNING" in cluster_info.resources | selectattr('name','==', cluster_name) | map(attribute='status'))
正如您所提到的,您可以有 1 個任務, until
條件如下:
- name: Wait for cluster to be in RUNNING
gcp_container_cluster_info:
location: "{{ gke_location }}"
project: "{{ project }}"
auth_kind: serviceaccount
service_account_file: "ansible-user.json"
register: cluster_info
retries: 60
delay: 30
until: ("RUNNING" in cluster_info.resources | selectattr('name','==', cluster_name) | map(attribute='status'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.