0

My hosts have 3 network IP addresses and one of them is needed later in my playbook.

In my playbook I have ran the following setup module:

- name: Gather Networks Facts into Variable
  setup:
  register: setup

- name: Debug Set Facts
  debug:
    var: setup.ansible_facts.ansible_ip_addresses

The provides the following output:

{
    "setup.ansible_facts.ansible_ip_addresses": [
        "10.0.2.15", 
        "fe80::85ae:2178:df12:8da0", 
        "192.168.99.63", 
        "fe80::3871:2201:c0ab:6e39", 
        "192.168.0.63", 
        "fe80::79c5:aa03:47ff:bf65", 
        "fd89:8d5f:2227:0:79c5:aa03:47ff:bf65", 
        "2a02:c7f:9420:7100:79c5:aa03:47ff:bf65"
    ]
}

I am trying to find a way to find the 192.168.0.63 by searching using the first three octets or 192.168.0. I also then want to get that value into a fact so I can use this later in my playbook.

What would be the best way to search and find that value with Ansible or Jinja2?

techraf
  • 64,883
  • 27
  • 193
  • 198
PatchSte
  • 11
  • 1
  • 3

1 Answers1

1

Will this do?

- set_fact:
    my_fact: "{{ (setup.ansible_facts.ansible_ip_addresses | select('match','192.168.0.') | list)[0] }}"

If there are multiple values matching the pattern, it will get the first one in order.

techraf
  • 64,883
  • 27
  • 193
  • 198