How to pass variables to Ansible from command

Just like any other scripting language , you can also use variables in Ansible.
You can use variables so your playbooks become more dymanic and re-usable in multiple scenarios.

To demonstrate, I will use copy module in ansible, however you can use variable in any where in your playbook.
so when you use copy module you normally define source and destination as shown below.

dest: /home/justgeek/example.txt
src: /home/justgeek/example.txt

However, eveytime the file name cannot be example.txt so there is a way to pass the file name when you run the ansible-playbook command. So you need to define the variable as shown below.

dest: /home/justgeek/{{ filename }}
src: /home/justgeek/{{ filename }}

So your complete playbook would look like this.

-
  gather_facts: no
  hosts:
       - webserver

  tasks:
   - name: Copy file
     copy:
         dest: /home/mumrethwala/{{ filename }}
         src: /home/mumrethwala/{{ filename }}

So when running playbook, you ansible command would look like below.

ansible-playbook -i Inventory.yml test.yml -k -K --extra-vars name=update.sh

You can use these variable and define dynamic hosts as well.

Defining Variables inside playbooks.

You can also define variables inside playbook as well.

---
- hosts: all
  vars:
      message: "Hello World"
  tasks:
    - name: Testing Ansible variable
      debug:
         msg: "{{ message }}"

So when you run playbook it will print as below

TASK [Testing Ansible variable] *******************************************************************************************
ok: [server01] => {
    "msg": "Hello World"
}

You can also check other ansible Articles to learn more.

What are the Linux kill command

So we have been covering diffrent Linux command as previosuly we saw echo command in Linux now today we will see what are the Linux kill command and Enlist all the Linux kill commands with its function

The Linux Kill commands are:

  1. Killall
  2. Pkill
  3. xkill
  • Killall: Killall command is used to kill all the processes with a particular name.
  • Pkill: This command is a lot like killall, except it kills processes with partial names.
  • Xkill: xkill allows users to kill command by clicking on the window

Difference between kill and kill -9

The normal kill command sends a signal to a process. This can terminate a processes. However if some program is hung or not responding you may have to kill that program using -9 option which will kill a program no matter what.
kill -9 you may have to think twice while using kill -9 as it will bypass any normal shutdown process and any unsaved data will be lost.

Killall Command in Linux

Unlike kill command killall command can terminate all processes by it’s name. For example, if you want to kill all processes used by firefox so instead of finding all process ID and killing them you can just do killall firefox

  • -e. Find an exact match for the process name.
  • -I. Ignore case when trying to find the process name.
  • -i. Ask for additional confirmation when killing the process.
  • -u. Only kill processes owned by a specific user.
  • -v. Report back on whether the process has been successfully killed.

pkill Command

You term pkill command as search and kill. It will kill processes based on name but when other paramerters match.

pkill options pattern

pkill options include:

  • -n. Only kill the newest of the processes that are discovered.
  • -o. Only kill the oldest of the processes that are discovered.
  • -u. Only kill the processes that are owned by the selected user.
  • -x. Only kill the processes that match the pattern exactly.
  • -signal. Send a specific signal to the process, rather than SIGTERM.

Seq Command in Linux

Seq Command in Linux: Linux seq command generates a list of numbers – you can use this in scripts and many other functions. Let’s see the examples.

Basic usage of command
Just typing seq 5 will create 5 numbers

[root@server ~]# seq 10
1
2
3
4
5
[root@server ~]#

If you type two numbers, first number will be start and other one will be end number.

[root@server ~]# seq 21 25
21
22
23
24
25
[root@server ~]#

You can add a third number in between which will work as a step.
for example, if you type seq 5 5 50 it will generate a number from 5 to 50 at the interval of 5 – see the example below

[root@server ~]# seq 5 5 50
5
10
15
20
25
30
35
40
45
50
[root@server ~]#

You can generate numbers in descending order as well by adding the middle number in -ve

[root@server ~]# seq 10 -1 1
10
9
8
7
6
5
4
3
2
1

Practical Usage of seq command
First, let’s see how to create files using the seq command. You will need to use format strings functions i.e. -f

[root@server seq]# touch $(seq -f "file-%g.txt" 1 10)

See the output of the command above.

You can use seq command in your scripting for example While Loops or For Loops.
We will see an example for loop.

[root@server seq]# for i in $(seq 1 10); do echo "Hello $i"; done
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10

Here is Man page you can use it to learn something more about it which may be covered in this page.

Case in shell scripting.

We use IF LOOP all time, but when it comes to multiple conditions IF Loop gets complicated as you have to write multiple times. CASE saves going through whole if…then…else statements.

#!/bin/bash
echo -n "Enter the Country name "
read Country

echo -n "Captial of $Country is "

case $Country in
        India )
       echo -n "Delhi"
       echo ""
                ;;

    Argentina )
     echo -n "Buenos Aires"
      echo ""
                ;;

    Brazil )
     echo -n "Brasília"
      echo ""
                ;;

    * )
     echo -n "unknown"
                ;;

esac

So, if you enter the country name which is mentioned it will give desired output and if it’s not present It will show unknown as mentioned in script.

[root@server ~]# ./case.sh
Enter the Country name India
Captial of India is Delhi

[root@server ~]# ./case.sh
Enter the Country name Argentina
Captial of Argentina is Buenos Aires

[root@server ~]# ./case.sh
Enter the Country name USA
Captial of USA is unknown
[root@server ~]#

Ansible Playbook to start services

Ansible Playbook to start services

We have already seen how to run Ansible Adhoc Command, now let’s focus on a playbook to start any service on your servers, In the example below I have started crond service but you can use it to suit your needs.

#Ansible Playbook to start service
-
  name: Start Services
  hosts: webservers

  become: true
  tasks:
    - name: Start Services
      service:
       name: crond
       state: started

If you have multiple host groups then replace hosts: webservers with below

 hosts: 
   - webserversUSA
   - webserversASIA
   - webserversEurope

You may have a situation where you want to start multiple services, In that case, you can use ansible loop the example playbook is as below.

#Ansible Playbook to start service
-
  name: Start Services
  hosts: webservers

  become: true
  tasks:
    - name: Start Services
      service:
        name: '{{ item }}'
        state: started
      loop:
        - httpd
        - crond
        - and-so-on