Skip to content

Latest commit

 

History

History
125 lines (91 loc) · 4.12 KB

Ansible Blockinfile Module.md

File metadata and controls

125 lines (91 loc) · 4.12 KB

Problem Statement


The Nautilus DevOps team wants to install and set up a simple httpd web server on all app servers in Stratos DC. Additionally, they want to deploy a sample web page for now using Ansible only. Therefore, write the required playbook to complete this task. Find more details about the task below.

  • We already have an inventory file under /home/thor/ansible directory on jump host. Create a playbook.yml under /home/thor/ansible directory on jump host itself.

  • Using the playbook, install httpd web server on all app servers. Additionally, make sure its service should up and running.

  • Using blockinfile Ansible module add some content in /var/www/html/index.html file. Below is the content:

Welcome to XfusionCorp!

This is Nautilus sample file, created using Ansible!

Please do not modify this file manually!

  • The /var/www/html/index.html file's user and group owner should be apache on all app servers.

  • The /var/www/html/index.html file's permissions should be 0777 on all app servers.

Note:

i. 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.

ii. Do not use any custom or empty marker for blockinfile module.


Solution

  1. Navigate to the Ansible Directory

    cd /home/thor/ansible
  2. Check the Inventory File

    cat inventory
    stapp01 ansible_host=172.16.238.10 ansible_ssh_pass=Ir0nM@n ansible_user=tony
    stapp02 ansible_host=172.16.238.11 ansible_ssh_pass=Am3ric@ ansible_user=steve
    stapp03 ansible_host=172.16.238.12 ansible_ssh_pass=BigGr33n ansible_user=banner
    
  3. Create the Playbook File

    vi playbook.yml

    Playbook Content:

    ---
    - hosts: all
      become: yes
      tasks:
      - name: Install httpd on all servers
        yum:
          name: httpd
          state: present
        # This task installs the 'httpd' package on all app servers using the 'yum' package manager.
        
      - name: Start httpd service
        service:
          name: httpd
          state: started
        # This task ensures that the 'httpd' service is started on all app servers.
      
      - name: Insert content into /var/www/html/index.html
        ansible.builtin.blockinfile:
          path: /var/www/html/index.html
          create: yes
          group: "apache"
          owner: "apache"
          mode: "0777"
          block: |
            Welcome to XfusionCorp!
            This is a Nautilus sample file, created using Ansible!
            Please do not modify this file manually!
        # This task creates or updates the '/var/www/html/index.html' file with the specified content,
        # sets the file ownership to 'apache', and sets permissions to '0777'.
  4. Run the Ansible Playbook

    ansible-playbook -i inventory playbook.yml
  5. Review the Output

    PLAY [all] ******************************************************************************
    
    TASK [Gathering Facts] ******************************************************************
    ok: [stapp02]
    ok: [stapp03]
    ok: [stapp01]
    
    TASK [Install httpd on all servers] *****************************************************
    ok: [stapp02]
    ok: [stapp01]
    ok: [stapp03]
    
    TASK [Start httpd service] **************************************************************
    ok: [stapp02]
    ok: [stapp03]
    ok: [stapp01]
    
    TASK [Insert content into /var/www/html/index.html] *****************************************************
    changed: [stapp01]
    changed: [stapp03]
    changed: [stapp02]
    
    PLAY RECAP ******************************************************************************
    stapp01                    : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    stapp02                    : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    stapp03                    : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0