0

Team,

Using single full defined string in with_item I have my task running fine. However, at scale i would like to loop with string inside with_items changing. any hints?

      - 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=e1.xlarge"

          verify_ssl: no
        register: cpu_class_list
        failed_when: cpu_class_list == ''

output:

ok: [localhost] => {
    "nodes_class_label": [
        {
            "instanceType": "e1.xlarge,
            "nodeType": "cpu",
            "node_name": "hostA"
        },
        {
            "instanceType": "e1.xlarge,
            "nodeType": "cpu",
            "node_name": "hostB"
        }
    ]
}


I would like to pull all the nodes matching any name with wildcard.

          label_selectors:
          - "beta.kubernetes.io/instance-type=e1.xlarge"
          - "beta.kubernetes.io/instance-type=f1.xlarge"
          - "beta.kubernetes.io/instance-type=g1.xlarge"

expected output:

list all e1 label nodes output
list all f1 label nodes output
list all g1 label nodes output

my attempted solution:


  - 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=*.xlarge"

      verify_ssl: no
    register: cpu_class_list
    failed_when: cpu_class_list == ''


AhmFM
  • 1,552
  • 3
  • 23
  • 53

1 Answers1

0

Unfortunately this is not possible. It is a limitation based on the implementation of the Kubernetes API, unrelated to Ansible or the k8s_facts module.

There are a couple of workarounds, but ultimately I think using a set-based selector is your best option. This would look similar to:

- k8s_facts:
    kind: Node
    label_selectors:
      - "beta.kubernetes.io/instance-type in (e1.xlarge, f1.xlarge, g1.xlarge)"

You should also be able to pull the instance types out into a variable for code readability and maintenance purposes.

The other option is to just loop over the k8s_facts task for each instance type, which I'm guessing you have already considered: "beta.kubernetes.io/instance-type={{ item }}".

Finally, one of your examples in the question will not work:

label_selectors:
  - "beta.kubernetes.io/instance-type=e1.xlarge"
  - "beta.kubernetes.io/instance-type=f1.xlarge"
  - "beta.kubernetes.io/instance-type=g1.xlarge"

This is looking for nodes that meet all of those criteria (i.e. e1.xlarge && f1.xlarge && g1.xlarge), which will always be none.

Matt P
  • 2,452
  • 1
  • 12
  • 15