Ansible troubleshooting - not a valid attribute for a Play error
How to reproduce, troubleshoot, and fix the “not a valid attribute for a Play” error.


Introduction
In today’s episode of Ansible Pilot, I’m Luca Berton, and we’ll be diving into Ansible troubleshooting, focusing on the infamous “not a valid attribute for a Play” error. This error can be a stumbling block for many, but fear not – I’ll guide you through reproducing, troubleshooting, and fixing this issue.
Unveiling the Error in a Live Demo
The most effective way to explore Ansible troubleshooting is through a live demonstration. Let’s reproduce the “not a valid attribute for a Play” error and walk through the steps to resolve 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
Executing the playbook with the error:
$ ansible-playbook -i demo/inventory troubleshooting/invalid_play_attribute_error.yml
ERROR! 'task' is not a valid attribute for a Play
The error appears to be in '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
Fixing the 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
The Best Resources For Ansible
Certifications
Video Course
Printed Book
eBooks
Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
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 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
Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
Executing the Fixed Code
Executing the fixed playbook:
$ ansible-playbook -i inventory 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
You can find the code on GitHub.
Recap
In summary, you’ve now gained valuable insights into troubleshooting the “not a valid attribute for a Play” error in Ansible. Understanding the error message, reproducing the issue, and implementing the necessary fixes are essential skills for every Ansible practitioner.
If you found this troubleshooting guide helpful, be sure to subscribe for more Ansible insights.
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