Create a Custom Script & Add the file on Slaves Machine via Playbook— Ansible Assignment 2

Visal Tyagi
DevOps-Guides
Published in
4 min readJun 10, 2024

Tasks to Be Performed:

1. Create a script that can add the text “This text has been added by custom script” to /tmp. 1.txt

2. Run this script using Ansible on all the hosts

Before checking this assignment solution, please check the Ansible-Assignment-1 Solution Also.

Check the Git Hub Repository for this Assignment to Copy the Commands:

Create a Custom Script & Install the file on Slaves Machine via Playbook- Ansible Assignment 2
Create a Custom Script & Install the file on Slaves Machine via Playbook — Ansible Assignment 2

Problem (1) Solution: Create a script that can add the text “This text has been added by custom script” to /tmp. 1.txt

Step 1: Go to the tmp directory using the below-given command:

cd /tmp
Go to the /tmp directory
Go to the /tmp directory

Step 2: Create a script file using the below-given command:

sudo nano assignment2.sh
Create a Script File
Create a Script File

Press “enter” from the keyboard & an empty file opens here.

Step 3: Type the below-given command here to display the content in “1.txt”:

echo "This text has been added by custom script" > /tmp/1.txt
Write the content in 1.txt file
Write the content in a 1.txt file

Do “CTRL+X” to exit the file & press “Yes” to save the file. In the end, press “enter” from the keyboard to save & exit the file.

Step 4: Run the script to add “1.txt” in the “/tmp” location. Use the below-given command to execute the script.

bash assignment2.sh
Run the Script
Run the Script

Step 5: Run the below-given command to view the 1.txt file here.

ls
View the “1.txt” file
View the “1.txt” file

The “1.txt” has been successfully added to the “/tmp” location.

Problem (2) Solution: Run this script using Ansible on all the hosts

Follow these steps here:

A. Create a play2.yaml file to Create Files using Script on “Slave1” & “Slave2” Also

Step 1: Create a “play2.yaml” file to execute the script. Use the below-given command to create the “play2.yaml” file:

sudo nano play2.yaml  
Create a playbook
Create a playbook

Step 2: Write the “yaml file to execute the script like this:

---
- name: creating the file using script
hosts: all
become: true
tasks:
- name: running script
script: assignment2.sh
Paste the script here
Paste the script here

Do “CTRL+X” to exit the file & press “Yes” to save the file. In the end, press “enter” from the keyboard to save & exit the file.

Step 3: Now, we will check the syntax of the “play2.yaml” file using the below-given command:

ansible-playbook play2.yaml --syntax-check

Press “enter” from the keyboard to execute this command.

Syntax Check
Syntax Check

The syntax is correct if the playbook shows as it is.

Step 4: Now, we will dry run the “play2.yaml” file using the below-given command:

ansible-playbook play2.yaml --check
Dry Run the Playbook
Dry Run the Playbook

The playbook has been successfully run.

Step 5: Now, we will run the playbook using the below-given command:

ansible-playbook play2.yaml
Playbook Successfully Run
Playbook Successfully Run

The “play2.yaml” has been successfully executed.

B. Check “1.txt” Present in Slave1 & Slave2 or Not

Step 1: Go to the “Slave1” & change the directory to tmp using the command

cd /tmp

Run this command to view the 1.txt file here:

ls
Go to the /tmp directory in Slave1
Go to the /tmp directory in Slave1

The “1.txt” has been successfully shown.

Step 2: Go to the “Slave2” & change the directory to tmp using the command

cd /tmp

Run this command to view the 1.txt file here:

ls
Create a Custom Script & Install the file on Slaves Machine via Playbook- Ansible Assignment 2
Go to the /tmp directory in Slave2

The “1.txt” has been successfully shown.

Check Other Ansible Assignments Here:

Install Apache2 & NGINX on Slaves Machine using Ansible Roles — Ansible Assignment 3

Replace index.html With Original index.html file using Ansible Roles — Ansible Assignment 4

Install Java and MySQL on Test and Prod Nodes Using Ansible Roles — Ansible Assignment 5

Create Two Server Groups & Install Apache on The First Group & NGINX on the Second Group — Ansible Case Study

--

--