Create a Custom Script & Add the file on Slaves Machine via Playbook— Ansible Assignment 2
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:
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
Step 2: Create a script file using the below-given command:
sudo nano assignment2.sh
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
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
Step 5: Run the below-given command to view the 1.txt file here.
ls
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
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
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.
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
The playbook has been successfully run.
Step 5: Now, we will run the playbook using the below-given command:
ansible-playbook play2.yaml
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
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
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