从ansible中的ping输出获取数据

Get data from ping output in ansible

从“shell”模块执行 ping 命令后,我无法从寄存器 var 中提取 ip。

ping.yml

---
- name: "Ping computers"
  shell:
    cmd: "ping -c1 -w 2 {{ pinging_host }}"
  register: pingged_host
  ignore_errors: yes
  with_items:
    - 192.168.1.27
    - 192.168.1.42
  loop_control:
    loop_var: pinging_host

- name: "Result ping"
  debug:
    var: pingged_host

- name: " ***Ip  ping"
  debug:
    var: pingged_host.results.value.pinging_host

...

詹金斯的输出:


TASK [ Result ping] **********************

ok: [computer1] => {
    "pingged_host": {
        "changed": true, 
        "failed": true, 
        "msg": "All items completed", 
        "results": [
            {
                "ansible_loop_var": "pinging_host", 
                "changed": true, 
                "cmd": "ping -c1 -w 2 192.168.1.27", 
                "delta": "0:00:00.003363", 
                "end": "2021-02-25 17:08:48.994930", 
                "failed": false, 
                "invocation": {
                    "module_args": {
                        "_raw_params": "ping -c1 -w 2 192.168.1.27", 
                        "_uses_shell": true, 
                        "argv": null, 
                        "chdir": null, 
                        "creates": null, 
                        "executable": null, 
                        "removes": null, 
                        "stdin": null, 
                        "stdin_add_newline": true, 
                        "strip_empty_ends": true, 
                        "warn": true
                    }
                }, 
                "pinging_host": "192.168.1.27", 
                "rc": 0, 
                "start": "2021-02-25 17:08:48.991567", 
                "stderr": "", 
                "stderr_lines": [], 
                "stdout": "PING 192.168.1.27 (192.168.1.27) 56(84) bytes of data.\n64 bytes from 192.168.1.27: icmp_seq=1 ttl=128 time=0.337 ms\n\n--- 192.168.1.27 ping statistics ---\n1 packets transmitted, 1 received, 0% packet loss, time 0ms\nrtt min/avg/max/mdev = 0.337/0.337/0.337/0.000 ms", 
                "stdout_lines": [
                    "PING 192.168.1.27 (192.168.1.27) 56(84) bytes of data.", 
                    "64 bytes from 192.168.1.27: icmp_seq=1 ttl=128 time=0.337 ms", 
                    "", 
                    "--- 192.168.1.27 ping statistics ---", 
                    "1 packets transmitted, 1 received, 0% packet loss, time 0ms", 
                    "rtt min/avg/max/mdev = 0.337/0.337/0.337/0.000 ms"
                ]
            }, 
            {
                "ansible_loop_var": "pinging_host", 
                "changed": true, 
                "cmd": "ping -c1 -w 2 192.168.1.42", 
                "delta": "0:00:02.003313", 
                "end": "2021-02-25 17:08:51.269047", 
                "failed": true, 
                "invocation": {
                    "module_args": {
                        "_raw_params": "ping -c1 -w 2 192.168.1.42", 
                        "_uses_shell": true, 
                        "argv": null, 
                        "chdir": null, 
                        "creates": null, 
                        "executable": null, 
                        "removes": null, 
                        "stdin": null, 
                        "stdin_add_newline": true, 
                        "strip_empty_ends": true, 
                        "warn": true
                    }
                }, 
                "msg": "non-zero return code", 
                "pinging_host": "192.168.1.42", 
                "rc": 1, 
                "start": "2021-02-25 17:08:49.265734", 
                "stderr": "", 
                "stderr_lines": [], 
                "stdout": "PING 192.168.1.42 (192.168.1.42) 56(84) bytes of data.\n\n--- 192.168.1.42 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 10ms", 
                "stdout_lines": [
                    "PING 192.168.1.42 (192.168.1.42) 56(84) bytes of data.", 
                    "", 
                    "--- 192.168.1.42 ping statistics ---", 
                    "2 packets transmitted, 0 received, 100% packet loss, time 10ms"
                ]
            }
        ]
    }
}


TASK [ ***Result ping] *******************
ok: [computer1] => {
    "pingged_host.results.value.pinging_host": "VARIABLE IS NOT DEFINED!"
}

注意:“变量未定义!”或“...模板化字符串时出现模板错误:预期名称...”,当我放置其他路径时得到此结果:

如何从我的寄存器变量中读取“pinging_host”或“rc”??????

谢谢

你在 yaml 中的数据结构如下所示

pingged_host:
  results:
    - pinging_host: 192.168.1.27
      rc: 0
    - pinging_host: 192.168.1.42
      rc: 0

由于结果包含一个数组,您必须引用数组的确切元素。

即要获得第一个结果,您应该使用

  - name: " {{ role_name }} | ***Ip  ping"
    debug:
      var: pingged_host.results[0].pinging_host

要获取每个属性,您可以使用 maplist 过滤器,如此处解释 https://ansiblemaster.wordpress.com/2017/02/24/debug-properly-data-registered-with-a-loop/ ,如果您想要获取所有 rc,您可以使用以下内容:

  - name: " {{ role_name }} | ***Ip  ping"
    debug:
      msg: "{{  pingged_host.results|map(attribute='rc')|list }}"

导致:

TASK [{{ role_name }} | ***Ip  ping] **********************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        0,
        0
    ]
}