[root@DevopsRoles ~]# mkdir ansible-nginx/roles
[root@DevopsRoles ~]# cd ansible-nginx/roles
[root@DevopsRoles roles]#
create the role for Nginx and PHP. Using ansible-galaxy creates a template
[root@DevopsRoles roles]# ansible-galaxy init nginx
- nginx was created successfully
[root@DevopsRoles roles]# ansible-galaxy init php
- php was created successfully
The confirm installed Nginx and PHP on a host web server
Nginx version
PHP version
Remember to customize the playbook and PHP-FPM configuration based on your specific requirements.
Conclusion
Through the article, you can use the Ansible playbook NGINX + PHP as above. I hope will this your helpful. For more details refer to the Ansible tutorial.
#This is a jinja template file.
{{ message }}
#jinja template can extract variables. like, ...
{% for key,value in Info.iteritems() %}
I am {{ value.age }} {{ key }} !
{% endfor %}
In the jinja template, you can also use variables in almost the same way as in Playbook. I can also iterate with for statement like with_dict, but here you can use it without putting the item.
Check Mode is a mode that does not actually change, you can check which part will be changed when you run Playbook. Generally, it is called Dry run mode for Ansible. If you give –check as an option, it works in Check Mode.
The tasks are described collectively in one YAML file so far, but this does not work well when creating similar hosts. Let’s cut out some of the tasks as roles and try to make them into parts.
Do not say that handling with_items is not good just because I just cut out (lol)
The role called by roles is searched under the name under the roles directory, and main.yml directly under tasks is executed. You can use dependencies etc to have a dependency on the role, but we will not handle it this time.
This tutorial provides a basic introduction to get you started with the Ansible tutorial. As you gain more experience, you can explore advanced topics like inventory management, playbook organization, and integration with other tools and systems.
Through the article, you can use Ansible tutorial beginners as above. I hope will this your helpful. For more details refer to Ansible tutorial.
How to Bash sleep until time or delay a specific amount of time. How do I pause for 5 seconds in a bash shell script on a Linux? Bash script the essential for DevOps Roles.
To make a Bash script sleep until a specific time, you can use the sleep command in combination with the date command to calculate the remaining time until the desired time is reached.
In Linux, the sleep command to add delay for a specified amount of time. The syntax sleep command
How to pass arguments into a function? How to Bash all arguments into a function? Bash script the essential for DevOps Roles.
Use variable $1, $2, $3 …$n to access argument pass to the function.
Bash script arguments
$# Number of arguments
$@ All arguments, starting from first
$1 First argument
Example
#!/bin/bash
function Name() {
arg1=Argument1;
arg2=Argument2;
arg3=Argument3;
if [ $# -eq 3 ]; then
arg1=$1;
arg2=$2;
arg3=$3;
fi
echo "1st argument: $arg1"
echo "2nd argument: $arg2"
echo "3nd argument: $arg3"
}
Name $1 $2 $3
To invoke the function use syntax:
Name huu phan www.devopsroles.com
Where
Name: Function Name huu: Argument #1 passed into the function phan: Argument #2 passed into the function www.devopsroles.com: Argument #3 passed into the function
Bash all arguments into a function
#!/bin/bash
function Name() {
args=("$@")
echo "Number of arguments: $#"
echo "1st argument: ${args[0]}"
echo "2nd argument: ${args[1]}"
echo "3nd argument: ${args[2]}"
}
Name $1 $2 $3
The screen output terminal:
Conclusion
Thought the article, you can use Bash script arguments into a function as above. I hope will this your helpful. More details refer to Bash script.
How do I check bash script is being run as a root user or not. when creating a new account a user ID is assigned to each user. Bash shell store value user ID is $UID and $EUID variable. Bash script the essential for DevOps Roles.
Example, To check bash script run by root or not.
Using $UID variable
#!/bin/bash
if [ "$(id -u)" != "0" ]; then
echo "Please run by root user" 2>&1
exit 1
else
echo "Mounting...."
mount /dev/sdb4 /mnt/disk4
fi
Using $EUID variable
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
echo "Please run by root user" 2>&1
exit 1
else
echo "Mounting...."
mount /dev/sdb4 /mnt/disk4
fi
Conclusion
Thought the article, you can use Bash script run by root or not as above. I hope will this your helpful. More details refer to Bash script.
How to debug a bash script? you can investigate the causes of the problems so you can apply fixes. Yeah, I will guide you turn on debug bash script. Bash script the essential for DevOps Roles.
Bash scripting is a powerful tool for automating tasks in Linux and Unix environments. However, like any programming language, debugging errors in Bash scripts can be challenging without the right techniques.
In this guide, we’ll explore how to debug Bash scripts effectively, from basic troubleshooting methods to advanced debugging tools. Whether you’re a beginner or an experienced sysadmin, mastering these techniques will save you time and help avoid unnecessary frustrations.
Why Debugging Bash Scripts is Important
Debugging is crucial to ensure your scripts function as intended. Bash scripts often interact with files, processes, and other programs, making it essential to quickly identify and resolve errors. Debugging not only improves script reliability but also deepens your understanding of Bash’s functionality.
How to Debug Bash Scripts: Techniques and Tools
1. Use the set Command for Debugging
The set command enables or disables shell options, making it highly useful for debugging. Common options include:
-x: Displays each command and its arguments as they are executed.
-e: Exits immediately if a command returns a non-zero status.
-u: Treats unset variables as errors.
Example:
#!/bin/bash
set -x # Enable debugging
# Example script
name="Debugging"
echo "Hello, $name!"
set +x # Disable debugging
2. Use bash -x for Script Debugging
Run your script with the -x option to trace each command:
bash -x your_script.sh
This provides a detailed execution trace, helping you pinpoint where errors occur.
3. Add echo Statements for Manual Debugging
Using echo is a simple yet effective way to debug scripts. Insert echo statements at key points to check variable values and script flow.
Debugging Bash scripts may seem daunting, but with the right tools and techniques, it becomes manageable. From leveraging set -x and trap to employing tools like ShellCheck and strace, there’s a method for every scenario. By mastering these strategies, you’ll write more robust and error-free scripts.
Debugging is not just about fixing errors-it’s a learning process that deepens your understanding of Bash. Start with the basics, experiment with advanced tools, and continuously refine your approach. I hope will this your helpful. More details refer to Bash script.
How to BASH create a new file with content in Linux? In Bash scripting, there are multiple ways to use Bash create new file and populate it with content.
In this tutorial, we will explore three common methods to create a new file with content in Bash. You can use the commands line below
touch command to create an empty file: touch FILENAME
cat command to create a text file: cat > FILENAME
Write the contents to a file: cat << “EOF” > /path/to/yourfilename write the contents to a file Use cat HERO DOCUMENT write content file EOF
For example, My bash create new file with content.
I will create file jarservice in /etc/init.d/jarservice to java run jar as service on centos 6 as below.
#!/bin/bash
# Author: HuuPV2
yourservice=jarservice
touch /etc/init.d/$yourservice
cat << "EOF" > /etc/init.d/$yourservice
#!/bin/bash
SERVICE_NAME=$yourservice
PATH_TO_JAR=/path/to/yourapplication.jar
LOG_DIR=/var/log/yourapplication.log
PID_PATH_FILE=/var/run/yourapplication.pid
case $1 in
start)
echo "Starting $SERVICE_NAME ..."
if [ ! -f $PID_PATH_FILE ]; then
nohup java -jar $PATH_TO_JAR >> $LOG_DIR 2>&1&
echo $! > $PID_PATH_FILE
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is already running ..."
fi
;;
stop)
if [ -f $PID_PATH_FILE ]; then
PID=$(cat $PID_PATH_FILE);
echo "$SERVICE_NAME stoping ..."
kill $PID;
echo "$SERVICE_NAME stopped ..."
rm $PID_PATH_FILE
else
echo "$SERVICE_NAME is not running ..."
fi
;;
*)
echo $"Usage: $0 {start|stop}"
exit 2
esac
EOF
chmod +x /etc/init.d/$yourservice
echo "Welcome to www.devopsroles.com"
Creating a new file with content in Bash is a straightforward task. By utilizing methods such as echo with output redirection, here documents, or printf with output redirection, you can generate files with ease.
Through the article, you can use Bash create new file with content as above. I hope will this your helpful. For more details refer to Bash script.
Use Bash script check if the directory exists or not exists. In this tutorial, I will check FILE/FOLDER exists or NOT exists and Check Symbolic Link. Bash script the essential for DevOps Roles.
if [ -d "/path/to/directory" ]
then
echo "Directory /path/to/directory is exists."
fi
Or syntax Bash script check if directory not exists
if [ ! -d "/path/to/directory" ]
then
echo "Directory /path/to/directory is NOT exists."
exit 99
fi
Check if the file is exists
if [ -f "/path/to/file" ]
then
echo "File /path/to/file is exists."
fi
Example Bash script checks if the directory exists or file exists.
#!/bin/bash
_FILE="/home/huupv/devopsroles/folder/file.txt"
_LINK_DIR="/home/huupv/devopsroles/link_folder"
# Check Directory and File is exists
if [ -f "${_FILE}" ]
then
echo "File ${_FILE} is exists."
_DIR=$(dirname ${_FILE})
if [ -d "${_DIR}" ]
then
echo "Directory /path/to/directory is exists."
fi
fi
# Check Symbolic link
ls -la /home/huupv/devopsroles/ | grep "\->"
# Check Symbolic link is exists
if [ -L "${_LINK_DIR}" ]
then
echo "Directory ${_LINK_DIR} is a symlink."
fi
# Check Directory is NOT exists
if [ ! -d "/home/huupv/devopsroles/folder2" ]
then
echo "Directory /home/huupv/devopsroles/folder2 is NOT exists."
exit 99; # ERROR code 99
fi
The screen output terminal:
Summary check File/Folder exists or NOT exists
-L “FILE“: FILE/FOLDER exists and is a symbolic link
-d “Directory“: FILE exists and is a directory
-f “FILE” : FILE exists
Conclusion
Through the article, you can use the Bash script check if directory exists as above. I hope will this your helpful. For more details refer to the Bash script.
How to use bash script put Multiple Line Comment. Would you like to use a bash script comment in Linux?
When writing Bash scripts, it’s crucial to include comments to enhance code readability, provide explanations, and document important details. Comments are lines of text within a script that are ignored by the Bash interpreter when executing the code.
In this tutorial, we will explore the two types of comments available in Bash scripting and learn how to effectively use them.
Bash script comment
Single line comment
Single-line comments are the simplest form of comments in Bash scripting. They begin with the # (hash) symbol and continue until the end of the line.
# Comment line 1
# Comment line 2
HERE DOCUMENT COMMENT
A here document is a construct in Bash that allows you to pass multiple lines of input to a command or store them in a variable. It is not used as a form of comment.
<<COMMENT
Comment 1
Comment 2
Comment N
COMMENT
Multiple line comment
While Bash does not have a built-in syntax for multi-line comments like some other programming languages, you can achieve a similar effect by using multiple single-line comments consecutively.
: '
This is a
multiple line
comment
'
Best Practices for Commenting
To make your Bash scripts more understandable and maintainable, consider the following best practices for commenting:
Use comments to describe the purpose and functionality of your code.
Add comments above sections of code to provide an overview of what the code does.
Include comments within complex code blocks to explain intricate logic or algorithms.
Document any assumptions, constraints, or prerequisites required for the script to function properly.
Comment your code as if someone else will be reading and maintaining it.
Avoid excessive commenting; focus on providing meaningful and concise explanations.
#!/bin/bash
_foo="Wellcome to Devopsroles.com"
# Single line 1
# Single line 2
<<COMMENT
Comment 1
Comment 2
Comment N
HERE DOCUMENT comment
COMMENT
: '
This is a
multi line
comment
'
echo ${_foo}
The screen output terminal:
Conclusion
In Bash scripting, comments play a vital role in enhancing code readability and maintaining script documentation.
Single-line comments starting with # and multi-line comments created by combining consecutive single-line comments are powerful tools for explaining code, providing instructions, and temporarily disabling sections.
I hope will this your helpful. For more details refer to Bash script.
You can use the date command to display or set the current date and time. In this tutorial, I will write small program use Bash script read time. Bash script the essential for DevOps Roles.
The syntax of date command
date +"%FORMAT"
Show current time
[huupv@huupv devopsroles]$ date +"%T"
The screen output terminal:
Set variable _now for the current time
_now=$(date +"%T")
echo "Current time : ${_now}"
The screen output terminal:
Create bash script bash_read_time.sh file
#!/bin/bash
# Display text at given row and column
function show(){
local x=$1
local y=$2
local txt="$3"
tput cup $x $y
echo "$txt"
}
while true
do
clear
# Get the system time 12 hour clock time
now="$(date +"%r")"
# Show main
show 11 11 "MAIN MENU for $HOSTNAME - $now"
show 11 11 "1. System info "
show 12 11 "2. OS version "
show 13 11 "3. Exit "
tput cup 16 11; read -t 2 -p "Choice [1-3] ? " _select
# do something
case ${_select} in
1) read -t 2 -p "Showing system info, wait..." ;;
2) read -t 2 -p "Showing apache info, wait..." ;;
3) echo "Bye."; exit 0;;
esac
done