I’m a bit stumped here.
I have a playbook setup that uses hostvars to fill in expressions in other places including jinja2 templates. Whenever one of those variables is an IP address, I get the error ‘dict’ object has no attribute ‘xx.xx.xx.xx’. when there’s a var set to that value.
For example,
/install/vars/main.yml
db_master_ip: “{{hostvars[‘hostname.localdomain’][ansible_eth0.ipv4.address]}}”
is used in a later step as
/install/tasks/main.yml
- include: standby.yml
/install/tasks/standby.yml
shell: pg_basebackup -h {{db_master_ip}} -D {{postgres_data_dir}} -U {{db_user}} -v -x
Ansible fails on that step with the ‘dict’ object has no attribute error. Though it shows the correct IP address in the error message
If I change it to
shell: pg_basebackup -h {{hostvars[‘hostname.localdomain’][ansible_eth0.ipv4.address]}}
It works just fine, but that’s definitely less than ideal as I use the {{ db_master_ip }} expression in quite a few places and would like to be able to easily modify it.
Alternatively, if I change
db_master_ip: 192.168.1.2
it also works.
I know it’s got to be something with the way jinja2 is parsing the dict, but I have no idea where it would be going wrong or what I can do to fix it. I’ve been googling around for the last day and haven’t really come up with anything that makes sense to my situation.