在寄存器变量列表输出时出现带失败_的可循环

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

Team,

当我只有一个节点或只有一个项目,但是我需要对其进行修改时,下面的任务就可以正常工作,但是对于存储在寄存器变量中的列表中返回的所有项目,我都需要对其进行修改。

      - name: "Fetch all CPU nodes from clusters using K8s beta.kubernetes.io/instance-type"
        k8s_info:
          kind: Node
          label_selectors:
          - "beta.kubernetes.io/instance-type={{ kube_cpu_node_class }}"
          verify_ssl: no
        register: cpu_class_list
        failed_when: not cpu_class_list.resources

我如何对带有循环或with_items的cpu_class_list变量中的所有节点执行此操作?

建议的解决方案,但不起作用

      - name: "Fetch all CPU nodes from clusters using K8s beta.kubernetes.io/instance-type"
        k8s_info:
          kind: Node
          label_selectors:
          - "beta.kubernetes.io/instance-type={{ kube_cpu_node_class }}"
          verify_ssl: no
        register: cpu_class_list
        failed_when: not {{ item }}
        with_items: cpu_class_list.resources

具有两个节点的示例输出在下面

services-pre-install-checks : debug] 


ok: [localhost] => {
    "cpu_class_list": {
        "changed": false,
        "deprecations": [
            {
                "msg": "The 'k8s_facts' module has been renamed to 'k8s_info'",
                "version": "2.13"
            }
        ],
        "failed": false,
        "failed_when_result": false,
        "resources": [
            {
                "apiVersion": "v1",
                "kind": "Node",
                "metadata": {
                    "annotations": {
                        "volumes.kubernetes.io/controller-managed-attach-detach": "true"
                    },
                    "creationTimestamp": "2019-07-16T00:23:27Z",
                    "labels": {
                        "nodeType": "cpu"
                    },
                    "name": "node1",
                    "nodeInfo": {
                        "architecture": "amd64",
                    }
                }
            },


{
             {
                "apiVersion": "v1",
                "kind": "Node",
                "metadata": {
                    "annotations": {
                        "volumes.kubernetes.io/controller-managed-attach-detach": "true"
                    },
                    "creationTimestamp": "2019-07-16T00:23:27Z",
                    "labels": {
                        "nodeType": "cpu"
                    },
                    "name": "node2",
                    "nodeInfo": {
                        "architecture": "amd64",
                    }
                }
             }
        ]
    }
}

建议的解决方案:

      - name: "Fetch all CPU nodes from clusters using K8s beta.kubernetes.io/instance-type"
        k8s_facts:
          kind: Node
          label_selectors:
          - "beta.kubernetes.io/instance-type={{ kube_cpu_node_class }}"
          verify_ssl: no
        register: cpu_class_list
        failed_when: not cpu_class_list.resources 

#above to fail when none of the nodes has label, that is resources list is empty.

#below to fail when any of the nodes has no label

       - debug:
          msg: "{{ item.metadata.labels.nodeType }}"
        loop: "{{ cpu_class_list.resources }}"
        loop_control:
          label: "{{ item.metadata.name }}"
        failed_when: not item.metadata.labels.nodeType
ansible ansible-2.x ansible-inventory
1个回答
0
投票

我建议将其分为两个不同的任务。首先,注册变量,然后使用fail ansible模块(docs)检查var,如果满足条件则失败。

请参见以下代码段概述逻辑:

- hosts: localhost
  vars:
    test: # test array
      - fail: false
      - fail: false
      - fail: true
      - fail: false
  tasks:
    - name: iterate and fail
      fail:
        msg: "Fail as requested"
      with_items: "{{ test }}"
      when: item.fail

运行此命令,将输出以下内容:

$ ansible-playbook failing.yml
PLAY [localhost] ***********************

TASK [Gathering Facts] *************************
ok: [localhost]

TASK [iterate and fail] **************************
skipping: [localhost] => (item={u'fail': False}) 
skipping: [localhost] => (item={u'fail': False}) 
failed: [localhost] (item={u'fail': True}) => {"ansible_loop_var": "item", "changed": false, "item": {"fail": true}, "msg": "Failed as requested"}
skipping: [localhost] => (item={u'fail': False}) 

PLAY RECAP *****************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

希望这会有所帮助!


0
投票

#在所有节点都没有标签,即资源列表为空时失败。

        k8s_facts:
          kind: Node
          label_selectors:
          - "beta.kubernetes.io/instance-type={{ kube_cpu_node_class }}"
          verify_ssl: no
        register: cpu_class_list
        failed_when: not cpu_class_list.resources 



#below to fail when any of the nodes has no label

       - debug:
          msg: "{{ item.metadata.labels.nodeType }}"
        loop: "{{ cpu_class_list.resources }}"
        loop_control:
          label: "{{ item.metadata.name }}"
        failed_when: not item.metadata.labels.nodeType
© www.soinside.com 2019 - 2024. All rights reserved.