ali haider ali haider - 2 months ago 15
YAML Question

ansible role unable to find modile

I have a test.yml file which has a role to checkout code from git (both copied below):

site.yml

---
- hosts: webservers
user: me
roles:
- role: test-role


main.yml file in test-role/tasks

---

- name: fetching my repo
vars:
my_repo_url: git@bitbucket.org:myrepo/my-server.git
my_proj_path: /tmp/repos/my-server/
tasks:
- name: check out git repository on host
git: repo={{ my_repo_url }} dest={{ my_proj_path }} accept_hostkey=yes


hosts:

[webservers]
testserver ansible_ssh_host=xxx.xxx.xxx.xxxx ansible_ssh_port=xxxx


when I run the following:

ansible-playbook -i hosts site.yml


I am getting the exception copied below referencing the main.yml file in the tasks section of the role. Any ideas what I might have misconfigured.

ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.

The error appears to have been in '/home/user/git/roles/test-role/tasks/main.yml': line 3, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- name: fetching my repo
^ here


The error appears to have been in '/home/user/git/test/roles/test-role/tasks/main.yml': line 3, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- name: fetching my repo
^ here


Update
Tried changing as per Konstantin's feedback but I am getting more or less the same exception:

roles/test-role/defaults/main.yml

---
vars:
my_repo_url: git@bitbucket.org:myrepo/my-server.git
my_proj_path: /tmp/repos/my-server/


tasks/main.yml

---
- name: fetching my repo
tasks:
- name: check out git repository on host
git: repo={{ my_repo_url }} dest={{ my_proj_path }} accept_hostkey=yes


Exception:
Now its complaining about the line below. I get the same response when I run syntax check.

---
- name: fetching my repo
^ here


Update
Followed Konstantin and Avalon's answers - I was able to run the task successfully.

Answer

main.yml for role should contain only list of tasks, nothing else.

Split your file into test-role/defaults/main.yml (variables):

---
my_repo_url: git@bitbucket.org:myrepo/my-server.git
my_proj_path: /tmp/repos/my-server/ 

and test-role/tasks/main.yml (tasks):

---
# fetching my repo
- name: check out git repository on host
  git: repo={{ my_repo_url }} dest={{ my_proj_path }} accept_hostkey=yes