Reputation: 8504
Testing a basic Ansible roles setup but got an error on the first line of a role main.yml
, I am sure it's something silly though
play.yml
- hosts: myhosts
remote_user: myuser
roles:
- test
Directory structure
play.yml
roles/test/tasks/main.yml
main.yml
- hosts: all
user: myuser
gather_facts: no
tasks:
- name: ping all hosts
ping:
When I run ansible-playbook play.yml
, I get the error
The offending line appears to be:
- hosts: all
^ here
It looks like a simple YAML parse error but if I run ansible-playbook main.yml
, it works fine, so not sure what's going on. Any thoughts?
Upvotes: 1
Views: 2260
Reputation: 9353
You cannot specify hosts in roles/test/tasks/main.yml
. The hosts are specified in the play.yml
file.
roles/test/tasks/main.yml
is used to define the actions you want Ansible to perform. In your case to ping hosts, it could simply look like:
---
- ping:
This will perform the ping
action on the hosts specified in your play.yml
Ansible has a set folder structure it can use to break down playbooks. The play.yml
file, specifies which hosts to target and what roles to apply along with other top level controls.
Individual roles specified in the play.yml
file are located in roles/X/
, there is a certain folder structure that Ansible expects. It will look for tasks to run in the test
role here roles/test/tasks/main.yml
.
play.yml
is just one playbook. You can create many in the same folder and call them with ansible-playbook
.
The official documentation has a more detailed example of the recommended playbook directory structure
Upvotes: 2