Ansible Setup Httpd and PHP Kodekloud

 

Question: Nautilus Application development team wants to test the 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 3.

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 the playbook using command ansible-playbook -i inventory httpd.yml, so please make sure the playbook works this way without passing any extra arguments.

Solution:

In thor@jump_host 

cd ~/playbooks

vi httpd.yml

- name: Setup Httpd and PHP
hosts: stapp03
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

 Here the server is stapp03 but it will be dependent on question

Now run this ansible command

ansible-playbook  -i inventory httpd.yml

To check we can go to the server

ssh banner@stapp03

rpm -qa | grep httpd

rpm -qa | grep php


Download Coding Interview Book and Get More Tutorials for Coding and Interview Solution: Click Here

Download System Design Interview Book and Get More Tutorials and Interview Solution: Click Here

Do you need more Guidance or Help? Then Book 1:1 Quick Call with Me: Click Here

Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment