在循环中将ssh转换为json_query响应值 [英] ansible ssh to json_query response values in loop

查看:88
本文介绍了在循环中将ssh转换为json_query响应值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

团队,我收到json_query的响应,它是一个dict键:值,我想遍历所有值并为每个值运行ssh命令

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}'

任务[3_validations_on_ssh:调试]

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

要编写的剧本:解析node_name并将其在ssh命令中用于所有1-3台主机

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

错误输出:

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

当我执行循环时,解决方案2也会失败:

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 }}"

输出溶胶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"}

推荐答案

在Ash的正确答案上进行扩展:

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 }}'


另外,除非您确实只是想运行该 one ssh命令,否则ansible关于该问题的思考方式是通过


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

这是一个人为的示例,因为如果您实际上只想获取kubernetes节点列表并在剧本中使用它们,则可以使用

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

这篇关于在循环中将ssh转换为json_query响应值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆