0

I want to change the behaviour of an ansible playbook when it is executed from within an interactive console as opposed to non-interactive console (like CI environment)

How can I do this from inside the playbook?

sorin
  • 161,544
  • 178
  • 535
  • 806

1 Answers1

1

You need a way to determine whether your command runs from a CI or not. If you have access to what command to run then I would suggest to simply add some extra variables to the command, for example

ansible-playbook -i inv.yml --extra-vars "ci=True" playbook.yml

and then you can refer to the ci variable inside your playbook to determine what to do.

If this is cumbersome then the best thing to do is rely on the environmental variables that most CI servers set. You should consult the documentation of your CI server whether it sets any kind of environmental variables, and if yes what they do. Most (but not all) do set the CI variable to true. Worst case scenario you should specify this variable manually in the configuration of your CI server.

You should be able to access the environment variables of the management host machine using:

{{ lookup('env','CI') }}

Of course for these options to work you have to make sure that the environment variable is properly set on non-interactive shells (which the CI should do for you), and is not set on interactive ones.

SztupY
  • 10,291
  • 8
  • 64
  • 87
  • i am really tired about getting ugly workaround with Ansible. In Python there is a clear way to do it, one that does not involve ugly hacks that will not work in all cases: http://stackoverflow.com/questions/6108330/checking-for-interactive-shell-in-a-python-script – sorin Jun 28 '16 at 21:32
  • @sorin that's also quite hackish, and it relies on a fact that you need to have a standard input available. ansible generally is not really a tool which expects stdin in any form – SztupY Jun 28 '16 at 21:47
  • On the other hand it might be worth a try making that check as a separate ansible module, but my guess would be that ansible itself will redirect stdin for various reasons anyway so it won't be a tty – SztupY Jun 28 '16 at 21:48