Ansible 剧本 - Install/Configure Apache 错误

Ansible Playbook - Install/Configure Apache Error

我们必须安装 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

我试过乱用空格并重新排列,但我仍然收到此错误。我确定这是我在这里遗漏的一些小东西,但这让我无休止!任何帮助,将不胜感激!非常感谢。

关于你的错误信息,需要注意缩进。它应该是这样的:

---
- 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 模块,您应该利用原生模块,例如 yum or service.查看链接文档页面中的示例。


另请注意,在您的示例中,有些任务有名称,有些则没有。例如,这是两个不同的任务(第一个有名字,第二个没有):

- 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

您必须提供完整路径。