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
COMPONENT NAME
«openshift_raw»
ANSIBLE VERSION
ansible 2.5.1
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/home/gbaufake/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.14 (default, Mar 14 2018, 13:36:31) [GCC 7.3.1 20180303 (Red Hat 7.3.1-5)]
CONFIGURATION
OS / ENVIRONMENT
- Fedora 27
SUMMARY
Openshift_raw (http://docs.ansible.com/ansible/2.5/modules/openshift_raw_module.html?highlight=openshift_raw) can’t be used on playbook
STEPS TO REPRODUCE
- Create a simple playbook (
playbook.yml
) with openshift_raw
like
- name: Create a project
openshift_raw:
api_version: v1
kind: Project
name: testing
description: Testing
display_name: "This is a test project."
state: present
ansible-playbook playbook.yml
EXPECTED RESULTS
- Execute the commands on the playbook
ACTUAL RESULTS
- Message saying «‘openshift_raw’ is not a valid attribute for a Play «
ansible-playbook playbook.yml -e username=user -e password=password
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
ERROR! 'openshift_raw' is not a valid attribute for a Play
ansibot
added
affects_2.5
This issue/PR affects Ansible v2.5
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:community
This issue/PR relates to code supported by the Ansible community.
labels
Apr 26, 2018
The error indicates that your playbook looks only like:
- name: Create a project
openshift_raw:
api_version: v1
kind: Project
name: testing
description: Testing
display_name: "This is a test project."
state: present
This is not a valid playbook.
A valid playbook must include a hosts:
key, and any tasks must live under a tasks:
header, such as:
- hosts: all
tasks:
- name: Create a project
openshift_raw:
api_version: v1
kind: Project
name: testing
description: Testing
display_name: "This is a test project."
state: present
If you have further questions please stop by IRC or the mailing list:
- IRC: #ansible on irc.freenode.net
- mailing list: https://groups.google.com/forum/#!forum/ansible-project
@sivel
Thanks for the update and sorry for the silly mistake!
ansible
locked and limited conversation to collaborators
Apr 27, 2019
I am relatively inexperienced when it comes to Ansible however I have previously created playbooks which will create a cron job successfully. For the life of me I cannot work out what is wrong with my .yml file.
I am receiving the below error when attempting to run my playbook locally with:
ansible-playbook -i "localhost," -c local crond.yml
ERROR! 'cron' is not a valid attribute for a Play
The error appears to have been in
'/etc/ansible/cds/roles/backups/tasks/crond.yml': line 3, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- cron:
^ here
I have seen similar questions asked on SE/Ansible forums but to no avail. Any help/guidance would be much appreciated.
Here is my crond.yml file:
- cron:
user: "root"
minute: "30"
hour: "02"
job: '{{/usr/bin/rsync -avhz -e "ssh" --rsync-path="sudo rsync" -H --delete --numeric-ids <ipaddress>:"/data/rsnapshot-backups/<ipaddress>" /data/rsnapshot-backup}}'
state: present
asked Jan 29, 2018 at 10:00
crond.yml
in your example is not a playbook. It is a tasks file (list of tasks), while playbook is a list of plays.
A play must have hosts
directive at minimum. Please see http://docs.ansible.com/ansible/latest/playbooks_intro.html#hosts-and-users
This is a playbook:
- hosts: localhost
tasks:
- cron:
user: "root"
minute: "30"
hour: "02"
job: '/usr/bin/rsync -avhz -e "ssh" --rsync-path="sudo rsync" -H --delete --numeric-ids <ipaddress>:"/data/rsnapshot-backups/<ipaddress>" /data/rsnapshot-backup'
state: present
answered Jan 29, 2018 at 12:03
3
I got around this by adding my crond.yml task to the global playbook and now call it via tags (ansible-playbook -vvv -i «localhost,» -c local site.yml —tags backups).
I added the cron_file section to define the location of the cron job as /etc/cron.d/rsync.cron rather than the root users crontab. I then added state: present to ensure that the cronjob exists on the server and doesn’t delete other entries in the file.
I also updated the job section of the task to remove the ‘{{ }}’ and replace with double quotes surrounding the command as well as »s around any double quoted strings like below:
- cron:
name: "Copy all rsnapshot backups from <hostname>"
minute: "30"
hour: "02"
job: "/usr/bin/rsync -avhz -e "ssh" --rsync-path="sudo rsync" -H --delete --numeric-ids <ipaddress>:"/data/rsnapshot-backups/<ipaddress>" /data/rsnapshot-backup"
user: root
cron_file: rsync.cron
state: present
answered Jan 29, 2018 at 13:40
jtojto
3781 gold badge6 silver badges19 bronze badges