Question : One of the Nautilus DevOps team members is working to develop a role for httpd installation and configuration. Work is almost completed, however there is a requirement to add a jinja2 template for index.html file. Additionally, the relevant task needs to be added inside the role. The inventory file ~/ansible/inventory is already present on jump host that can be used. Complete the task as per details mentioned below:
a. Update ~/ansible/playbook.yml playbook to run the httpd role on App Server 2.
b. Create a jinja2 template index.html.j2 under /home/thor/ansible/role/httpd/templates/ and add a line This file was created using Ansible on <respective server> (for example This file was created using Ansible on stapp01 in case of App Server 1). Also please make sure not to hard code the server name inside the template. Instead, use inventory_hostname variable to fetch the correct value.
c. Add a task inside /home/thor/ansible/role/httpd/tasks/main.yml to copy this template on App Server 2 in /var/www/html/index.html. Also make sure that /var/www/html/index.html file permissions 0644.
d. The user/group owner of /var/www/html/index.html file must be respective sudo user of server (for example tony in case of stapp01).
1. Go through the folder mentioned in task and verified the playbook
thor@jump_host
/$ cd ~/ansible/ thor@jump_host
~/ansible$ thor@jump_host
~/ansible$ ll total 12 -rw-r--r-- 1
thor thor 237 Jul 19 14:39 inventory -rw-r--r-- 1
thor thor 73 Jul 19 14:39
playbook.yml drwxr-xr-x 3
thor thor 4096 Jul 19 14:40 role thor@jump_host
~/ansible$ |
thor@jump_host
~/ansible$ cat playbook.yml --- - hosts: become: yes become_user: root roles: - role/httpd thor@jump_host ~/ansible$ thor@jump_host
~/ansible$ vi playbook.yml thor@jump_host
~/ansible$ cat playbook.yml --- - hosts: stapp02
become: yes become_user: root roles: - role/httpd thor@jump_host
~/ansible$ |
thor@jump_host
~/ansible$ vi /home/thor/ansible/role/httpd/templates/index.html.j2 thor@jump_host
~/ansible$ cat
/home/thor/ansible/role/httpd/templates/index.html.j2 This file was
created using Ansible on {{ ansible_hostname }} thor@jump_host
~/ansible$ |
thor@jump_host
~/ansible$ vi /home/thor/ansible/role/httpd/tasks/main.yml thor@jump_host
~/ansible$ cat
/home/thor/ansible/role/httpd/tasks/main.yml --- #task file for
role/test - name: install
the latest version of httpd yum: name: httpd state: latest - name: Start
service httpd service: name: httpd state: started - name: Use
Jinja2 template to generate index.html template: src:
/home/thor/ansible/role/httpd/templates/index.html.j2 dest: /var/www/html/index.html mode: "0644" owner: "{{ ansible_user }}" group: "{{ ansible_user }}" thor@jump_host
~/ansible$ |
thor@jump_host
~/ansible$ ansible-playbook -i
inventory playbook.yml ok: [stapp02] changed:
[stapp02] changed: [stapp02] changed:
[stapp02] stapp02 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
thor@jump_host
~/ansible$ ansible stapp02 -a "cat /var/www/html/index.html" -i
inventory stapp02 |
CHANGED | rc=0 >> This file was created
using Ansible on stapp02 thor@jump_host
~/ansible$ |
Happy Learning!!!!
0 Comments