How to assign an array to a variable in an Ansible-Playbook

Question:

In a playbook I got the following code:

I would like to replace the value of postgresql_pg_hba_passwd_hosts with all of my webservers private ips. I understand I can get the values like this in a template:

What is the simplest/easiest way to assign the result of this loop to a variable in a playbook? Or is there a better way to collect this information in the first place? Should I put this loop in a template?

Additional challenge: I’d have to add /32 to every entry.

Answer:

You can assign a list to variable by set_fact and ansible filter plugin.

Put custom filter plugin to filter_plugins directory like this:


to_group_vars.py convert hostvars into list that selected by group.

Use like this:

Leave a Reply