Ansible Certification Preparation Course - Lab - Project - File Seperation

While doing the labs after doing the tasks for separation in tasks folder and testing of the modified yml file in include-tasks get the below error for index.php.j2 file

TASK [Creates the index.php file] **********************************************
fatal: [lampweb]: FAILED! => {“changed”: false, “msg”: “AnsibleUndefinedVariable: ‘dict object’ has no attribute ‘eth0’”}
to retry, use: --limit @/home/thor/playbooks/lamp-stack-playbooks/deploy-lamp-stack.retry

PLAY RECAP *********************************************************************
lamp-db : ok=13 changed=1 unreachable=0 failed=0
lampweb : ok=11 changed=1 unreachable=0 failed=1

The jinja file line is -

$link = mysqli_connect("{{ hostvars[‘lamp-db’][‘ansible_facts’][‘eth0’][‘ipv4’][‘address’] }}", “{{ hostvars[‘lamp-db’][‘dbuser’] }}”, “{{ hostvars[‘lamp-db’][‘dbpassword’] }}”, “{{ hostvars[‘lamp-db’][‘dbname’] }}”);

ansible lampweb -m setup -i inventory command output shows as

“ansible_eth0”: {
“active”: true,
“device”: “eth0”,
“ipv4”: {
“address”: “”,
“broadcast”: “”,
“netmask”: “”,
“network”: “”
“macaddress”: “02:42:ac:14:01:64”,
“mtu”: 1500,
“promisc”: false,
“speed”: 10000,
“type”: “ether”

Can someone help on what is wrong here ?

Hi @srinivasons

It appears that the facts for lamp-db have not been gathered at this point, therefore the facts dictionary has no keys and eth0 cannot be found. Adding the following as the first task in deploy-lamp-stack.yml fixed it for me

- name: gather facts
  hosts: all
  gather_facts: yes