2017-03-20 110 views
0

我想在主任務中擁有2個「子任務」,但由於某種原因,我收到了語法錯誤。主任務中的有限子任務

在我/etc/ansible目錄我有以下結構:

playbooks/ 
     set_users.yml 
roles/ 
    users/ 
      tasks/ 
       main.yml 
       create_admin.yml 
       update_root.yml 
      vars/ 
       main.yml 

create_admin.yml文件我有以下幾點:

--- 
- name: Create srv_admin user 
    user: name=srv_admin password="{{ admin_password }}" groups=wheel shell=/bin/bash 

而且在update_root.yml

--- 
- name Update root password 
    user: name=root password="{{ root_password }}" 

然後我將這些任務包含在main.yml中:

--- 
- name: Modify users 
    tasks: 
    - include: update_root.yml 
    - include: create_admin.yml 

vars/main.yml包含我的密碼:

--- 
admin_password: SHA512HASH 
root_password: SHA512HAS 

現在把它一起在playbooks/set_users.yml

--- 
- name: create user 
    hosts: servers 
    remote_user: root 
    roles: 
    - users 

但我明明做錯了什麼。當我運行的劇本,我得到以下錯誤:

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

The error appears to have been in '/etc/ansible/roles/users/tasks/main.yml': line 2, column 3, but may 
be elsewhere in the file depending on the exact syntax problem. 

The offending line appears to be: 

--- 
- name: Modify users 
^here 

我如何使用這兩個「子」任務tasks/main.yml,這樣我可以簡單地導入在劇本的角色?

編輯 實施@Konstantin蘇沃洛夫建議後:

The error appears to have been in '/etc/ansible/roles/users/tasks/update_root.yml': line 3, column 7, but may 
be elsewhere in the file depending on the exact syntax problem. 

The offending line appears to be: 

- name Update root password 
    user: name=root password="{{ root_password }}" 
    ^here 
We could be wrong, but this one looks like it might be an issue with 
missing quotes. Always quote template expression brackets when they 
start a value. For instance: 

    with_items: 
     - {{ foo }} 

Should be written as: 

    with_items: 
     - "{{ foo }}" 

回答

3

tasks/main.yml應該是任務列表,以便在tasks:關鍵字沒有必要:

--- 
- include: update_root.yml 
- include: create_admin.yml 

,並避免使用key=value語法,它有時候會用腿拍,使用純YAML:

- name: Update root password 
    user: 
    name: root 
    password: "{{ root_password }}" 
+0

這有幫助,但現在我得到另一個錯誤。請參閱上面的錯誤。 – rhillhouse

+0

已更新的答案。但是,請善待 - 閱讀Ansible的錯誤消息。 –

+0

謝謝!對不起,我對Ansible還是很陌生,並不理解錯誤。我仍然試圖掌握語法。更新的答案還包含語法錯誤,但我可以通過將' - name ...'修改爲' - name:...'來修復它。' – rhillhouse