2016-10-03 107 views
0

我有一個具有從git的作用結賬代碼(以下兩個拷貝)一個test.yml文件:ansible角色無法找到模塊

site.yml

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

爲主。在測試角色YML文件/任務

--- 

- 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 

主機:

[webservers] 
testserver ansible_ssh_host=xxx.xxx.xxx.xxxx ansible_ssh_port=xxxx 
當我運行以下

ansible-playbook -i hosts site.yml 

我得到複製下面引用了角色的任務部分的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: [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 

例外: 現在它抱怨下面的行。當我運行語法檢查時,我得到相同的響應。

--- 
- name: fetching my repo 
^here 

更新 其次康斯坦丁和Avalon的答案 - 我能成功運行任務。

回答

1

main.yml對於角色應該只包含任務列表,沒有別的。

分割你的文件轉換成test-role/defaults/main.yml(變量):

--- 
my_repo_url: [email protected]: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 
+0

spaciba - 更新我的原始文章與您的建議,但它不工作 - 任何想法 - 在此先感謝 –

+0

我的代碼是不同的,請密切關注,請。 –

+0

spaciba - 兩個答案同樣有用 –

1

要添加到什麼康斯坦丁說,你的任務應該只包含任務。變量需要在單獨的文件中定義。此外,你的語法/格式可以改進。

site.yml

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

角色/測試角色/任務/主。陽明海運

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

角色/測試角色/瓦爾/ main.yml

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

另外,我建議設置你的ansible.cfg文件庫存路徑。配置文件可以在/etc/ansible.cfg中,也可以在與您的劇本相同的目錄中(我個人將它放在與我的劇本相同的目錄中)。

ansible.cfg

inventory  = inventory/hosts 

如果你這樣做,你會不會當你運行你的劇本到指定目錄文件的位置。如果沒有別的,它會爲您節省幾個按鍵。

您應該查看this URL瞭解創建角色結構的最佳實踐和示例。

+0

謝謝 - 將嘗試 –

+0

謝謝 - 作品像一個魅力。自從它進來後,我接受了康斯坦丁的回答(兩者都是upvoted)。 –