I’m just trying to write a basic playbook, and keep getting the error below.
Tried a tonne of things but still can’t get it right. I know it must be a syntax thing but no idea where.
This is the code I have:
---
# This playbook runs a basic DF command.
- hosts: nagios
#remote_user: root
tasks:
- name: find disk space available.
command: df -hPT
This is the error I get:
> ERROR! 'command' is not a valid attribute for a Play
>
> The error appears to have been in '/root/playbooks/df.yml': line 4,
> column 3, but may be elsewhere in the file depending on the exact
> syntax problem.
>
> The offending line appears to be:
>
>
> - hosts: nagios
^ here
Ansible ver: 2.4.2.0
It’s driving me insane. I’ve looked at some axamples from the Ansible docs, and it looks the same.
No idea…
Anyone know?
user513951
12.1k7 gold badges66 silver badges81 bronze badges
asked Mar 13, 2018 at 1:17
8
The problem is that without the indentation of the command line, the command directive is part of the overall play, and not the task block.
i.e. the command should be part of the task block.
---
# This playbook runs a basic DF command.
- hosts: nagios
#remote_user: root
tasks:
- name: find disk space available.
command: df -hPT
Tms91
3,1564 gold badges37 silver badges68 bronze badges
answered Mar 13, 2018 at 3:42
JesseJesse
1,7161 gold badge20 silver badges25 bronze badges
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
Comments
ansibot
added
affects_2.6
This issue/PR affects Ansible v2.6
bug
This issue/PR relates to a bug.
module
This issue/PR relates to a module.
needs_triage
Needs a first human triage before being processed.
support:core
This issue/PR relates to code supported by the Ansible Engineering Team.
labels
Jun 8, 2018
ansibot
added
the
needs_info
This issue requires further information. Please answer any outstanding questions.
label
Jun 8, 2018
ansibot
removed
the
needs_info
This issue requires further information. Please answer any outstanding questions.
label
Jun 8, 2018
ansible
locked as resolved and limited conversation to collaborators
Nov 20, 2018
Hi,
Need help in Ansible playbook.
Trying to run a basic playbook to fetch facts and command, but it keeps popping error. Can please anyone help.
Playbook 1
- name: Collect
bigip_device_facts:
gather_subset:
- interfaces
- vlans
provider:
server: x.x.x.x
user: <username>
password: <password>
Error
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
ERROR! 'bigip_device_facts' is not a valid attribute for a Play
The error appears to have been in '/ansible-01/playbook.yml': line 1, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Collect
^ here
Playbook 2
- name: run show version
bigip_command:
commands: show sys version
provider:
server: x.x.x.x
user: <username>
password: <password>
Error
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
ERROR! 'bigip_command' is not a valid attribute for a Play
The error appears to have been in '/ansible-01/playbook1.yml': 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 show version
^ here
Thanks,
Aditya
- is not a valid attribute for a Play
When ever you get the above error firstly crosscheck that the ansible attribute you have mentioned is correct. If it is correct then the issue probably is that you have created tasks as follows:--- - vars_prompt: - name: "var1" prompt: "Please pass variable" private: no - fail: msg="var1 is not passed or blank" when: var1 is undefined or ( var1 is defined and storeid == "" )when it should be as follows:
--- vars_prompt: - name: "var1" prompt: "Please pass variable" private: no tasks: - fail: msg="var1 is not passed or blank" when: var1 is undefined or ( var1 is defined and storeid == "" )the example referenced is just a task. It is not a valid playbook because it is missings a hosts declaration and the module call is not under a tasks section.
- ERROR! conflicting action statements: fail, command
I get this error if I have a task as follows:
– name: deploy
win_get_url:
url: ‘http://server_ip/builds/build.zip’
dest: ‘D:build.zip’
win_unzip:
src: D:build.zip
dest: D:You cannot have multiple actions listed inside a single task like this. Instead you need to do this:- name: deploy get url win_get_url: url: 'http://server_ip/builds/build.zip' dest: 'D:build.zip' - name: deploy unzip win_unzip: src: D:build.zip dest: D: - Ansible task to check API status
Here I am checking ES cluster health:- name: Get ES cluster health uri: url: http://{{inventory_hostname}}:9200/_cluster/health return_content: yes register: cluster_status - set_fact: es_cluster_health: "{{ cluster_status.content | from_json }}" - fail: msg="ES cluster not healthy" when: "es_cluster_health.status != 'yellow'"You can compare the status with any string you want. Here I am comparing it with string “yellow”


