可以使用Ansible的template模块来生成pg_hba.conf配置文件,其中使用主机文件中的IP进行配置。以下是一个示例解决方法:
---
- name: Configure pg_hba.conf
hosts: all
become: true
tasks:
- name: Generate pg_hba.conf
template:
src: pg_hba.conf.j2
dest: /etc/postgresql/{{ postgres_version }}/main/pg_hba.conf
notify: Restart PostgreSQL
{% for host in groups['all'] %}
host all all {{ hostvars[host]['ansible_host'] }}/32 md5
{% endfor %}
该模板文件会迭代所有主机,并为每个主机生成一行配置,其中使用主机文件中的IP(hostvars[host]['ansible_host'])作为配置项。
handlers:
- name: Restart PostgreSQL
service:
name: postgresql
state: restarted
ansible-playbook pg_hba_conf.yml
这将使用主机文件中的IP生成pg_hba.conf配置文件,并在配置文件更改后重启PostgreSQL服务。