The Nautilus DevOps team want to install and set up a simple httpd web server on all app servers in Stratos DC. They also want to deploy a sample web page using Ansible. Therefore, write the required playbook to complete this task as per details mentioned below.
We already have an inventory file under /home/thor/ansible directory on jump host. Write a playbook playbook.yml under /home/thor/ansible directory on jump host itself. Using the playbook perform below given tasks:
- Install
httpdweb server on all app servers, and make sure its service is up and running. - Create a file
/var/www/html/index.htmlwith content:
This is a Nautilus sample file, created using Ansible!
- Using
lineinfileAnsible module add some more content in/var/www/html/index.htmlfile. Below is the content:
Welcome to xFusionCorp Industries!
Also make sure this new line is added at the top of the file.
- The
/var/www/html/index.htmlfile's user and groupownershould beapacheon all app servers. - The
/var/www/html/index.htmlfile's permissions should be0655on all app servers.
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.
The Requirements
The task had the following specific requirements:
- Install httpd on all app servers and ensure the service is running.
- Create
/var/www/html/index.htmlwith the content:This is a Nautilus sample file, created using Ansible! - Use the
lineinfilemodule to addWelcome to xFusionCorp Industries!at the top of the file. - Set the file owner and group to
apacheon all app servers. - Set the file permissions to
0655on all app servers. - The playbook must be at
/home/thor/ansible/playbook.ymland must run with the exact commandansible-playbook -i inventory playbook.yml.
Step 1: Navigate to the Working Directory
cd /home/thor/ansible
Verify the inventory file:
cat inventory
Output:
stapp01 ansible_host=stapp01 ansible_ssh_pass=Ir0nM@n ansible_user=tony
stapp02 ansible_host=stapp02 ansible_ssh_pass=Am3ric@ ansible_user=steve
stapp03 ansible_host=stapp03 ansible_ssh_pass=BigGr33n ansible_user=banner
This is a flat inventory with three hosts. The playbook will use hosts: all to target them.
Step 2: Create the Playbook
Open the playbook file:
vi /home/thor/ansible/playbook.yml
Add the following content:
---
- name: Install httpd and deploy sample index.html
hosts: all
become: true
tasks:
- name: Install httpd package
yum:
name: httpd
state: present
- name: Start and enable httpd service
systemd:
name: httpd
state: started
enabled: yes
- name: Create index.html with initial content
copy:
dest: /var/www/html/index.html
content: "This is a Nautilus sample file, created using Ansible!\n"
owner: apache
group: apache
mode: '0655'
- name: Add welcome message at the top of index.html
lineinfile:
path: /var/www/html/index.html
line: "Welcome to xFusionCorp Industries!"
insertbefore: BOF
- name: Ensure correct ownership and permissions on index.html
file:
path: /var/www/html/index.html
owner: apache
group: apache
mode: '0655'
Save the file and exit the editor.
Understanding Each Task
Install httpd package uses the yum module with state: present. This installs the package if it is not already present and does nothing if it is. This makes the task idempotent.
Start and enable httpd service uses the systemd module. The state: started parameter ensures the service is running right now. The enabled: yes parameter ensures it starts automatically at boot.
Create index.html with initial content uses the copy module with the content parameter. This writes the given string directly to the file. An important detail here is that copy overwrites the file if it already exists. On these lab servers, an index.html may already exist. If you used lineinfile with create: true for this step, the new content would be appended to the existing content instead of replacing it, which would break the validation.
Add welcome message at the top of index.html uses the lineinfile module. The insertbefore: BOF parameter places the new line at the beginning of the file, satisfying the requirement that the welcome message be at the top.
Ensure correct ownership and permissions on index.html uses the file module to set owner, group, and mode in a final, explicit step. This guarantees the final state is correct even if earlier modules behaved differently than expected.
Step 3: Validate the Syntax
ansible-playbook -i inventory playbook.yml --syntax-check
Output:
playbook: playbook.yml
A successful syntax check returns the playbook filename with no error messages.
Step 4: Run the Playbook
ansible-playbook -i inventory playbook.yml
Output:
PLAY [Install httpd and deploy sample index.html] ************************************************************
TASK [Gathering Facts] ***************************************************************************************
ok: [stapp01]
ok: [stapp03]
ok: [stapp02]
TASK [Install httpd package] *********************************************************************************
changed: [stapp03]
changed: [stapp01]
changed: [stapp02]
TASK [Start and enable httpd service] ************************************************************************
changed: [stapp03]
changed: [stapp02]
changed: [stapp01]
TASK [Create index.html with initial content] ****************************************************************
changed: [stapp01]
changed: [stapp03]
changed: [stapp02]
TASK [Add welcome message at the top of index.html] **********************************************************
changed: [stapp01]
changed: [stapp03]
changed: [stapp02]
TASK [Ensure correct ownership and permissions on index.html] ************************************************
ok: [stapp01]
ok: [stapp03]
ok: [stapp02]
PLAY RECAP ***************************************************************************************************
stapp01 : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
stapp02 : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
stapp03 : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
The play recap confirms the playbook ran successfully on all three servers. Each host shows changed=4, meaning four tasks made actual changes. The final file task reports ok because ownership and permissions were already set correctly by the copy task earlier in the play.
Step 5: Verify the File Content
Check that the file contains both lines in the correct order:
ansible all -i inventory -a "cat /var/www/html/index.html"
Output:
stapp01 | CHANGED | rc=0 >>
Welcome to xFusionCorp Industries!
This is a Nautilus sample file, created using Ansible!
stapp03 | CHANGED | rc=0 >>
Welcome to xFusionCorp Industries!
This is a Nautilus sample file, created using Ansible!
stapp02 | CHANGED | rc=0 >>
Welcome to xFusionCorp Industries!
This is a Nautilus sample file, created using Ansible!
The welcome message appears as the first line on all three servers, exactly as required. The second line contains the original content created by the copy module.
Step 6: Verify Ownership and Permissions
Check that the file has the correct owner, group, and mode:
ansible all -i inventory -a "stat -c '%a %U %G' /var/www/html/index.html"
Output:
stapp01 | CHANGED | rc=0 >>
655 apache apache
stapp02 | CHANGED | rc=0 >>
655 apache apache
stapp03 | CHANGED | rc=0 >>
655 apache apache
The output shows 655 for permissions, apache for the user, and apache for the group on all three servers. This matches the requirements exactly.
Why copy and lineinfile Are Both Used
A common question is why the playbook uses copy for the initial content and lineinfile for the second line. The reason is that the task explicitly requires using the lineinfile module, and the two modules serve different purposes.
The copy module with the content parameter writes a fresh file, overwriting whatever was there before. This guarantees a clean starting state.
The lineinfile module manages individual lines in a file. With insertbefore: BOF, it places a new line at the beginning without disturbing other content. This is exactly what the task asked for.
If the initial content had been created with lineinfile and create: true, the module would have appended the content to whatever was already in the file. On these lab servers, an existing index.html could contain different text, and the validation would fail because the file would have extra content.
Key Concepts Demonstrated
| Concept | What It Does in This Playbook |
|---|---|
yum: state=present |
Installs the package idempotently |
systemd: state=started enabled=yes |
Starts the service and enables it at boot |
copy: content=... |
Writes exact content to a file, overwriting existing content |
lineinfile: insertbefore=BOF |
Inserts a line at the beginning of a file |
file: owner/group/mode |
Ensures ownership and permissions are correct |
hosts: all |
Targets all hosts in the flat inventory |
Final Playbook
---
- name: Install httpd and deploy sample index.html
hosts: all
become: true
tasks:
- name: Install httpd package
yum:
name: httpd
state: present
- name: Start and enable httpd service
systemd:
name: httpd
state: started
enabled: yes
- name: Create index.html with initial content
copy:
dest: /var/www/html/index.html
content: "This is a Nautilus sample file, created using Ansible!\n"
owner: apache
group: apache
mode: '0655'
- name: Add welcome message at the top of index.html
lineinfile:
path: /var/www/html/index.html
line: "Welcome to xFusionCorp Industries!"
insertbefore: BOF
- name: Ensure correct ownership and permissions on index.html
file:
path: /var/www/html/index.html
owner: apache
group: apache
mode: '0655'
Conclusion
This exercise combines several foundational Ansible modules into a realistic deployment scenario. The playbook installs a web server, starts and enables the service, deploys a sample page, modifies it using the lineinfile module, and enforces ownership and permissions.
The most important lesson is understanding when to use copy versus lineinfile. The copy module is best for creating or overwriting files with known content. The lineinfile module is best for managing individual lines within a file. Using the right module for each purpose keeps the playbook predictable and idempotent.
Verification with cat and stat confirms that the desired state was achieved on every host. This practice of verifying after running should become a habit in any automation workflow.
Top comments (0)