Install Apache with Ansible Playbook on a Linux host

In this blog post, I will show you how to use an Ansible playbook to install Apache web server on a Linux host.

Before you start this process, make sure you have the following:

Install Ansible control node on a Linux host

Add a Linux host to the host’s inventory file

Use passwordless SSH key to connect to Linux hosts

Create a Playbook

On my Linux control host, I will create a playbook using the following command

touch playbook.yaml

I will edit the file using the following code:

---

- hosts: linux
  gather_facts: true
  become: yes
  become_method: sudo
  become_user: root

  tasks:
    - name: install
      package:
        name: httpd
        state: present

In the above code you can see the I am targeting my Linux hosts.

I am using the local package manager (yum) to install Apache.

Note: The state configuration goes like:

present = install

absent = remove

latest = update

I will save the file and run the playbook with the code below:

ansible-playbook playbook.yaml

Processing…
Success! You're on the list.