Ansible Setup Httpd and PHP

Ticker

6/recent/ticker-posts

Ansible Setup Httpd and PHP

Question  Nautilus Application development team wants to test an Apache and PHP setup on one of the app servers in Stratos Datacenter. They want the DevOps team to prepare an Ansible playbook to accomplish this task. Below you can find more details about the task.

There is an inventory file ~/playbooks/inventory on jump host.

Create a playbook ~/playbooks/httpd.yml on jump host and perform the following tasks on App Server 2.

a. Install httpd and php packages (whatever default version is available in yum repo).

b. Change default document root of Apache to /var/www/html/myroot in default Apache config /etc/httpd/conf/httpd.conf. Make sure /var/www/html/myroot path exists (if not please create the same).

c. There is a template ~/playbooks/templates/phpinfo.php.j2 on jump host. Copy this template to the Apache document root you created as phpinfo.php file and make sure user owner and the group owner for this file is apache user.

d. Start and enable httpd service.

Note: Validation will try to run playbook using command ansible-playbook -i inventory httpd.yml, so please make sure playbook works this way without passing any extra arguments.

Please Note :-  Perform the below commands based on your question  server,  user name & other details might differ . So please read task carefully before executing. All the Best 👍

Solution:  

1Go through the folder mentioned in task and create inventory & playbook files   

thor@jump_host /$ cd  ~/playbooks/

thor@jump_host ~/playbooks$

thor@jump_host ~/playbooks$ ll

total 12

-rw-r--r-- 1 thor thor   36 Jun 17 14:28 ansible.cfg

-rw-r--r-- 1 thor thor  237 Jun 17 14:28 inventory

drwxr-xr-x 2 thor thor 4096 Jun 17 14:29 templates

thor@jump_host ~/playbooks$


2Create a playbook file  httpd.yml as per the task  ( refer Video below for clarity )

thor@jump_host ~/playbooks$ vi httpd.yml

thor@jump_host ~/playbooks$ cat httpd.yml

- name: Setup Httpd and PHP

  hosts: stapp02

  become: yes

  tasks:

    - name: Install latest version of httpd and php

      package:

        name:

          - httpd

          - php

        state: latest

    - name: Replace default DocumentRoot in httpd.conf

      replace:

        path: /etc/httpd/conf/httpd.conf

        regexp: DocumentRoot \"\/var\/www\/html\"

        replace: DocumentRoot "/var/www/html/myroot"

    - name: Create the new DocumentRoot directory if it does not exist

      file:

        path: /var/www/html/myroot

        state: directory

        owner: apache

        group: apache

    - name: Use Jinja2 template to generate phpinfo.php

      template:

        src: /home/thor/playbooks/templates/phpinfo.php.j2

        dest: /var/www/html/myroot/phpinfo.php

        owner: apache

        group: apache

    - name: Start and enable service httpd

      service:

        name: httpd

        state: started

        enabled: yes 


3. Post file saved , run below command to execute the playbook
  

thor@jump_host ~/playbooks$ ansible-playbook  -i inventory httpd.yml

 

PLAY [Setup Httpd and PHP] ******************************************************************

 

TASK [Gathering Facts] **********************************************************************

ok: [stapp02]

 

TASK [Install latest version of httpd and php] **********************************************

changed: [stapp02]

 

TASK [Replace default DocumentRoot in httpd.conf] *******************************************

changed: [stapp02]

 

TASK [Create the new DocumentRoot directory if it does not exist] ***************************

changed: [stapp02]

 

TASK [Use Jinja2 template to generate phpinfo.php] ******************************************

changed: [stapp02]

 

TASK [Start and enable service httpd] *******************************************************

changed: [stapp02]

 

PLAY RECAP **********************************************************************************

stapp02                    : ok=6    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

 thor@jump_host ~/playbooks$



4. validate the task by login on app server and check
    

[steve@stapp02 ~]$ rpm -qa |grep httpd

httpd-tools-2.4.6-97.el7.centos.x86_64

httpd-2.4.6-97.el7.centos.x86_64

[steve@stapp02 ~]$

[steve@stapp02 ~]$ rpm -qa |grep php

php-cli-5.4.16-48.el7.x86_64

php-common-5.4.16-48.el7.x86_64

php-5.4.16-48.el7.x86_64

[steve@stapp02 ~]$


5. Click on Finish & Confirm to complete the task successful

Happy Learning!!!!


Apart from this if you need more clarity,  I have made a  tutorial video on this , please go through and share your comments. Like and share the knowledge



Post a Comment

0 Comments

Latest Posts

KodeKloud Kubernetes Security CKS  Lab Challenge 4 |  Audit-policy | Install & configure falco utility | Inspect the API server audit logs and identify the user