Yum Variables – How to View Variables Like $releasever, $basearch & $YUM0

dnfrepositoryscientific-linuxyum

I am setting up a yum repository, and need to debug some of the URLs in the yum.conf file. I need to know why is Scientific Linux trying to grab this URL, when I was expecting it to grab another URL:

# yum install package 
http://192.168.1.100/pub/scientific/6.1/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 22 - "The requested URL returned error: 404"
Trying other mirror.
Error: Cannot retrieve repository metadata (repomd.xml) for repository: sl. Please verify its path and try again

The yum.conf(5) manpage gives some information about these variables:

Variables

There are a number of variables you can use to ease maintenance of
yum's configuration files. They are available in the values of several
options including name, baseurl and commands.

$releasever This will be replaced with the value of the version of the
package listed in distroverpkg. This defaults to the version of
'redhat-release' package.

$arch This will be replaced with your
architecture as listed by os.uname()[4] in Python.

$basearch This will be replaced with your base architecture in yum.
For example, if your $arch is i686 your $basearch will be i386.

$YUM0-$YUM9 These will be replaced with the value of the shell
environment variable of the same name. If the shell environment
variable does not exist then the configuration file variable will not
be replaced.

Is there a way to view these variables by using the yum commandline utility? I would prefer to not hunt down the version of the 'redhat-release' package, or manually get the value of os.uname()[4] in Python.

Best Answer

When this answer was written in 2011, json wasn't installed for python by default for all the versions of RHEL/CentOS at that time so I used pprint to print the stuff nicely.

It is now 2020 and all current versions of RHEL/CentOS have json by default for python. The answer has been updated to use json and updated to include RHEL/CentOS 8 by modifying @sysadmiral's answer for Fedora.

RHEL/CentOS 8:

/usr/libexec/platform-python -c 'import dnf, json; db = dnf.dnf.Base(); print(json.dumps(db.conf.substitutions, indent=2))'

RHEL/CentOS 6 and 7

python -c 'import yum, json; yb = yum.YumBase(); print json.dumps(yb.conf.yumvar, indent=2)'

RHEL/CentOS 4 and 5

# if you install python-simplejson
python -c 'import yum, simplejson as json; yb = yum.YumBase(); print json.dumps(yb.conf.yumvar, indent=2)'

# otherwise
python -c 'import yum, pprint; yb = yum.YumBase(); pprint.pprint(yb.conf.yumvar, width=1)'

Example output:

# CentOS 8:
# ---
[root@0928d3917e32 /]# /usr/libexec/platform-python -c 'import dnf, json; db = dnf.dnf.Base(); print(json.dumps(db.conf.substitutions, indent=2))'
Failed to set locale, defaulting to C
{
  "arch": "x86_64",
  "basearch": "x86_64",
  "releasever": "8"
}
[root@0928d3917e32 /]# 


# CentOS 7:
# ---
[root@c41adb7f40c2 /]# python -c 'import yum, json; yb = yum.YumBase(); print json.dumps(yb.conf.yumvar, indent=2)'
Loaded plugins: fastestmirror, ovl
{
  "uuid": "cb5f5f60-d45c-4270-8c36-a4e64d2dece4", 
  "contentdir": "centos", 
  "basearch": "x86_64", 
  "infra": "container", 
  "releasever": "7", 
  "arch": "ia32e"
}
[root@c41adb7f40c2 /]# 

# CentOS 6:
# ---
[root@bfd11c9a0880 /]# python -c 'import yum, json; yb = yum.YumBase(); print json.dumps(yb.conf.yumvar, indent=2)'
Loaded plugins: fastestmirror, ovl
{
  "releasever": "6", 
  "basearch": "x86_64", 
  "arch": "ia32e", 
  "uuid": "3e0273f1-f5b6-481b-987c-b5f21dde4310", 
  "infra": "container"
}
[root@bfd11c9a0880 /]# 

Original answer below:

If you install yum-utils, that will give you yum-debug-dump which will write those variables and more debugging info to a file. There is no option to write to stdout, it will always write to some file which really isn't that helpful.

This is obviously not a great solution so here's a python one-liner you can copy and paste which will print those variables to stdout.

python -c 'import yum, pprint; yb = yum.YumBase(); pprint.pprint(yb.conf.yumvar, width=1)'

This works on CentOS 5 and 6, but not 4. yum is written in python, so the yum python module is already on your server, no need to install anything exra.

Here's what it looks like on CentOS 5:

[root@somebox]# python -c 'import yum, pprint; yb = yum.YumBase(); pprint.pprint(yb.conf.yumvar, width=1)'
{'arch': 'ia32e',
 'basearch': 'x86_64',
 'releasever': '5',
 'yum0': '200',
 'yum5': 'foo'}
[root@somebox]# 
Related Question