How to use Variables and Global Variables on Asterisk Dialplan Context on Oracle Linux 9.3

To Use Asterisk Dialplan Variables And Global Variables On Oracle Linux 9.3

Introduction:

Asterisk is an open-source telephony platform that is classified into two types of variables: Dialplan Variables and Global Variables. These variables are used to store and manipulate data within the Asterisk dialplan and can be accessed and modified during the call processing.

Dialplan Variables: Dialplan variables are defined and used within a specific context or extension in the Asterisk dialplan. They are used to store temporary data that is relevant to a particular call or call leg. Dialplan variables are scoped to the context in which they are defined and are typically used to pass information between different parts of the dialplan.

Global Variables: Global variables have a global scope and are accessible from anywhere within the Asterisk dialplan. They are typically used to store configuration settings, constants, or values that must be shared across multiple contexts or extensions. Global variables are defined in the Asterisk configuration files, such as extensions.conf or other included configuration files.

Procedure:

Step 1: Check the OS version by using the below command

[root@linuxhelp ~]# cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="9.3"
ID="ol"
ID_LIKE="fedora"
VARIANT="Server"
VARIANT_ID="server"
VERSION_ID="9.3"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Oracle Linux Server 9.3"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:oracle:linux:9:3:server"
HOME_URL="https://linux.oracle.com/"
BUG_REPORT_URL="https://github.com/oracle/oracle-linux"
ORACLE_BUGZILLA_PRODUCT="Oracle Linux 9"
ORACLE_BUGZILLA_PRODUCT_VERSION=9.3
ORACLE_SUPPORT_PRODUCT="Oracle Linux"
ORACLE_SUPPORT_PRODUCT_VERSION=9.3

Step 2: Check the status of the Asterisk by using the below command

[root@linuxhelp ~]# systemctl status asterisk.service
● asterisk.service - LSB: Asterisk PBX
     Loaded: loaded (/etc/rc.d/init.d/asterisk; generated)
     Active: active (running) since Wed 2024-04-17 12:52:51 IST; 53min ago
       Docs: man:systemd-sysv-generator(8)
    Process: 2867 ExecStart=/etc/rc.d/init.d/asterisk start (code=exited, status=0/SUCCESS)
   Main PID: 2896 (asterisk)
      Tasks: 45 (limit: 21649)
     Memory: 46.0M
        CPU: 24.914s
     CGroup: /system.slice/asterisk.service
             ├─2894 /bin/sh /usr/sbin/safe_asterisk
             └─2896 /usr/sbin/asterisk -f -vvvg -c

Apr 17 12:52:51 linuxhelp systemd[1]: Starting LSB: Asterisk PBX...
Apr 17 12:52:51 linuxhelp asterisk[2867]: Starting asterisk:
Apr 17 12:52:51 linuxhelp systemd[1]: asterisk.service: Can't open PID file /run/asterisk/asterisk.pid (yet?) after start: Operation>
Apr 17 12:52:51 linuxhelp systemd[1]: asterisk.service: Supervising process 2896 which is not our child. We'll most likely not notic>
Apr 17 12:52:51 linuxhelp systemd[1]: Started LSB: Asterisk PBX.

Step 3: Move to the Asterisk directory by using the below command

[root@linuxhelp ~]# cd /etc/asterisk/

Step 4: Show the endpoints in the pjsip.conf file by using the below command

[root@linuxhelp asterisk]# vim pjsip.conf
Add those following lines
[tcp_transport]
type=transport ; Must be of type 'transport'
protocol=tcp
bind=0.0.0.0
tos=cs3
cos=3
allow_reload=false
[udp_transport]
type=transport ; Must be of type 'transport'
protocol=udp
bind=0.0.0.0
tos=cs3
cos=3
allow_reload=false

[167]
auth_type=userpass
type=auth
username=167
password=123456

[167]
type=aor
qualify_frequency=60
max_contacts=1
remove_existing=yes
qualify_timeout=3.0
authenticate_qualify=no
[167]
context=internal
language=en
deny=0.0.0.0/0.0.0.0
trust_id_inbound=yes
send_rpid=no
rtcp_mux=no
call_group=
pickup_group=
disallow=all
allow=ulaw,alaw,g722,gsm,vp9,vp8,h264,opus
;dtls_auto_generate_cert=yes
;webrtc=yes
permit=0.0.0.0/0.0.0.0
ice_support=no
use_avpf=no
dtls_cert_file=
dtls_private_key=
dtls_ca_file=
dtls_setup=actpass
dtls_verify=no
media_encryption=no
message_context=
subscribe_context=
allow_subscribe=yes
rtp_symmetric=yes
force_rport=yes
rewrite_contact=yes
direct_media=no
media_use_received_transport=no
auth=167
aors=167
type=endpoint
mailboxes=63121@63
callerid="User" <167>
[168]
auth_type=userpass
type=auth
username=168
password=123456
[168]
type=aor
qualify_frequency=60
max_contacts=1
remove_existing=yes
qualify_timeout=3.0
authenticate_qualify=no
[168]
context=internal
language=en
deny=0.0.0.0/0.0.0.0
trust_id_inbound=yes
send_rpid=no
rtcp_mux=no
call_group=
pickup_group=
disallow=all
allow=ulaw,alaw,g722,gsm,vp9,vp8,h264,opus
;dtls_auto_generate_cert=yes
;webrtc=yes
permit=0.0.0.0/0.0.0.0
ice_support=no
use_avpf=no
dtls_cert_file=
dtls_private_key=
dtls_ca_file=
dtls_setup=actpass
dtls_verify=no
media_encryption=no
message_context=
subscribe_context=
allow_subscribe=yes
rtp_symmetric=yes
force_rport=yes
rewrite_contact=yes
direct_media=no
media_use_received_transport=no
auth=168
aors=168
type=endpoint
mailboxes=63121@63
callerid="User" <168>

Step 5: Open extensions.conf file to edit the dialplan using variables by using the below command

[root@linuxhelp asterisk]# vim extensions.conf
Add those following Lines
[internal]
exten => 167,1,NoOp(Call Started)
exten => 167,2,Dial(Pjsip/167)
exten => 167,3Playback(Sorry_didnt_get)
exten => 167,4,Hangup()

exten => 168,1,Dial(Pjsip/168)
[internal]
;exten => 167,1,Dial(PJSIP/167)
;exten => 168,1,Dial(PJSIP/168)

Step 6: Login to the Asterisk CLI mode by using the below command

[root@linuxhelp asterisk]# asterisk -rvvvvvvvvv
Asterisk 20.7.0, Copyright (C) 1999 - 2022, Sangoma Technologies Corporation and others.
Created by Mark Spencer <markster@digium.com>
Asterisk comes with ABSOLUTELY NO WARRANTY; type 'core show warranty' for details.
This is free software, with components licensed under the GNU General Public
License version 2 and other licenses; you are welcome to redistribute it under
certain conditions. Type 'core show license' for details.
=========================================================================
Connected to Asterisk 20.7.0 currently running on linuxhelp (pid = 2896)
linuxhelp*CLI>

Step 7: Reload the dialplan by using the below command

linuxhelp*CLI> dialplan reload
Dialplan reloaded.
       > Time to scan old dialplan and merge leftovers back into the new: 0.000016 sec
       > Time to restore hints and swap in new dialplan: 0.000021 sec
       > Time to delete the old dialplan: 0.000010 sec
       > Total time merge_contexts_delete: 0.000047 sec
       > pbx_config successfully loaded 1 contexts (enable debug for details).

Output:
Executing [168@internal:1] Dial("PJSIP/167-00000021", "Pjsip/168") in new stack
    -- Called Pjsip/168
    -- PJSIP/168-00000022 is ringing
  == Everyone is busy/congested at this time (1:1/0/0)
    -- Auto fallthrough, channel 'PJSIP/167-00000021' status is 'BUSY'
    -- Executing [167@internal:1] NoOp("PJSIP/168-00000023", "Call Started") in new stack
    -- Executing [167@internal:2] Dial("PJSIP/168-00000023", "Pjsip/167") in new stack
    -- Called Pjsip/167
    -- PJSIP/167-00000024 is ringing
  == Everyone is busy/congested at this time (1:1/0/0)
[Apr 17 13:48:55] WARNING[4414][C-00000016]: pbx.c:2928 pbx_extension_helper: No application '' for extension (internal, 167, 3)
  == Spawn extension (internal, 167, 3) exited non-zero on 'PJSIP/168-00000023'

Step 8: Exit from the Asterisk CLI by using the below command

linuxhelp*CLI> exit
Asterisk cleanly ending (0).
Executing last minute cleanups
[root@linuxhelp asterisk]#

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps how to use Variables and Global Variables on Asterisk Dialplan Context. Your feedback is much welcome.

FAQ
Q
What is the advantage of using Global Variables in Asterisk?
A
Global Variables provide a way to centralize and manage shared data and configuration settings across your Asterisk configuration. They can be accessed from any part of the dialplan, making them useful for storing constants, configuration parameters, or values that need to be accessed globally.
Q
How are Global Variables defined in Asterisk, and where are they typically configured?
A
Global Variables are defined in the Asterisk configuration files, often within a [globals] section in the extensions.conf file or other included configuration files. Here's an example of defining a Global Variable:
plaintextCopy code
[globals] my_global_variable = 100
Q
Can Dialplan Variables be used to share data between different contexts in Asterisk?
A
Dialplan Variables are primarily scoped to the context or extension in which they are defined. However, you can use channel variables or Global Variables to share data between different contexts or extensions.
Q
How do I set a value for a Dialplan Variable in Asterisk?
A
You can set a Dialplan Variable using the Set application within a specific extension in your Asterisk dialplan. For example:
plaintextCopy code
exten => 123,1,Set(my_variable=42)
This sets a Dialplan Variable named my_variable with a value of 42 within extension 123.
Q
What is the main difference between Dialplan Variables and Global Variables in Asterisk?
A
The main difference is their scope. Dialplan Variables are scoped to a specific context or extension within the dialplan and are used for temporary data relevant to a single call or call log. Global Variables, on the other hand, have a global scope and can be accessed from anywhere within the Asterisk configuration, making them suitable for storing shared data and configuration settings.