Mcollective: How to run mco with a non root user

I thought this little trick could be interesting for people out there using mcollective.

By default mco  (mcollective command line application) will look for the configuration file at /etc/mcollective/client.cfg, and as a non root user you will not have access to it:

pere@sw-veremis:~$ cat /etc/mcollective/client.cfg
cat: /etc/mcollective/client.cfg: Permission denied

To overcome this, mco  offers the option to load a custom configuration file with the -c modifier:

-c, --config FILE                Load configuratuion from file rather than default

Now we can add a function to our non-root user .bashrc configuration file like this:

#functions
mco() { /usr/bin/mco "$@" -c ~/.mcollective/client.cfg; } 

That will ensure that every time we use the mco command with our non-root user, our new custom configuration file will be used.

The custom configuration file will contain our user specific details and related files and will vary depending on how you are using mcollective, the security plugins deployed, the middleware used ... Here do you have a sample: 

pere@sw-veremis:~$ cat ~/.mcollective/client.cfg
# main config
libdir = /usr/share/mcollective/plugins
logfile = /home/pere/.mcollective/log
loglevel = debug
# ActiveMQ connector settings
connector = activemq
direct_addressing = 1
plugin.activemq.pool.size = 1
plugin.activemq.pool.1.host = stomp.mystompserver.com
plugin.activemq.pool.1.port = 61614
plugin.activemq.pool.1.user = myusername
plugin.activemq.pool.1.password = mypassword
plugin.activemq.pool.1.ssl = 1
plugin.activemq.pool.1.ssl.ca = /home/pere/.mcollective/ssl/cacert.pem
plugin.activemq.pool.1.ssl.cert = /home/pere/.mcollective/ssl/perehospital-public.pem
plugin.activemq.pool.1.ssl.key = /home/pere/.mcollective/ssl/perehospital-private.pem
plugin.activemq.pool.1.ssl.fallback = 0
# SSL security plugin settings:
securityprovider = ssl
plugin.ssl_server_public = /home/pere/.mcollective/ssl/server-public.pem
plugin.ssl_client_private = /home/pere/.mcollective/ssl/perehospital-private.pem
plugin.ssl_client_public = /home/pere/.mcollective/ssl/perehospital-public.pem
# Interface settings:
default_discovery_method = mc
direct_addressing_threshold = 10
ttl = 60
color = 1
rpclimitmethod = first
# No additional subcollectives:
collectives = mcollective
main_collective = mcollective

Surely you can define other functions in .bashrc to help you achieve other stuff that you want to get from mcollective

#functions
mco() { /usr/bin/mco "$@" -c ~/.mcollective/client.cfg; }
mcocustomer() { mco find -F cloudways_customer="$@"; } 
Here we have defined the mcocustomer function that will get a list of servers from a given customer on our mcollective network.

From here on, use your imagination!