On 4/27/20 10:12 AM, Marc Haber wrote: > Let's assume I have a service that can listen on different combinations > or host/port: >
Hello Marc,
nice to see you here :-). This is certainly doable:
---
- hosts: all
vars:
host:
service:
hostname: "google.com"
ports:
- "80"
- "443"
tasks:
- name: Get DNS records
set_fact:
ip_addresses: "{{ ip_addresses | default([]) + lookup('dig',
host.service.hostname + '/' + item, wantlist=True) }}"
loop:
- A
- AAAA
when: "'listen' not in host.service"
- name: Determine all combinations of IP address and ports
set_fact:
ips_ports: "{{ ip_addresses | product(host.service.ports) | list }}"
when: "'listen' not in host.service"
- name: Turn list members into dictionaries
set_fact:
host:
service:
listen: "{{ host.service.listen | default([]) + [{ 'ip': item[0],
'port': item[1] }] }}"
loop: "{{ ips_ports }}"
when: ips_ports is defined
- debug:
msg: "{{ host }}"
Give it a try and let me know if it works. Or if you need help to understand
how it works :-).
Regards
Racke
> ---
> host:
> service:
> listen:
> - ip: "2001:db8:1:2::1"
> port: "80"
> - ip: "2001:db8:1:2::1"
> port: "443"
> - ip: "192.0.1.2"
> port: "80"
>
> let's also assume that the vast majority of cases use a rather simple
> setup so that it would be tedious to write the standard configuration
> over and over again, so it would be nice if one could just write:
>
> ---
> host:
> service:
> hostname: "some-dns-name.example"
> ports:
> - "80"
> - "443"
>
> I would then like to have some component of my ansible setup to go to
> DNS, look up A and AAAA records for the DNS name and generate the
> detailed listen configuration.
>
> Given some-dns-name.example would have an A record of 192.0.1.40 and an
> AAAA record of 2001:db8:1::40, the data structure built would be
>
> ---
> host:
> service:
> listen:
> - ip: "2001:db8:1::40"
> port: "80"
> - ip: "2001:db8:1::40"
> port: "443"
> - ip: "192.0.1.40"
> port: "80"
> - ip: "192.0.1.40"
> port: "443"
>
> so that the tasks and templates could always act as if the fully
> detailed configuration were explicitly given, while giving the admin the
> possibility to always write doen the fully detailed configuration
> explicitly AND the ease of writing down the easier form for the standard
> cases.
>
> I could have a program read in the inventory, write out a temporary
> inventory with the "augmented" host variables and then have ansible run
> from this, but I hope that this won't be necessary...
>
> Greetings
> Marc
>
>
--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/f84938f1-21b5-dc05-f4b5-e4151da4113d%40linuxia.de.
signature.asc
Description: OpenPGP digital signature
