More adventures with ansible
16 Dec 2019I’m still learning how to use ansible. It seems to actually be quite simple at heart, but it has its subtleties.
For instance, if you tell it to run a command on some other machine, it takes it very literally. You can tell it something like this:
command: pkginfo VENDORgit | grep VERSION.*2.24.1 || pkgadd -d /export/src/pkg VENDORgit
What that does is run the command “pkginfo” with the arguments
“VENDORgit”, “|”, “grep”, “VERSION.*2.24.1”, “||”, “pkgadd”, “-d”,
“/export/src/pkg”, “VENDORgit”. That is to say, it doesn’t bother
feeding it through a shell at all, so any shell-like directives you
throw in will be completely ignored, because all of those words you’ve
just specified on what you think is a command line just get passed
directly to the command you asked for as arguments. This results in a
spectacular yield of error messages, because the poor pkginfo command
has no idea what to do with all of that extra line noise you’ve just
thrown its way.
That wasn’t even slightly what I was expecting. I assumed that the “command” directive would just feed its operands into the shell. I blame bitter experience with a variety of other mass-provisioning software (hello, fabric, I’m talking about you).
It was all of course just a misunderstanding on my part. There is
another directive to feed the operands into the shell, and that is
shell. It works like this:
shell: pkginfo VENDORgit | grep VERSION.*2.24.1 || pkgadd -d /export/src/pkg VENDORgit
I didn’t know about the shell directive until I discovered it in,
well, let’s be honest, a StackOverflow thread, so of course things went
wrong for me. But now I know about the shell directive, so I guess
things will be going more smoothly from here on.
I have so much stuff to learn. Good thing I really enjoy learning stuff.