Samselvaprabu
Samselvaprabu

Reputation: 18197

How to run next ansible playbook if previous playbook has failed

We are testing our setup with ansible. The test results are captured in a file. Once our ansible playbook execution is done ,we used to see the test summary file and know how many of them are passed. Now that process is automated via jenkins. In Jenkins ,at shell prompt below code is being executed.

cd ${repodir}
ansible-playbook -i rhelhost plugin.yml
result=$?
ansible-playbook -i rhelhost testsummary.yml
if [ $result -ne 0 ]; then  
  echo "ERROR: Execution is failed"
  exit 1
fi

But if only the plugin.yml execution is completed successfully then testsummary.yml is being executed. if failure occurs even result=$? line not getting executed.

How to execute those lines in case of failure in plugin.yml? Even if something failed our test summary will have results of what happened till then.

Upvotes: 0

Views: 407

Answers (1)

Prakash Krishna
Prakash Krishna

Reputation: 1267

Try below

cd ${repodir}
ansible-playbook -i rhelhost plugin.yml
result=$?
if [[ $result -eq 0 ]]; then
    echo "SUCCESS"
    ansible-playbook -i rhelhost testsummary.yml
else
    echo "FAILURE"
    exit 1
fi

Upvotes: 1

Related Questions