Dahlgren
Dahlgren

Reputation: 3

Ansible Playbook - Install/Configure Apache Error

We have to install Apache, copy configuration files, and then start and configure it to run.

Here is the playbook written thus far:

---
- 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

However, when I run the command: ansible-playbook playbook.yml, I recieve this:

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

I have tried messing with whitespace and re-arranging things, but I still recieve this error. I am sure it's something small I am missing here, but it is bugging me to no end! Any help would be appreciated! Thanks so much.

Upvotes: 0

Views: 1101

Answers (1)

techraf
techraf

Reputation: 68459

Regarding your error message, you need to pay attention to indentation. This is how it should look like:

---
- 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

But this is rather an example of how not to use Ansible.

I assume you just started to use Ansible and want to verify things, but instead of running everything with command module, you should rather take advantage of native modules, like yum or service. Have a look at the examples in the linked documentation pages.


Also take notice that in your example some tasks have names some don't. For example these are two different tasks (the first one with a name, the seconds one without):

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

More appropriate naming should be:

- 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

Another problem: this command will fail as there should be no httpd-vshosts.conf in the current directory on the target machine:

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

You must provide the full path.

Upvotes: 2

Related Questions