ansible ssh to json_query response values in loop

跟風遠走 提交于 2019-12-11 16:58:22

问题


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

回答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




回答2:


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



来源:https://stackoverflow.com/questions/58498378/ansible-ssh-to-json-query-response-values-in-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!