在ansible中从ping输出中获取数据 [英] Get data from ping output in ansible

查看:22
本文介绍了在ansible中从ping输出中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I can't extract the ip from the register var after ping command from "shell" module.

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

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

Nota: "VARIABLE IS NOT DEFINED!" or "...template error while templating string: expected name...", I have this result when I put other path's:

  • pingged_host.results.value.pinging_host
  • pingged_host.results.pinging_host
  • pingged_host.results.[pinging_host]
  • pingged_host.results[pinging_host]
  • pingged_host.results['pinging_host']

我如何阅读pinging_host"?或rc"从我的寄存器 var?????????

How I can read "pinging_host" or "rc" from my register var???????

谢谢

推荐答案

你在 yaml 中的数据结构是这样的

Your data structure in yaml looks like this

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

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

Since result contains an array, you have to reference exact element of the array.

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

i.e to get first result you should use

  - 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,你可以使用以下命令:

To get every attribute you can use map and list filters, as explained here https://ansiblemaster.wordpress.com/2017/02/24/debug-properly-data-registered-with-a-loop/ , if you want to get all rc's, you can use the following:

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

导致:

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

这篇关于在ansible中从ping输出中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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