Ask Your Question

Unless statement not behaving as expected.

asked 2014-04-30 18:30:51 -0500

jdehnert gravatar image

updated 2014-05-01 09:22:12 -0500

spuder gravatar image

I am trying to use an unless statement to restrict an exec from running if it does not need to. What I am trying to do is to check a file for a specific variable. If that variable is a specific number, then my config file is correct, and the exec has no need to run. However, I cannot seem to get this to operate properly. Here is my statement and associated variables...

$backup_port = 1167
$backup_conf = '/usr/sbin/r1soft/conf/agent_config'
$backup_port_conf = "cat ${backup_conf} | grep ^LocalPort | awk -F= '{print $2}'"

  exec { "serverbackup-setup --agent-port ${backup_port}":
    unless  => "${backup_port_conf} == ${backup_port}",

Now if I manually run the statement that I have in the $backup_port_conf variable, it returns 1167. I have tried this with the whole "cat....'{print $2}'" statement in the unless statement, but that didn't work any better. From what I have been able to read about variables,

"The variable will contain the value that the statement resolves to, rather than a reference to the statement."

I am assuming that ${backup_port_conf} should return the value that the statement resolves to, but notify spits out the whole "cat....'{print $2}'" line instead of the 1167 that I am hoping to see.

Normally I would just work around this since all my existing hosts are set, but I would prefer to keep this check if I can for future hosts. The big problem is that when the exec process runs, it restarted the client and makes any running backups fail.

What am I missing?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2014-04-30 19:34:42 -0500

ramindk gravatar image

The problem is that unless is assumed to be a command that is run on the agent that returns a 0 as an exit code. There isn't any further evaluation.

In order to use unless as you've written it you might do

exec { "serverbackup-setup --agent-port ${backup_port}":
  unless    => "bash -c 'cat ${backup_conf} | grep ^LocalPort | awk -F= \{print $2}\' | grep ${backup_port} \'",

I think that should work, but you might need to play around with the quoting. Note that you'll need to either use provider => shell, which spawns a /bin/sh or spawn your own shell if you want shell built-ins like pipes to work.

edit flag offensive delete link more

answered 2014-05-01 12:31:45 -0500

cristaldo gravatar image

I think it's better something more simple!, like this!

$backupport = "1167" $backupconf = '/usr/sbin/r1soft/conf/agent_config'

exec { "serverbackup-setup --agent-port $backupport}": path => '/usr/bin:/usr/sbin:/bin', unless => "egrep 'LocalPort 1167' ${backupconf}", }

It works like a charme!

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2014-04-30 18:30:51 -0500

Seen: 247 times

Last updated: May 01 '14