4

Is it possible to call a remote function defined in bash (for example added in one of the scripts that are stored under /etc/profile.d) via ansible ad-hoc command (using shell , command modules ?)

For example I have the following function that allows to see the status of apt history:

function apt-history(){
  case "$1" in
    install)
          cat /var/log/dpkg.log | grep 'install '
          ;;
    upgrade|remove)
          cat /var/log/dpkg.log | grep $1
          ;;
    rollback)
          cat /var/log/dpkg.log | grep upgrade | \
              grep "$2" -A10000000 | \
              grep "$3" -B10000000 | \
              awk '{print $4"="$5}'
          ;;
    *)
          cat /var/log/dpkg.log
          ;;
  esac
}

Is it possible to make a call to this function directly via function name from one of the ansible existing modules via ad-hoc command ? I know it would be possible to create a new script and call it directly remotely, but this is not what I want to achieve here. Any suggestions appreciated.

techraf
  • 64,883
  • 27
  • 193
  • 198
Patryk
  • 321
  • 2
  • 9

1 Answers1

3

You have to instantiate bash on the remote side, using the command or shell module like this :

ansible localhost -m command -a 'bash -lc apt-history'

This is a common trick if you need environment variables to be set-up.

leucos
  • 17,661
  • 1
  • 44
  • 34
  • 1
    ok great, it worked ! In order to pass arguments to remote function I did used quotation and a bit modified syntax and it works fine (btw command is a default module in ansible so I can skip it). My modified command: **ansible testbed -a 'bash -lc "apt-history install"'** – Patryk Oct 23 '14 at 06:55