100-Days-Of-DevOps-Challenge-KodeKloud

Managing Jinja2 Templates Using Ansible

One of the Nautilus DevOps team members is working on 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:

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

Steps

  1. Move into ansible directory and check each files

     cd ansible
     ls
     cat inventory
     cat playbook.yml
     ls role
    
  2. Create index.html.j2 file

     vi role/httpd/templates/index.html.j2
    

    copy paste the following line:

     <p> This file was created using Ansible on  </p>
    
  3. Update main.yml of httpd role

     vi role/httpd/tasks/main.yml
    

    add these lines at the below:

     - name: Copy index.html template
       ansible.builtin.template:
         src: index.html.j2
         dest: /var/www/html/index.html
         mode: '0777'
         owner: ""
         group: ""
    

    Or you can replace all contents with this YAML file

  4. Add target hosts in playbook.yml

     hosts: all
    

    playbook.yml file should be look like this:

     ---
     - hosts: all 
       become: yes
       become_user: root
       roles:
         - role/httpd
    
  5. Run the playbook command:

     ansible-playbook -i inventory playbook.yml
    
  6. verify with curl

     curl http://stapp01
     curl http://stapp02
     curl http://stapp03
    

    ansible curl result

  7. Further check?

    login into each app server and run this command

     ls -la /var/www/html
    
     [tony@stapp01 ~]$ ls -la /var/www/html
     total 12
     drwxr-xr-x 2 root root 4096 Oct 25 02:47 .
     drwxr-xr-x 4 root root 4096 Oct 25 02:46 ..
     -rwxrwxrwx 1 tony tony   57 Oct 25 02:47 index.html
     [tony@stapp01 ~]$