0
votes

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: [email protected]: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: [email protected]: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.

2

2 Answers

1
votes

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: [email protected]: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
1
votes

To add to what Konstantin said, your tasks should only contain tasks. Variables need to be defined in a separate file. Additionally, your syntax/formatting could be improved upon.

site.yml

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

roles/test-role/tasks/main.yml

---
- name: fetching my repo
  git: repo={{ my_repo_url }} dest={{ my_proj_path }} accept_hostkey=yes

roles/test-role/vars/main.yml

---
my_repo_url: [email protected]:myrepo/my-server.git
my_proj_path: /tmp/repos/my-server/ 

Also, I recommend setting your inventory path in your ansible.cfg file. The config file can either be in /etc/ansible.cfg or in the same directory as your playbooks (I personally have mine in the same directory as my playbook).

ansible.cfg

inventory      = inventory/hosts

If you do this, you will not have to specify the inventory file location when you run your playbook. If nothing else, it will save you a few keystrokes.

You should review this URL for best practices and examples for creating your roles structure.