首页 > 解决方案 > 如何解析ansible的输出

问题描述

我的播放列表有以下比赛

- set_fact:
    ip={{output_result.cmd | regex_findall('[0-9./]+') | list}}
  register: ip_result

- debug:
    var: ip_result.ansible_facts.ip

并低于输出(它带有与之关联的 IP 和端口。)

TASK [debug] 
*********************************************************
ok: [localhost] => {
    "ip_result.ansible_facts.ip": [
        "192.168.2.157", 
        "3306"
    ]

但我不能只得到192.168.2.157,我该如何使用ansible?

我也尝试设置 regex_search 而不是 regex_findall

  ip={{output_result.cmd| regex_search('[0-9./]+') | list}}

但是我得到的输出就像

  TASK [debug] 
  ****************************************************************
  ok: [localhost] => {
    "ip_result.ansible_facts.ip": [
        "1", 
        "9", 
        "2", 
        ".", 
        "1", 
        "6", 
        "8",
        ".", 
        "2", 
        ".", 
        "1", 
        "5", 
        "7", 
        ".", 
        ".", 
        "."
    ]

标签: ansible

解决方案


它是一个数组(以 ansible 语言列出)。您正在寻找第一个元素

ip_result.ansible_facts.ip[0]

推荐阅读