In a playbook I got the following code:
---
- hosts: db
vars:
postgresql_ext_install_contrib: yes
postgresql_pg_hba_passwd_hosts: ['10.129.181.241/32']
...
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:
{% for host in groups['web'] %}
{{ hostvars[host]['ansible_eth1']['ipv4']['address'] }}
{% endfor %}
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.
You can use jinja2 filters:
will return a list of ip addresses. i.e.
Does not include the challange (appending
/32
). But it should also be possible somehow with jinja2 filters.Reqiures ansible version >= 2.1
To add '/32' to the address, you can use the Ansible ipaddr filter (converting to CIDR notation).
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:
in playbook:
in template: (example is of type ini file, with sections, keys and values):
This should render the template as:
Variables can be represented as standard YAML structures so you can assign a list value to a key like this: