Disabling a trunk by setting invalid parameter?

I have an Asterisk system with 2 trunks (as shown below). I need to be able to disable a trunk at runtime. I may not change the dialplan but I can change sip.conf and reload. (And I can’t use realtime)

Any attempt to dial in the dialplan uses trunk A and trunk B in that order. Normally calls will route through trunk A, but if I disable A I want calls to go to trunk B.

Is there a creative way to effectively disable a trunk at runtime given these parameters? I don’t think there is an “enabled” key-value pair for sip.conf stanzas. If I change the host key value to 0.0.0.0 and reload will that effectively cause the dialplan to use trunk B? (Or set port=9999, etc)

[trunk_A]
context=from-trunk-sip-trunk_A

[trunk_A_in]
type=peer
qualify=yes
host=1.2.3.4
context=from-trunk

[trunk_B]
context=from-trunk-sip-trunk_B

[trunk_B_in]
type=peer
qualify=yes
host=1.2.3.4
context=from-trunk

A trunk normally handles both inbound and outbound calling. Since you show both trunks are using the same host, it’s not clear witch direction you want to address.

The inbound behavior of Asterisk when two (2) trunks use the same host and port is a little unpredictable. I forget which, think it’s the first match.

If I write it like an Apache config, the below shows is how Asterisk sees inbound calling. Changing the host and/or port will disable the inbound side of the trunk. This approach could be used to switch the dialplan context for inbound calling.

# Showing inbound calling using the Apache config format
# It's an example and does NOT work in Asterisk
<SipTrunk 1.2.3.4:5060>
    name=trunk_A
    context=from-trunk_A-dialplan
</SipTrunk>
<SipTrunk 1.2.3.4:5060>
    name=trunk_B
    context=from-trunk_B-dialplan
</SipTrunk>
<SipTrunk *:5060>
    name=default
    context=default-dialplan
</SipTrunk>    

For outbound calling, the following Dial() statements are basically identical.

# extensions.conf
 same => n,Dial(SIP/1.2.3.4:9999/18004052200)
 same => n,Dial(SIP/my_trunk/18004052200)

# sip.conf
[my_trunk]
 host=1.2.3.4
 port=9999

What you may be asking, is how to make outbound calls go to trunk B when trunk A does not work. Useful when you have a different host to send an outbound call through.

# extensions.conf
[outbound-trunk-a]
...
 same => n,Dial(SIP/trunk-a/${EXTEN})
 same => n,GotoIf($["${DIALSTATUS}"="CHANUNAVAIL"]?outbound-trunk-b,${EXTEN},1)

[outbound-trunk-b]
...
 same => n,Dial(SIP/trunk-b/${EXTEN})

# sip.conf
[trunk-a]
 host=1.2.3.4
[trunk-b]
 host=5.6.7.8

You do not want to use a different context for the inbound and outbound calling in sip.conf. As the Apache example above shows, you end up creating multiple matches with the following. Which could break the inbound calling, if context (only used by inbound calling) is not defined, as shown below.

[trunk-a-in]
 host=1.2.3.4
 context=inbound
[trunk-a-out]
 host=1.2.3.4
 // no context here = default dialplan
[trunk-b-in]
 host=5.6.7.8
 context=inbound
[trunk-b-out]
 host=5.6.7.8
 // no context here = default dialplan

How about using the internal asterisk database?

https://wiki.asterisk.org/wiki/display/AST/Asterisk+16+Function_DB

You could define which trunk to use by setting a key/value pair in the database and checking it as part of your outbound dial.

No changes would need to be made to your extensions.conf, and you could even use something like AMI to toggle the database entry to control calls.