user10373379
user10373379

Reputation: 215

how to parse output from ansible

my playlist has below contests

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

- debug:
    var: ip_result.ansible_facts.ip

And getting below output (It comes with IP and port associated with it.)

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

But I am not able to get only 192.168.2.157 , how do I do that with ansible?

I also trying set regex_search instead of regex_findall

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

But output which I am getting is like

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

Upvotes: 0

Views: 2652

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68004

It's an array (list in ansible language). You're looking for the first element

ip_result.ansible_facts.ip[0]

Upvotes: 1

Related Questions