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

4,103 views
Skip to first unread message

Mitesh Bhandari

unread,
Apr 25, 2020, 5:32:01 PM4/25/20
to Ansible Project
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 }}"

[user@node0101 veriable_include]$ cat myplaybook.yml
---
- name: Install Some Package
  hosts: node0102
  tasks:
  - name: include variables from varPackage.yml
    include_vars: varPackage.yml
  - name: include tasks from tasks.yml
    include: tasks.yml
...
[user@node0101 veriable_include]$


Please advice ...

---
Kind Regards
~Mitesh

Dick Visser

unread,
Apr 26, 2020, 12:42:30 AM4/26/20
to ansible...@googlegroups.com
You don't need to mention 'vars', just the variable itself, i.e. "{{ package }}".



[user@node0101 veriable_include]$ cat myplaybook.yml
---
- name: Install Some Package
  hosts: node0102
  tasks:
  - name: include variables from varPackage.yml
    include_vars: varPackage.yml
  - name: include tasks from tasks.yml
    include: tasks.yml
...
[user@node0101 veriable_include]$


Please advice ...

---
Kind Regards
~Mitesh

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/383a093b-cd48-4e00-95e1-f2535f083e5d%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Mitesh Bhandari

unread,
Apr 26, 2020, 4:23:25 PM4/26/20
to Ansible Project
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
---
- name: include veriables from varPackages.yml
  include_vars: varPackage.yml
- name: Install {{ package }} package
  yum:
   name: "{{ package }}"
   state: latest
- name: Start {{ service }} service
  service:
   name: "{{ service }}"
   state: "{{ state }}"
   enabled: "{{ enabled }}"


[user@node0101 veriable_include]$ cat myplaybook.yml
---
- name: Install Some Package
  hosts: node0102
  tasks:
  - name: include tasks from tasks.yml
    include: tasks.yml
...
[user@node0101 veriable_include]$


---
Kind Regards,
~Mitesh

Kai Stian Olstad

unread,
Apr 26, 2020, 4:40:15 PM4/26/20
to ansible...@googlegroups.com
On Sat, Apr 25, 2020 at 02:32:01PM -0700, Mitesh Bhandari wrote:
> *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 }}"

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


--
Kai Stian Olstad

Mitesh Bhandari

unread,
Apr 27, 2020, 5:09:37 AM4/27/20
to Ansible Project
Hello Kai, 

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

[user@node0101 veriable_include]$ cat varPackage.yml
---
myvars:

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


Regards,
~ Mitesh 

On Sunday, April 26, 2020 at 3:02:01 AM UTC+5:30, Mitesh Bhandari wrote:
Reply all
Reply to author
Forward
0 new messages