1

I am making a simple script that will run on the Ansible node, and notify me via Slack api if upgrades are available.

If I execute script in my hosts (all Debian based), I receive notification in the Slack app as desired, but when it is run via Ansible, plays reports no errors but I never got the output.

What could be the issue? I am out of the ideas.

Bash script ran in the Ansible:

#!/bin/bash

UPGRADE_LIST=$(mktemp)
HOLDED_PACKAGES=$(mktemp)
TMP=$(mktemp)
HOST=$(hostname)
APT=$(which apt)



#Get packages that are holded by the pkg manager
dpkg --get-selections | awk '/hold$/{print $1}' > "${HOLDED_PACKAGES}"

#Get list of upgradeable packages
$APT list --upgradable > "${UPGRADE_LIST}"

#Filter it
awk  '/.*\/.*/ {FS="/";  print $1 }' "${UPGRADE_LIST}" > "${TMP}"


slack () {

    a=$1
    curl -X POST -H "Content-type: application/json" --data "{\"text\":\"${a}\"}" "API HOOK"
}

COUNT=$(wc -l "${TMP}")

    # If count is not equal to 0, then there available packages for upgrade
    if [[ $COUNT != 0 ]]; then
        c="Upgrade available for $HOST"
        slack $c

        #Print only those that aren't holded by dpkg
        while read line; do 
            pkg=$(grep -v $line "${TMP}")
            slack "${pkg}"
            pkg=""
        done < "${HOLDED_PACKAGES}"

    else 
     b="No upgrade available for ${HOST}"
     slack  $b
    fi

#CLEANUP
rm "${TMP}" "${UPGRADE_LIST}" "${HOLDED_PACKAGES}"

Playbook:

---
- hosts: debian
  tasks:
    - script: /etc/ansible/scripts/update-manager.sh

Edit:

Fixed. Was the local issue within inventory. It was pointing to a localhost which is Alpine.

closed as off-topic by Jeff Schaller, Patrick, G-Man, Anthon, Satō Katsura May 17 '17 at 7:10

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Jeff Schaller, Patrick, G-Man, Anthon, Satō Katsura
If this question can be reworded to fit the rules in the help center, please edit the question.