0

Team, I have response from json_query which is a dict key:value and i would like to iterate over all values and run ssh command for each value

Below gets me list of all nodes

- name: "Fetch all nodes from clusters using K8s facts"
k8s_facts:
  kubeconfig: $WORKSPACE
  kind: Node
  verify_ssl: no
register: node_list

- debug:
  var: node_list | json_query(query)
vars:
  query: 'resources[].{node_name: metadata.name, nodeType: metadata.labels.nodeType}'

TASK [3_validations_on_ssh : debug]

ok: [target1] => {
    "node_list | json_query(query)": [
        {
            "nodeType": null,
            "node_name": "host1"
        },
        {
            "nodeType": "gpu",
            "node_name": "host2"
        },
        {
            "nodeType": "gpu",
            "node_name": "host3"
        }
    ]
}

playbook to write: parse node_name and use that in ssh command for all hosts 1-3


- name: "Loop on all nodeNames and ssh."
shell: ssh -F ~/.ssh/ssh_config bouncer@{{ item }}.internal.sshproxy.net "name -a"
register: ssh_result_per_host
failed_when: ssh_result_per_host.rc != 0
with_item: {{ for items in query.node_name }}
- debug:
  var: ssh_result_per_host.stdout_lines

error output:

> The offending line appears to be:
        failed_when: ssh_result_per_host.rc != 0
        with_item: {{ for items in query.node_name }}
                    ^ here

solution 2 also fails when i do loop:

          shell: ssh -F ~/.ssh/ssh_config bouncer@{{ item.metadata.name }}.sshproxy.internal.net "name -a"
        loop: "{{ node_list.resources }}"
        loop_control:
          label: "{{ item.metadata.name }}"

output sol 2:

failed: [target1] (item=host1) => {"msg": "Invalid options for debug: shell"}
failed: [target1] (item=host2) => {"msg": "Invalid options for debug: shell"}
fatal: [target1]: FAILED! => {"msg": "All items completed"}
AhmFM
  • 1,552
  • 3
  • 23
  • 53

2 Answers2

1

To expand on Ash's correct answer:

  - name: "Fetch all nodes from clusters using K8s facts"
    k8s_facts:
      kubeconfig: $WORKSPACE
      kind: Node
      verify_ssl: no
    register: node_list

  - set_fact:
      k8s_node_names: '{{ node_list | json_query(query) | map(attribute="node_name") | list }}'
    vars:
      query: 'resources[].{node_name: metadata.name, nodeType: metadata.labels.nodeType}'

  - name: "Loop on all nodeNames and ssh."
    shell: ssh -F ~/.ssh/ssh_config bouncer@{{ item }}.internal.sshproxy.net "name -a"
    register: ssh_result_per_host
    with_items: '{{ k8s_node_names }}'

Separately, unless you quite literally just want to run that one ssh command, the way ansible thinks about that problem is via add_host::

- hosts: localhost
  connection: local
  gather_facts: no
  tasks:
  # ... as before, to generate the  "k8s_node_names" list
  - add_host:
      hostname: '{{ item }}.internal.sshproxy.net'
      groups:
      - the_k8s_nodes
      ansible_ssh_username: bouncer
      # whatever other per-host variables you want
    with_items: '{{ k8s_node_names }}'

# now, run the playbook against those nodes
- hosts: the_k8s_nodes
  tasks:
  - name: run "name -a" on the host
    command: name -a
    register: whatever

This is a contrived example, because if you actually wanted just to get the list of kubernetes nodes and use those in a playbook, you use would a dynamic inventory script

mdaniel
  • 31,240
  • 5
  • 55
  • 58
  • Thanks a lot. this was super. I just tried 1st sol and will try 2nd next. – AhmFM Oct 23 '19 at 06:04
  • I have a followup to this: how can i map multiple attributes? like ``` k8s_node_names: '{{ node_list | json_query(query) | map(attribute=("node_name", "nodeType")) | list }}'``` – AhmFM Oct 24 '19 at 01:03
0

It is not with_item it is with_items
You cannot use with_item: {{ for items in query.node_name }} Save the values to a variable and use the variable in with_items: {{ new_variable }}

Smily
  • 2,308
  • 2
  • 17
  • 41
  • But it’s a dictionary how can I save just the values as a list to a variable ? Would you please mind giving complete syntax, meanwhile I try. – AhmFM Oct 22 '19 at 15:07