-1

Suppose I have this list:

hosts:
  - {"ip": "1.2.3.4", "hostname": "www.example.com"}
  - {"ip": "42.42.42.42", "hostname": "www.wikipedia.com"}

I want to obtain a string like this (to put it into a template):

'1.2.3.4,42.42.42.42'

It's similar to what I'd obtain with hosts|join(','), but I only want to concatenate the values of ip, so probably I should obtain a list of IPs first... but how?

1 Answers1

1

you could pass the hosts to map(attribute='ip') filter.

here is a PB to try it:

---
- hosts: localhost
  gather_facts: false
  vars:
    hosts:
      - {"ip": "1.2.3.4", "hostname": "www.example.com"}
      - {"ip": "42.42.42.42", "hostname": "www.wikipedia.com"}
  tasks:
  - debug:
      var: hosts | map(attribute='ip') | list | join(',')

cheers

ilias-sp
  • 6,135
  • 4
  • 28
  • 41