relation-get
reads the settings of the local unit, or of any remote unit,
in a given relation (set with -r
, defaulting to the current relation
identifier, as in relation-set
). The first argument specifies the settings
key, and the second the remote unit, which may be omitted if a default is
available (that is, when running a relation hook other than -broken).
If the first argument is omitted, a dictionary of all current keys and values
will be printed; all values are always plain strings without any
interpretation. If you need to specify a remote unit but want to see all
settings, use -
for the first argument.
The environment variable
JUJU_REMOTE_UNIT
stores the default remote unit:
You should never depend upon the presence of any given key in relation-get
output. Processing that depends on specific values (other than private-address
)
should be restricted to -changed
hooks for the relevant unit, and the absence
of a remote unit's value should never be treated as an
error in the local unit.
In practice, it is common and encouraged for -relation-changed hooks to exit
early, without error, after inspecting relation-get
output and determining it
to be inadequate; and for all other hooks to be
resilient in the face of missing keys, such that -relation-changed hooks will be
sufficient to complete all configuration that depends on remote unit settings.
Settings for remote units already known to have departed remain accessible for the lifetime of the relation.
# Getting the settings of the default unit in the default relation is done with:
relation-get
username: jim
password: "12345"
# To get a specific setting from the default remote unit in the default relation
relation-get username
jim
# To get all settings from a particular remote unit in a particular relation you
relation-get -r database:7 - mongodb/5
username: bob
password: 2db673e81ffa264c
from charmhelpers.core.hookenv import relation_get
# Since we define the relation id on every call to relation_get, both bash
# examples look like the line below
relation_get(rel_id)
# To get a specific setting from the remote unit in the specified relation
relation_get(rel_id, 'username')