0

So I'm connecting with ansible to an interconnect device running PicOS8. I'm issuing a command which has the following output:

10.240.18.20,fe80::a94:efff:fe50:b516%eth0
fe80::a94:efff:fe71:d996%vlan.4,10.240.23.34
fe80::a94:efff:fe71:daa6%vlan.4,10.240.23.

This is not always standard since sometimes, the ipv4 address preceeds the ipv6 or at times there is no ipv6 at all.

I'm registering this output using ansible register, let's say as smm_output.

Then I want to display the output, showing only the ipv4 ip addresses (with the playbook bellow):

   - debug:
       msg: "{{ smm_output.stdout_lines | ipv4('address') }}"

but the output is blank. Not sure what I'm doing wrong here or how to strictly show the ipv4 ip addresses from that output.

Bogdan Stoica
  • 4,349
  • 2
  • 23
  • 38

1 Answers1

1

You have to traverse the list stdout_lines and split the string to the list. Try this one:

- debug:
    msg: "{{ item.split(',') | ipv4('address') }}"
  loop: "{{smm_output.stdout_lines}}"

You can also create separate array with IPv4 only

- set_fact:
    ip4: []

- set_fact:
    ip4: "{{ ip4 + (item.split(',') | ipv4('address') )}}"
  loop: "{{smm_output.stdout_lines}}"

- debug:
    var: ip4

Which should generate below output:

TASK [debug] ***********************************************************************************************************************************************************************************************************
ok: [127.0.0.1] => {
    "ip4": [
        "10.240.18.20",
        "10.240.23.34"
    ]
}
itiic
  • 3,284
  • 4
  • 20
  • 31
  • So I'm using as you suggested. The output is this: `TASK [debug] ok: [10.240.18.57] => { "msg": [ " fe80::a94:efff:fe50:b516%eth0,10.240.18.20", "fe80::a94:efff:fe71:d996%vlan.4,10.240.23.34", "fe80::a94:efff:fe71:daa6%vlan.4,10.240.23.35" ] } TASK [debug] ok: [10.240.18.57] => (item=None) => { "msg": [ "10.240.18.20" ] } ok: [10.240.18.57] => (item=None) => { "msg": [ "10.240.23.34" ] } ok: [10.240.18.57] => (item=None) => { "msg": [ "10.240.23.35" ] }` how do I get rid of that item=None? – Bogdan Stoica Feb 05 '20 at 09:16
  • Sorry not sure how to format the text right inside a comment – Bogdan Stoica Feb 05 '20 at 09:17
  • can you send me the output of below task `- debug: msg="{{ smm_output.stdout_lines }}"` – itiic Feb 05 '20 at 09:38
  • `ok: [10.240.18.57] => { "msg": [ "fe80::a94:efff:fe50:b516%eth0,10.240.18.20", "fe80::a94:efff:fe71:d996%vlan.4,10.240.23.34", "fe80::a94:efff:fe71:daa6%vlan.4,10.240.23.35" ] }` – Bogdan Stoica Feb 05 '20 at 10:27
  • Weird, I have ```ok: [127.0.0.1] => (item=10.240.18.20,fe80::a94:efff:fe50:b516%eth0) => { "msg": [ "10.240.18.20" ] } ok: [127.0.0.1] => (item=fe80::a94:efff:fe71:d996%vlan.4,10.240.23.34) => { "msg": [ "10.240.23.34" ] } ok: [127.0.0.1] => (item=fe80::a94:efff:fe71:daa6%vlan.4,10.240.23.) => { "msg": [] } ``` – itiic Feb 05 '20 at 10:32
  • I'll check it later. Thank you very much! – Bogdan Stoica Feb 05 '20 at 10:43