Ansible 循环遍历所有 Woker 节点

问题描述 投票:0回答:1

我需要使用正确的注释修补我的 Kubernetes Worker 节点,下面有脚本:

---
  - hosts: all
    gather_facts: false
    remote_user: opc
    environment: 
      KUBECONFIG: "/home/opc/.kube/config"
      PATH: "/usr/local/bin:/usr/bin"
      OCI_CLI_AUTH: instance_principal
    
    tasks:
      - name: Get a list of all the nodes
        kubernetes.core.k8s_info:
          kind: node
        register: node_list

      - name: Display k8s Worker Node Name
        debug:
          var: node_list | json_query(query)
        vars:
          query: 'resources[].{name: metadata.name}'
        register: node
The script runs fine and displays the following output:

TASK [Display k8s Worker Node Name] ***************************************************
ok: [40.233.80.218] => {
"node_list | json_query(query)": [
     {
      "name": "172.16.107.244"
     },
     {
     ]"name": "172.16.111.95"
     }
]
}

我需要获取两个主机名并在两者上运行 kubectl patch node {{name}} 命令。需要一些关于循环的想法来从 ansible 主机本地执行命令。

kubernetes ansible
1个回答
0
投票

给出测试的简化数据

  node_list:
    resources:
      - metadata:
          name: 172.16.107.244
      - metadata:
          name: 172.16.111.95

迭代“...获取两个主机名并运行 kubectl 补丁...”似乎微不足道

    - debug:
        msg: "kubectl patch node {{ item.metadata.name }}"
      loop: "{{ node_list.resources }}"

给出(删节)

  msg: kubectl patch node 172.16.107.244
  msg: kubectl patch node 172.16.111.95
© www.soinside.com 2019 - 2024. All rights reserved.