2016-12-06 115 views
0

我們必須安裝Apache,複製配置文件,然後啓動並配置它運行。Ansible Playbook - 安裝/配置Apache錯誤

這是迄今爲止寫的劇本:

--- 
- hosts: example 

tasks: 
- name:Install Apache 
    command: yum install --quiet -y httpd httpd-devel 
- name:Copy configuration files 
    command:> 
    cp httpd.conf /etc/httpd/conf/httpd.conf 
- command:> 
    cp httpd-vshosts.conf /etc/httpd/conf/httpd-vshosts.conf 
- name:Start Apache and configure it to run 
    command: service httpd start 
- command: chkconfig httpd on 

然而,當我運行命令:ansible-playbook playbook.yml,我收到這樣的:

error: ERROR! Syntax Error while loading YAML. 
The error appears to have been in '/etc/ansible/playbook.yml': line 3, column 1, but may 
be elsewhere in the file depending on the exact syntax problem. 

The offending line appears to be: 

- hosts: example 
tasks: 
^ here 

我試圖用空白,並重新安排搞亂事情,但我仍然收到這個錯誤。我相信這是我在這裏想念的小事,但它卻讓我無法接受!任何幫助,將不勝感激!非常感謝。

回答

2

關於您的錯誤消息,您需要注意縮進。這是應該的樣子:

--- 
- hosts: example 

    tasks: 
    - name: Install Apache 
     command: yum install --quiet -y httpd httpd-devel 
    - name: Copy configuration files 
     command: cp httpd.conf /etc/httpd/conf/httpd.conf 
    - command: cp httpd-vshosts.conf /etc/httpd/conf/httpd-vshosts.conf 
    - name: Start Apache and configure it to run 
     command: service httpd start 
    - command: chkconfig httpd on 

但是,這是相當如何不使用 Ansible一個例子。

我假設你剛開始使用Ansible,並希望確認的東西,但不是運行與command模塊的一切,你應該寧願冒機模塊的優勢,像yumservice。查看鏈接文檔頁面中的示例。


另請注意,在您的示例中,某些任務的名稱有些沒有。例如這是兩個不同的任務(第一個與一個名字,使秒針一個沒有):

- name: Copy configuration files 
    command: cp httpd.conf /etc/httpd/conf/httpd.conf 
- command: cp httpd-vshosts.conf /etc/httpd/conf/httpd-vshosts.conf 

更多相應的命名應該是:

- name: Copy one configuration file 
    command: cp httpd.conf /etc/httpd/conf/httpd.conf 
- name: Copy another configuration file 
    command: cp httpd-vshosts.conf /etc/httpd/conf/httpd-vshosts.conf 

另一個問題是:此命令將失敗,因爲目標機器上當前目錄中應該沒有httpd-vshosts.conf

- command: cp httpd-vshosts.conf /etc/httpd/conf/httpd-vshosts.conf 

您必須提供完整路徑。

+0

非常感謝。它爲我工作..問題是與空白! –