sandejai
sandejai

Reputation: 989

How to return a value from ansible playbook to an shell script?

I call a playbook from a shell script , example

#!/bin/bash
UPGRADE=`ansible-playbook -i /etc/ansible/hosts checkUpgrade.yml`
echo " UPGRADE VALUE $UPGRADE "

I want to return/set some variable from checkUpgrade.yml, so that my caller script can use it for further use.

Note: Don't want to write the value to file

Upvotes: 4

Views: 5001

Answers (1)

Paul Hodges
Paul Hodges

Reputation: 15248

Registering Variables for later use in playbooks

Using registered variables in conditionals

Reading Registered variable Return Values


If you register a variable on a command or shell, the return code is always(?) saved.

More specific to your apparent use, have the end of the playbook output some key sentinel string that you can scan for in $UPGRADE.

if grep -q SPECIAL_CONDITION_1 <<< "$UPGRADE"
then doStuff1
elif grep -q SPECIAL_CONDITION_2 <<< "$UPGRADE"
then doStuff2
...

ways to make more efficient, but this should make the point.

Upvotes: 0

Related Questions