Ansible – A playbook for setting up an apache webserver

Here’s a simple playbook that sets up an Apache web server on a RHEL/CentOS 7 box:


You can find all my latest posts on medium.
[root@controller playbooks]# pwd
[root@controller playbooks]# cat httpd.yaml
- name: This sets up an httpd webserver
  hosts: ansibleclient01.local
  - name: Install apache packages 
      name: httpd
      state: present
  - name: ensure httpd is running
      name: httpd 
      state: started
  - name: Open port 80 for http access
      service: http
      permanent: true
      state: enabled
  - name: Restart the firewalld service to load in the firewall changes
      name: firewalld 
      state: restarted

Here’s a breakdown of what the above yaml syntax shows. At the top level is a single item list. This list item houses a hash with 3 key-value pairs. The first to key’s values are of the type string. However the third key’s (tasks) houses a list of 4 items. Each one of these list items contains a hash. Each of these hashes is made up of 2 key-value pairs.

Now let’s run this playbook:

[root@controller playbooks]# pwd
[root@controller playbooks]# ls
$ ansible-playbook httpd.yml

We can test if this has worked, by opening to http://ansibleclient01.local in your web browser.