How to reproduce, troubleshoot, and fix the “not a valid attribute for a Play” error.
September 20, 2021
Today we’re going to talk about Ansible troubleshooting and specifically about the “not a valid attribute for a Play” error.
I’m Luca Berton and welcome to today’s episode of Ansible Pilot.
The Best Resources For Ansible
Video Course
- Learn Ansible Automation in 250+examples & practical lessons: Learn Ansible with some real-life examples of how to use the most common modules and Ansible Playbook
Printed Book
- Ansible For VMware by Examples: A Step-by-Step Guide to Automating Your VMware Infrastructure
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
demo
The best way of talking about Ansible troubleshooting is to jump in a live demo to show you practically the not a valid attribute for a Play
error and how to solve it!
error code
- invalid_play_attribute_error.yml
---
- name: file module demo
hosts: all
vars:
myfile: "~/example.txt"
task:
- name: Creating an empty
ansible.builtin.file:
path: "{{ myfile }}"
state: touch
error execution
- output
$ ansible-playbook -i demo/inventory troubleshooting/invalid_play_attribute_fix.yml
ERROR! 'task' is not a valid attribute for a Play
The error appears to be in 'ansible-pilot/troubleshooting/invalid_play_attribute_error.yml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- -
- name: file module demo
^ here
fix code
- invalid_play_attribute_fix.yml
---
- name: file module demo
hosts: all
vars:
myfile: "~/example.txt"
tasks:
- name: Creating an empty
ansible.builtin.file:
path: "{{ myfile }}"
state: touch
fix execution
- output
$ ansible-playbook -i demo/inventory troubleshooting/invalid_play_attribute_fix.yml
PLAY [file module demo] *********************************************************************
TASK [Gathering Facts] **********************************************************************
ok: [demo.example.com]
TASK [Creating an empty file] ***************************************************************
changed: [demo.example.com]
PLAY RECAP **********************************************************************************
demo.example.com : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
code with ❤️ in GitHub
Recap
Now you know better how to troubleshoot the not a valid attribute for a Play error Error.
Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate
ISSUE TYPE
- Bug Report
- Documentation Report
COMPONENT NAME
include_tasks
ANSIBLE VERSION
The error appears on both ansible-2.3 and 2.5
sudo rpm -Uvh ./rpm-build/ansible-*.noarch.rpm
Preparing... ################################# [100%]
Updating / installing...
1:ansible-2.5.0-100.git201711240634################################# [ 50%]
Cleaning up / removing...
2:ansible-2.3.1.0-1.fc25 ################################# [100%]
CONFIGURATION
OS / ENVIRONMENT
Fedora 25
SUMMARY
A sample playbook [1] from docs returns an error: ERROR! 'include_tasks' is not a valid attribute for a Play
.
[1] https://github.com/ansible/ansible/pull/30466/files#diff-1a5a2a1237daf780d2ef2061e1b42545R97
I’m not sure whether it’s a documentation bug only or smth else. I searched for include_tasks:
in this git repo. The found sample playbooks are similar, I didn’t try to run them though.
STEPS TO REPRODUCE
I created two files main.yml
and execute_batch.yml
as at the bottom of [2], just added in main.yml
:
hosts: localhost
connection: local
become: yes
become_method: sudo
and executed: sudo ansible-playbook main.yml
. The error was mentioned earlier.
[2] https://github.com/ansible/ansible/blob/devel/docs/docsite/rst/playbooks_async.rst
EXPECTED RESULTS
If to update main.yml
given at the bottom of [2] as following, evrth works fine:
<...>
- 4
- 5
durations: "{{ item }}"
tasks:
- include_tasks: execute_batch.yml
with_items:
- "{{ sleep_durations | batch(2) | list }}"
Result:
TASK [Gathering Facts] *********************************************************************************
ok: [127.0.0.1]
TASK [include_tasks] ***********************************************************************************
included: /home/igulina/git_projects/compose-tests/execute_batch.yml for 127.0.0.1
included: /home/igulina/git_projects/compose-tests/execute_batch.yml for 127.0.0.1
included: /home/igulina/git_projects/compose-tests/execute_batch.yml for 127.0.0.1
TASK [Async sleeping for batched_items] ****************************************************************
changed: [127.0.0.1] => (item=1)
changed: [127.0.0.1] => (item=2)
TASK [Check sync status] *******************************************************************************
changed: [127.0.0.1] => (item={'_ansible_parsed': True, '_ansible_item_result': True, u'async_item': 1, '_ansible_item_label': 1, u'ansible_job_id': u'898592897896.7697', u'started': 1, 'changed': True, 'failed': False, u'finished': 0, u'results_file': u'/root/.ansible_async/898592897896.7697', '_ansible_ignore_errors': None, '_ansible_no_log': False})
FAILED - RETRYING: Check sync status (30 retries left).
changed: [127.0.0.1] => (item={'_ansible_parsed': True, '_ansible_item_result': True, u'async_item': 2, '_ansible_item_label': 2, u'ansible_job_id': u'132655700235.7728', u'started': 1, 'changed': True, 'failed': False, u'finished': 0, u'results_file': u'/root/.ansible_async/132655700235.7728', '_ansible_ignore_errors': None, '_ansible_no_log': False})
TASK [Async sleeping for batched_items] ****************************************************************
changed: [127.0.0.1] => (item=3)
changed: [127.0.0.1] => (item=4)
TASK [Check sync status] *******************************************************************************
FAILED - RETRYING: Check sync status (30 retries left).
changed: [127.0.0.1] => (item={'_ansible_parsed': True, '_ansible_item_result': True, u'async_item': 3, '_ansible_item_label': 3, u'ansible_job_id': u'907788887794.7868', u'started': 1, 'changed': True, 'failed': False, u'finished': 0, u'results_file': u'/root/.ansible_async/907788887794.7868', '_ansible_ignore_errors': None, '_ansible_no_log': False})
changed: [127.0.0.1] => (item={'_ansible_parsed': True, '_ansible_item_result': True, u'async_item': 4, '_ansible_item_label': 4, u'ansible_job_id': u'267599673068.7898', u'started': 1, 'changed': True, 'failed': False, u'finished': 0, u'results_file': u'/root/.ansible_async/267599673068.7898', '_ansible_ignore_errors': None, '_ansible_no_log': False})
TASK [Async sleeping for batched_items] ****************************************************************
changed: [127.0.0.1] => (item=5)
TASK [Check sync status] *******************************************************************************
FAILED - RETRYING: Check sync status (30 retries left).
changed: [127.0.0.1] => (item={'_ansible_parsed': True, '_ansible_item_result': True, u'async_item': 5, '_ansible_item_label': 5, u'ansible_job_id': u'672139654132.8010', u'started': 1, 'changed': True, 'failed': False, u'finished': 0, u'results_file': u'/root/.ansible_async/672139654132.8010', '_ansible_ignore_errors': None, '_ansible_no_log': False})
PLAY RECAP *********************************************************************************************
127.0.0.1 : ok=10 changed=6 unreachable=0 failed=0
ACTUAL RESULTS
ERROR! 'include_tasks' is not a valid attribute for a Play
The error appears to have been in 'la/la/la/main.yaml': line 1, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Run items asynchronously in batch of two items
^ here
I am still very much learning ansible so bear with me, but I am getting very frustrated as I keep running into the same issue over and over again and can’t seem to figure out/learn what I am doing wrong. vscode ansible addon seems to think it’s valid yaml, online yaml linters seem to see it as correct yaml syntax wise, but whenever I try and execute it all goes to shit.
I keep getting the following error messages and can’t determine whether it is my understanding of the /etc/ansible/hosts file as I am using it in the INI way and not the yaml format. Or if it the syntax of the way I am referencing my hosts within my playbook. I get the same error message when I use — name: as well, please see below.
ansible-playbook 2.7.9
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/home/my name/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 2.7.15rc1 (default, Nov 12 2018, 14:31:15) [GCC 7.3.0]
Using /etc/ansible/ansible.cfg as config file
/etc/ansible/hosts did not meet host_list requirements, check plugin documentation if this is unexpected
/etc/ansible/hosts did not meet script requirements, check plugin documentation if this is unexpected
Parsed /etc/ansible/hosts inventory source with ini plugin
ERROR! 'task' is not a valid attribute for a Play
The error appears to have been in '/etc/ansible/scripts/simpletest.yaml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
---
- hosts: localhost
^ here
and as said before happens here as well.
ansible-playbook 2.7.9
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/home/my name/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 2.7.15rc1 (default, Nov 12 2018, 14:31:15) [GCC 7.3.0]
Using /etc/ansible/ansible.cfg as config file
/etc/ansible/hosts did not meet host_list requirements, check plugin documentation if this is unexpected
/etc/ansible/hosts did not meet script requirements, check plugin documentation if this is unexpected
Parsed /etc/ansible/hosts inventory source with ini plugin
ERROR! 'debug' is not a valid attribute for a Play
The error appears to have been in '/etc/ansible/scripts/emailconfigtest.yaml': line 13, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Information Discovery on Cisco IOS Device
^ here
This leads me to believe it’s a syntax issue but I can’t imagine what it could be.