ansible ssh 到 json_query 循环响应值 [英] ansible ssh to json_query response values in loop

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

问题描述

团队,我收到来自 json_query 的响应,这是一个 dict key:value,我想遍历所有值并为每个值运行 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 并在所有主机 1-3 的 ssh 命令中使用它

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 考虑这个问题的方式是通过 add_host::


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 节点列表并在剧本中使用它们,您可以使用 would 动态清单脚本

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

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

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