首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在寄存器变量列表输出上使用failed_when的可选循环

在寄存器变量列表输出上使用failed_when的可选循环
EN

Stack Overflow用户
提问于 2019-12-12 10:53:02
回答 2查看 1.7K关注 0票数 0

队员们,

当我只有一个节点或者只有一个项目时,我有下面的任务可以很好地工作,但是我需要修改它,它适用于存储在注册变量中的列表中返回的所有项目。

代码语言:javascript
复制
      - 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

如何使用loop或with_items对cpu_class_list变量中的所有节点执行此操作?

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

代码语言:javascript
复制
      - 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

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

代码语言:javascript
复制
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",
                    }
                }
             }
        ]
    }
}

建议的解决方案:

代码语言:javascript
复制
      - 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
EN

回答 2

Stack Overflow用户

发布于 2019-12-12 19:14:27

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

请参阅此代码片段来概述逻辑:

代码语言:javascript
复制
- 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

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

代码语言:javascript
复制
$ 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
EN

Stack Overflow用户

发布于 2019-12-13 14:19:09

代码语言:javascript
复制
 #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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59297032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档