ansible角色无法找到模块

时间:2016-10-03 15:49:26

标签: ansible ansible-2.x

我有一个test.yml文件,它有一个从git签出代码的角色(两者都在下面复制):

site.yml

---
- hosts: webservers
  user: me
  roles: 
  - role: test-role
测试角色/任务中的

main.yml文件

---

- 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

主机:

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

当我运行以下内容时:

ansible-playbook -i hosts site.yml

我在下面复制了异常,引用了角色的tasks部分中的main.yml文件。任何想法我可能错误配置。

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

更新 根据康斯坦丁的反馈尝试改变,但我或多或少地得到了同样的例外:

角色/测试角色/默认/ main.yml

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

任务/ 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

异常: 现在它抱怨下面这一行。我在运行语法检查时得到相同的响应。

---
- name: fetching my repo
  ^ here

更新 跟随康斯坦丁和阿瓦隆的回答 - 我能够成功地完成任务。

2 个答案:

答案 0 :(得分:1)

main.yml for role应该只包含任务列表,不包含任何其他内容。

将文件拆分为test-role/defaults/main.yml(变量):

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

test-role/tasks/main.yml(任务):

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

答案 1 :(得分:1)

要添加康斯坦丁所说的内容,您的任务应该只包含任务。变量需要在单独的文件中定义。此外,您的语法/格式可以改进。

<强> site.yml

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

<强>角色/测试角色/任务/ main.yml

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

<强>角色/测试角色/瓦尔/ main.yml

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

另外,我建议您在ansible.cfg文件中设置库存路径。配置文件可以在/etc/ansible.cfg中,也可以在与剧本相同的目录中(我个人与我的剧本位于同一目录中)。

<强> ansible.cfg

inventory      = inventory/hosts

如果您这样做,则在运行Playbook时不必指定库存文件位置。如果没有别的,它将为您节省一些按键。

您应该查看this URL,了解有关创建角色结构的最佳做法和示例。

相关问题