在寄存器变量列表输出上时,ansible循环带有失败的_

在寄存器变量列表输出上时,ansible循环带有失败的_,ansible,ansible-2.x,ansible-inventory,Ansible,Ansible 2.x,Ansible Inventory,团队 当我只有一个节点或只有一个项时,我有下面的任务可以正常工作,但我需要修改它,以便为存储在寄存器变量中的列表中返回的所有项工作 - name: "Fetch all CPU nodes from clusters using K8s beta.kubernetes.io/instance-type" k8s_info: kind: Node label_selectors: - "beta.kuberne

团队

当我只有一个节点或只有一个项时,我有下面的任务可以正常工作,但我需要修改它,以便为存储在寄存器变量中的列表中返回的所有项工作

      - 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
如何对cpu_类_列表变量中的所有节点执行此操作,该变量包含循环或_项

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

      - 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

我建议将其分为两个不同的任务。首先,注册变量,然后使用
fail
ansible模块()检查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   


希望这有帮助

我在您的示例任务和示例输出中都没有看到任何循环。只是更新了我的示例输出,在资源列表中显示了两个节点node1和node2。您能否根据我的任务提出建议,将您的想法映射到您的脑海中有点混乱。不过我还在努力。。
 #to fail when none of the nodes has label, that is resources list is empty.

     - 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 



#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