Ask Your Question
0

exec ignores onlyif and unless

asked 2016-09-27 03:09:50 -0500

aspinos gravatar image

updated 2016-09-29 08:28:10 -0500

Good day all,

I have below exec resource definition in my manifest:

exec {'enable_debian_php':
                path => '/usr/bin',
                command => 'sudo a2enmod php7.0 ; sudo a2dismod mpm_event ; sudo a2enmod mpm_prefork ; /bin/systemctl restart apache2.service',
                onlyif => '/bin/true ; phpen=$(apachectl -M | grep "php7_module\|mpm_prefork_module" | wc -l) ; phpdis=$(apachectl -M | grep "mpm_event" | wc -l) ; [ "$phpen" -ne 2 -o "$phpdis" -ne 0 ]',
                require => Service['Web service'],
        }

Onlyif condition correctly evaluates in target server as "1" but exec still goes on and runs the command.

/bin/true ; phpen=$(apachectl -M | grep "php7_module\|mpm_prefork_module" | wc -l) ; phpdis=$(apachectl -M | grep "mpm_event" | wc -l) ; [ "$phpen" -ne 2 -o "$phpdis" -ne 0 ] ; echo {$phpen,$phpdis,$?}
2 0 1

Edit!

I changed the exec resource as below:

exec {'enable_debian_php':
                path => '/usr/bin',
                command => 'sudo a2enmod php7.0 ; sudo a2dismod mpm_event ; sudo a2enmod mpm_prefork ; /bin/systemctl restart apache2.service',
               onlyif => '/bin/bash /root/check_apache_modules.sh;',
              require => File['check_apache_modules'],
         }

The bash script is the below:

#! /bin/bash
phpen=$(apachectl -M | grep "php7_module\|mpm_prefork_module" | wc -l);
phpdis=$(apachectl -M | grep "mpm_event" | wc -l);

if [ \( "$phpen" -ne "2"  -o "$phpdis" -ne "0" \) ]
then exit 0
else exit 1
fi

When the script is run on the server and then I look at the exit code I get:

root@ubuntu:~# bash /root/check_apache_modules.sh ; echo $?
1

But the exec again executes the command.

edit retag flag offensive close merge delete

Comments

1

/bin/test !

puser gravatar imagepuser ( 2016-09-28 13:37:34 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2016-09-27 12:24:26 -0500

lupin gravatar image

That's the expected outcome.

onlyif

If this parameter is set, then this exec will only run if the command has an exit code of 0.

unless

If this parameter is set, then this exec will run unless the command has an exit code of 0.

edit flag offensive delete link more
0

answered 2016-10-01 07:04:35 -0500

Stefan gravatar image

you are specifying the path parameter which will set PATH environment variable before running the command. The same goes for your onlyif command. So if apachectl, grep, or wc (which are executed in your onlyif script) are not in /usr/bin your command will most likely fail.

Also

onlyif => '/bin/bash /root/check_apache_modules.sh;',

seem to have a trailing ; which should not be there.

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

Stats

Asked: 2016-09-27 03:09:50 -0500

Seen: 883 times

Last updated: Oct 01 '16