ansible Playbook execution failed with error: "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'package'

Hello,

I am new learner, trying to get hands on inclusions, where I am using include_vars & include, however playbook execution is falling with an undefined variable error.

[user@node0101 veriable_include]$ ansible-playbook -i inventory myplaybook.yml
PLAY [Install Some Package] ********************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [node0102]
TASK [include variables from varPackage.yml] ***************************************************************************************************************************
ok: [node0102]
TASK [Install {{ vars.package }} package] ******************************************************************************************************************************
fatal: [node0102]: FAILED! => {“msg”: “The task includes an option with an undefined variable. The error was: ‘dict object’ has no attribute ‘package’\n\nThe error appears to be in ‘/home/user/veriable_include/tasks.yml’: line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n—\n- name: Install {{ vars.package }} package\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - "{{ foo }}"\n”}
PLAY RECAP *************************************************************************************************************************************************************
node0102 : ok=2 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
[user@node0101 veriable_include]$

[user@node0101 veriable_include]$ pwd
/home/user/veriable_include

[user@node0101 veriable_include]$ ls -l
total 20
-rw-rw-r–. 1 user user 93 Apr 25 17:57 ansible.cfg
-rw-rw-r–. 1 user user 24 Apr 25 16:31 inventory
-rw-rw-r–. 1 user user 208 Apr 26 02:51 myplaybook.yml
-rw-rw-r–. 1 user user 247 Apr 26 02:51 tasks.yml
-rw-rw-r–. 1 user user 71 Apr 26 02:52 varPackage.yml

Following are the yml files & playbooks content:

[user@node0101 veriable_include]$ cat varPackage.yml

Hello,

I am new learner, trying to get hands on inclusions, where I am using include_vars & include, however playbook execution is falling with an undefined variable error.

[user@node0101 veriable_include]$ ansible-playbook -i inventory myplaybook.yml
PLAY [Install Some Package] ********************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [node0102]
TASK [include variables from varPackage.yml] ***************************************************************************************************************************
ok: [node0102]
TASK [Install {{ vars.package }} package] ******************************************************************************************************************************
fatal: [node0102]: FAILED! => {“msg”: “The task includes an option with an undefined variable. The error was: ‘dict object’ has no attribute ‘package’\n\nThe error appears to be in ‘/home/user/veriable_include/tasks.yml’: line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n—\n- name: Install {{ vars.package }} package\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - "{{ foo }}"\n”}
PLAY RECAP *************************************************************************************************************************************************************
node0102 : ok=2 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
[user@node0101 veriable_include]$

[user@node0101 veriable_include]$ pwd
/home/user/veriable_include

[user@node0101 veriable_include]$ ls -l
total 20
-rw-rw-r–. 1 user user 93 Apr 25 17:57 ansible.cfg
-rw-rw-r–. 1 user user 24 Apr 25 16:31 inventory
-rw-rw-r–. 1 user user 208 Apr 26 02:51 myplaybook.yml
-rw-rw-r–. 1 user user 247 Apr 26 02:51 tasks.yml
-rw-rw-r–. 1 user user 71 Apr 26 02:52 varPackage.yml

Following are the yml files & playbooks content:

[user@node0101 veriable_include]$ cat varPackage.yml

vars:
package: samba
service: smb
state: started
enabled: true

[user@node0101 veriable_include]$ cat tasks.yml

  • name: Install {{ vars.package }} package
    yum:
    name: “{{ vars.package }}”
    state: latest
  • name: Start {{ vars.service }} service
    service:
    name: “{{ vars.service }}”
    state: “{{ vars.state }}”
    enabled: “{{ vars.enabled }}”

You don’t need to mention ‘vars’, just the variable itself, i.e. “{{ package }}”.

Hello Visser,

Thank you very much looking in to, I have already tried by referencing variable {{ package }} but same error.

[user@node0101 veriable_include]$ ansible-playbook -i inventory myplaybook.yml
PLAY [Install Some Package] ********************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [node0102]
TASK [include veriables from varPackages.yml] **************************************************************************************************************************
ok: [node0102]
TASK [Install {{ package }} package] ***********************************************************************************************************************************
fatal: [node0102]: FAILED! => {“msg”: “The task includes an option with an undefined variable. The error was: ‘package’ is undefined\n\nThe error appears to be in ‘/home/user/veriable_include/tasks.yml’: line 4, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n include_vars: varPackage.yml\n- name: Install {{ package }} package\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - "{{ foo }}"\n”}
PLAY RECAP *************************************************************************************************************************************************************
node0102 : ok=2 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
[user@node0101 _include]$

[user@node0101 veriable_include]$ cat tasks.yml

vars is a reserved word in Ansible so you need to choose another name for your variable.

Hello Kai,

Thanks for help, I changed variable name other than vars and issue resolved.

[user@node0101 veriable_include]$ cat varPackage.yml